<commit_before># -*- coding: utf-8 -*-
# See LICENSE.txt for licensing terms
#$HeadURL$
#$LastChangedDate$
#$LastChangedRevision$

__docformat__ = 'reStructuredText'

import sys
import os
import tempfile
from os.path import abspath, dirname, expanduser, join
from string import ascii_lowercase
from urlparse import urljoin, urlparse
from copy import copy, deepcopy
from cgi import escape
from optparse import OptionParser
import logging

from docutils.languages import get_language
import docutils.readers.doctree
import docutils.core
import docutils.nodes

import pygments_code_block_directive # install code-block directive

from reportlab.platypus import *
from reportlab.platypus.flowables import _listWrapOn, _Container
from reportlab.lib.enums import *
from reportlab.lib.units import *
from reportlab.lib.pagesizes import *

from flowables import * # our own reportlab flowables

from svgimage import SVGImage
from math_directive import math_node
from math_flowable import Math
from aafigure_directive import Aanode

from log import log

from smartypants import smartyPants

# Is this really the best unescape in the stdlib for '&amp;' => '&'????
from xml.sax.saxutils import unescape

import config

#def escape (x,y):
#    "Dummy escape function to test for excessive escaping"
#    return x
from utils import log, parseRaw
import styles as sty

try:
    from PIL import Image as PILImage
except ImportError:
    try:
        import Image as PILImage
    except ImportError:
        log.warning("No support for images other than JPG,"
            " and limited support for image size. Please install PIL.")

try:
    import wordaxe
    from wordaxe.rl.paragraph import Paragraph
    from wordaxe.rl.styles import ParagraphStyle, getSampleStyleSheet
    from wordaxe.PyHnjHyphenator import PyHnjHyphenator
    from wordaxe.plugins.PyHyphenHyphenator import PyHyphenHyphenator
except ImportError:
    # log.warning("No support for hyphenation, install wordaxe")
    haveWordaxe = False
else:
    haveWordaxe = True

try:
    import sphinx
    HAS_SPHINX = True
except ImportError:
    HAS_SPHINX = False


class RstToPdf(object):

    def __init__(self, stylesheets=[], language='en_US',
                 header=None, footer=None,
                 inlinelinks=False, breaklevel=1,
                 fontPath=[], stylePath=[],
                 fitMode='shrink', sphinx=False,
                 smarty='0', baseurl=None,
                 repeatTableRows=False,
                 footnote_backlinks=True, inline_footnotes=False,
                 def_dpi=300, show_frame=False):
        global HAS_SPHINX
        self.language = language
        self.lowerroman = 'i ii iii iv v vi vii viii ix x xi'.split()
        self.loweralpha = ascii_lowercase
        self.doc_title = ""
        self.doc_author = ""
        self.decoration = {'header': header,
                           'footer': footer,
                           'endnotes': []}
        # find base path
        if hasattr(sys, 'frozen'):
            PATH = abspath(dirname(sys.executable))
        else:
            PATH = abspath(dirname(__file__))
        stylesheets = [join(PATH, 'styles', 'styles.json'),
                       join(PATH, 'styles', 'default.json')] + stylesheets
        self.styles = sty.StyleSheet(stylesheets,
                                     fontPath,
                                     stylePath,
                                     def_dpi=def_dpi)
        self.docutils_languages = {}
        self.inlinelinks = inlinelinks
        self.breaklevel = breaklevel
        self.fitMode = fitMode
        self.to_unlink = []
        self.smarty = smarty
        self.baseurl = baseurl
        self.repeatTableRows = repeatTableRows
        self.footnote_backlinks = footnote_backlinks
        self.inline_footnotes = inline_footnotes
        self.def_dpi=def_dpi
        self.show_frame=show_frame
        self.img_dir=os.path.join(abspath(dirname(__file__)), 'images')

        # Sorry about this, but importing sphinx.roles makes some
        # ordinary documents fail (demo.txt specifically) so
        # I can' t just try to import it outside. I need
        # to do it only if it's requested
        if HAS_SPHINX and sphinx:
            import sphinx.roles
        else:
            HAS_SPHINX = False

        if not self.styles.languages:
            self.styles.languages = [language]
            self.styles['bodytext'].language = language

        # Load the docutils language modules for all required languages
        for lang in self.styles.languages:
            try:
                self.docutils_languages[lang] = get_language(lang)
            except ImportError:
                try:
                    self.docutils_languages[lang] = \
                         get_language(lang.split('_', 1)[0])
                except ImportError:
                    log.warning("Can't load Docutils module \
                        for language %s", lang)

        # Load the hyphenators for all required languages
        if haveWordaxe:
            for lang in self.styles.languages:
                if lang.split('_', 1)[0] == 'de':
                    try:
                        from wordaxe.DCWHyphenator import DCWHyphenator
                        wordaxe.hyphRegistry[lang] = DCWHyphenator('de', 5)
                        continue
                    except Exception:
                        # hyphenators may not always be available or crash,
                        # e.g. wordaxe issue 2809074 (http://is.gd/16lqs)
                        log.warning("Can't load wordaxe DCW hyphenator"
                            " for German language, trying Py hyphenator instead")
                    else:
                        continue
                try:
                    from wordaxe.plugins.PyHyphenHyphenator import PyHyphenHyphenator
                    wordaxe.hyphRegistry[lang] = PyHyphenHyphenator(lang)
                except Exception:
                    log.warning("Can't load wordaxe Py hyphenator"
                        " for language %s, trying PyHnj hyphenator", lang)
                else:
                    continue
                try:
                    from wordaxe.PyHnjHyphenator import PyHnjHyphenator
                    wordaxe.hyphRegistry[lang] = PyHnjHyphenator(
                        lang, 5, purePython=True)
                except Exception:
                    log.warning("Can't load wordaxe PyHnj hyphenator"
                        " for language %s, trying base hyphenator", lang)
                else:
                    continue
                try:
                    from wordaxe.BaseHyphenator import BaseHyphenator
                    wordaxe.hyphRegistry[lang] = BaseHyphenator(lang)
                except Exception:
                    log.warning("Can't even load wordaxe base hyphenator")
            log.info('hyphenation by default in %s , loaded %s',
                self.styles['bodytext'].language,
                ','.join(self.styles.languages))

    def size_for_image_node(self, node):
        imgname = str(node.get("uri"))
        scale =float(node.get('scale', 100))/100

        # Figuring out the size to display of an image is ... annoying.
        # If the user provides a size with a unit, it's simple, adjustUnits
        # will return it in points and we're done.

        # However, often the unit wil be "%" (specially if it's meant for<commit_msg>PyHnjHyphenator crashes for most languages, I prefer not to hyphenate instead of crashing<commit_after># -*- coding: utf-8 -*-
# See LICENSE.txt for licensing terms
#$HeadURL$
#$LastChangedDate$
#$LastChangedRevision$

__docformat__ = 'reStructuredText'

import sys
import os
import tempfile
from os.path import abspath, dirname, expanduser, join
from string import ascii_lowercase
from urlparse import urljoin, urlparse
from copy import copy, deepcopy
from cgi import escape
from optparse import OptionParser
import logging

from docutils.languages import get_language
import docutils.readers.doctree
import docutils.core
import docutils.nodes

import pygments_code_block_directive # install code-block directive

from reportlab.platypus import *
from reportlab.platypus.flowables import _listWrapOn, _Container
from reportlab.lib.enums import *
from reportlab.lib.units import *
from reportlab.lib.pagesizes import *

from flowables import * # our own reportlab flowables

from svgimage import SVGImage
from math_directive import math_node
from math_flowable import Math
from aafigure_directive import Aanode

from log import log

from smartypants import smartyPants

# Is this really the best unescape in the stdlib for '&amp;' => '&'????
from xml.sax.saxutils import unescape

import config

#def escape (x,y):
#    "Dummy escape function to test for excessive escaping"
#    return x
from utils import log, parseRaw
import styles as sty

try:
    from PIL import Image as PILImage
except ImportError:
    try:
        import Image as PILImage
    except ImportError:
        log.warning("No support for images other than JPG,"
            " and limited support for image size. Please install PIL.")

try:
    import wordaxe
    from wordaxe.rl.paragraph import Paragraph
    from wordaxe.rl.styles import ParagraphStyle, getSampleStyleSheet
    # PyHnjHyphenator is broken for non-ascii characters, so
    # let's not use it and avoid useless crashes (http://is.gd/19efQ)
    
    #from wordaxe.PyHnjHyphenator import PyHnjHyphenator
    from wordaxe.plugins.PyHyphenHyphenator import PyHyphenHyphenator
except ImportError:
    # log.warning("No support for hyphenation, install wordaxe")
    haveWordaxe = False
else:
    haveWordaxe = True

try:
    import sphinx
    HAS_SPHINX = True
except ImportError:
    HAS_SPHINX = False


class RstToPdf(object):

    def __init__(self, stylesheets=[], language='en_US',
                 header=None, footer=None,
                 inlinelinks=False, breaklevel=1,
                 fontPath=[], stylePath=[],
                 fitMode='shrink', sphinx=False,
                 smarty='0', baseurl=None,
                 repeatTableRows=False,
                 footnote_backlinks=True, inline_footnotes=False,
                 def_dpi=300, show_frame=False):
        global HAS_SPHINX
        self.language = language
        self.lowerroman = 'i ii iii iv v vi vii viii ix x xi'.split()
        self.loweralpha = ascii_lowercase
        self.doc_title = ""
        self.doc_author = ""
        self.decoration = {'header': header,
                           'footer': footer,
                           'endnotes': []}
        # find base path
        if hasattr(sys, 'frozen'):
            PATH = abspath(dirname(sys.executable))
        else:
            PATH = abspath(dirname(__file__))
        stylesheets = [join(PATH, 'styles', 'styles.json'),
                       join(PATH, 'styles', 'default.json')] + stylesheets
        self.styles = sty.StyleSheet(stylesheets,
                                     fontPath,
                                     stylePath,
                                     def_dpi=def_dpi)
        self.docutils_languages = {}
        self.inlinelinks = inlinelinks
        self.breaklevel = breaklevel
        self.fitMode = fitMode
        self.to_unlink = []
        self.smarty = smarty
        self.baseurl = baseurl
        self.repeatTableRows = repeatTableRows
        self.footnote_backlinks = footnote_backlinks
        self.inline_footnotes = inline_footnotes
        self.def_dpi=def_dpi
        self.show_frame=show_frame
        self.img_dir=os.path.join(abspath(dirname(__file__)), 'images')

        # Sorry about this, but importing sphinx.roles makes some
        # ordinary documents fail (demo.txt specifically) so
        # I can' t just try to import it outside. I need
        # to do it only if it's requested
        if HAS_SPHINX and sphinx:
            import sphinx.roles
        else:
            HAS_SPHINX = False

        if not self.styles.languages:
            self.styles.languages = [language]
            self.styles['bodytext'].language = language

        # Load the docutils language modules for all required languages
        for lang in self.styles.languages:
            try:
                self.docutils_languages[lang] = get_language(lang)
            except ImportError:
                try:
                    self.docutils_languages[lang] = \
                         get_language(lang.split('_', 1)[0])
                except ImportError:
                    log.warning("Can't load Docutils module \
                        for language %s", lang)

        # Load the hyphenators for all required languages
        if haveWordaxe:
            for lang in self.styles.languages:
                if lang.split('_', 1)[0] == 'de':
                    try:
                        from wordaxe.DCWHyphenator import DCWHyphenator
                        wordaxe.hyphRegistry[lang] = DCWHyphenator('de', 5)
                        continue
                    except Exception:
                        # hyphenators may not always be available or crash,
                        # e.g. wordaxe issue 2809074 (http://is.gd/16lqs)
                        log.warning("Can't load wordaxe DCW hyphenator"
                            " for German language, trying Py hyphenator instead")
                    else:
                        continue
                try:
                    from wordaxe.plugins.PyHyphenHyphenator import PyHyphenHyphenator
                    wordaxe.hyphRegistry[lang] = PyHyphenHyphenator(lang)
                except Exception:
                    log.warning("Can't load wordaxe Py hyphenator"
                        " for language %s, trying base hyphenator", lang)
                else:
                    continue
                #try:
                    #from wordaxe.PyHnjHyphenator import PyHnjHyphenator
                    #wordaxe.hyphRegistry[lang] = PyHnjHyphenator(
                        #lang, 5, purePython=True)
                #except Exception:
                    #log.warning("Can't load wordaxe PyHnj hyphenator"
                        #" for language %s, trying base hyphenator", lang)
                #else:
                    #continue
                try:
                    from wordaxe.BaseHyphenator import BaseHyphenator
                    wordaxe.hyphRegistry[lang] = BaseHyphenator(lang)
                except Exception:
                    log.warning("Can't even load wordaxe base hyphenator")
            log.info('hyphenation by default in %s , loaded %s',
                self.styles['bodytext'].language,
                ','.join(self.styles.languages))

    def size_for_image_node(self, node):
        imgname = str(node.get("uri"))
        scale =float(node.get('scale', 100))/100

        # Figuring out the size to display of an image is ... annoying.
        # If the user provides a size with a unit, it's simple, adjustUnits
        # will return it in points and we're done.

        # However, often the unit wil be "%" (specially if it's meant for
{'fraction_non_alphanumeric': 0.07383616979008013, 'fraction_numerical': 0.0037083636845242035, 'mean_word_length': 1.9490333919156415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4186236', 'n_tokens_mistral': 4323, 'n_tokens_neox': 4033, 'n_words': 1181}