Beispiel #1
0
def convert_to_pango(txt, headers=None, options=None):
    '''
    Code partly taken from txt2tags tarball
    '''
    original_txt = txt

    # Here is the marked body text, it must be a list.
    txt = txt.split('\n')

    # Set the three header fields
    if headers is None:
        headers = ['', '', '']

    config = txt2tags.ConfigMaster()._get_defaults()

    config['outfile'] = txt2tags.MODULEOUT  # results as list
    config['target'] = 'xhtml'

    config['preproc'] = []
    # We need to escape the ampersand here, otherwise "&" would become
    # "&"
    config['preproc'].append([r'&', '&'])

    # Allow line breaks
    config['postproc'] = []
    config['postproc'].append([REGEX_LINEBREAK, '\n'])

    if options is not None:
        config.update(options)

    # Let's do the conversion
    try:
        body, toc = txt2tags.convert(txt, config)
        full_doc = body
        finished = txt2tags.finish_him(full_doc, config)
        result = ''.join(finished)

    # Txt2tags error, show the messsage to the user
    except txt2tags.error, msg:
        logging.error(msg)
        result = msg
Beispiel #2
0
def convert_to_pango(txt, headers=None, options=None):
    """
    Code partly taken from txt2tags tarball
    """
    original_txt = txt

    # Here is the marked body text, it must be a list.
    txt = txt.split("\n")

    # Set the three header fields
    if headers is None:
        headers = ["", "", ""]

    config = txt2tags.ConfigMaster()._get_defaults()

    config["outfile"] = txt2tags.MODULEOUT  # results as list
    config["target"] = "xhtml"

    config["preproc"] = []
    # We need to escape the ampersand here, otherwise "&" would become
    # "&"
    config["preproc"].append([r"&", "&"])

    # Allow line breaks
    config["postproc"] = []
    config["postproc"].append([REGEX_LINEBREAK, "\n"])

    if options is not None:
        config.update(options)

    # Let's do the conversion
    try:
        body, toc = txt2tags.convert(txt, config)
        full_doc = body
        finished = txt2tags.finish_him(full_doc, config)
        result = "".join(finished)

    # Txt2tags error, show the messsage to the user
    except txt2tags.error as msg:
        logging.error(msg)
        result = msg

    # Unknown error, show the traceback to the user
    except Exception:
        result = txt2tags.getUnknownErrorMessage()
        logging.error(result)

    # remove unwanted paragraphs
    result = result.replace("<p>", "").replace("</p>", "")

    logging.log(
        5,
        'Converted "%s" text to "%s" txt2tags markup'
        % (repr(original_txt), repr(result)),
    )

    # Remove unknown tags (<a>)
    def replace_links(match):
        """Return the link name."""
        return match.group(1)

    result = re.sub(REGEX_HTML_LINK, replace_links, result)

    try:
        Pango.parse_markup(result, -1, "0")
        # result is valid pango markup, return the markup.
        return result
    except GObject.GError:
        # There are unknown tags in the markup, return the original text
        logging.debug("There are unknown tags in the markup: %s" % result)
        return original_txt
Beispiel #3
0
def _get_config(target, options):
    # Set the configuration on the 'config' dict.
    config = txt2tags.ConfigMaster()._get_defaults()

    config["outfile"] = txt2tags.MODULEOUT  # results as list
    config["target"] = target

    # The Pre (and Post) processing config is a list of lists:
    # [ [this, that], [foo, bar], [patt, replace] ]
    config["postproc"] = []
    config["preproc"] = []
    config["style"] = []

    # Allow line breaks, r'\\\\' are 2 \ for regexes
    config["preproc"].append([REGEX_LINEBREAK, "LINEBREAK"])

    # Highlight hashtags.
    if target == "tex":
        config["preproc"].append(
            [HASHTAG.pattern, r"\1{\2\3BEGININDEX\3ENDINDEX|color:red}"]
        )
    else:
        config["preproc"].append([HASHTAG.pattern, r"\1{\2\3|color:red}"])

    # Escape color markup.
    config["preproc"].append([r"\{(.*?)\|color:(.+?)\}", ESCAPE_COLOR])

    if target in ["xhtml", "html"]:
        config["encoding"] = "UTF-8"  # document encoding
        config["toc"] = 0
        config["css-sugar"] = 1

        # Fix encoding for export opened in firefox
        config["postproc"].append([r"<head>", "<head>" + CHARSET_UTF8])

        # Line breaks
        config["postproc"].append([r"LINEBREAK", "<br />"])

        # Apply image resizing
        config["postproc"].append([r"src=\"WIDTH(\d+)-", r'width="\1" src="'])

        # {{red text|color:red}} -> <span style="color:red">red text</span>
        config["postproc"].append([COLOR_ESCAPED, r'<span style="color:\2">\1</span>'])

        # Custom css
        font = options.pop("font", "sans-serif")
        css = CSS % {
            "font": font,
            "bgcolor": options.get("bgcolor", "white"),
            "fgcolor": options.get("fgcolor", "black"),
        }
        config["postproc"].append([r"</head>", css + "</head>"])

        # MathJax
        if options.pop("add_mathjax"):
            config["postproc"].append([r"</body>", MATHJAX + "</body>"])

    elif target == "tex":
        config["encoding"] = "utf8"
        config["preproc"].append(["€", "Euro"])

        # Latex only allows whitespace and underscores in filenames if
        # the filename is surrounded by "...". This is in turn only possible
        # if the extension is omitted.
        config["preproc"].append([r'\[""', r'["""'])
        config["preproc"].append([r'""\.', r'""".'])

        scheme = filesystem.LOCAL_FILE_PEFIX

        # For images we have to omit the file:// prefix
        config["postproc"].append(
            [r'includegraphics\{(.*)"%s' % scheme, r'includegraphics{"\1']
        )

        # Special handling for LOCAL file links (Omit scheme, add run:)
        # \htmladdnormallink{file.txt}{file:///home/user/file.txt}
        # -->
        # \htmladdnormallink{file.txt}{run:/home/user/file.txt}
        config["postproc"].append(
            [
                r"htmladdnormallink\{(.*)\}\{%s(.*)\}" % scheme,
                r"htmladdnormallink{\1}{run:\2}",
            ]
        )

        # Line breaks
        config["postproc"].append([r"LINEBREAK", r"\\\\"])

        # Apply image resizing
        config["postproc"].append(
            [r'includegraphics\{("?)WIDTH(\d+)-', r"includegraphics[width=\2px]{\1"]
        )

        # We want the plain latex formulas unescaped.
        # Allowed formulas: $$...$$, \[...\], \(...\)
        config["preproc"].append(
            [r"\\\[\s*(.+?)\s*\\\]", r"BEGINEQUATION''\1''ENDEQUATION"]
        )
        config["preproc"].append(
            [r"\$\$\s*(.+?)\s*\$\$", r"BEGINEQUATION''\1''ENDEQUATION"]
        )
        config["postproc"].append([r"BEGINEQUATION(.+)ENDEQUATION", r"$$\1$$"])

        config["preproc"].append([r"\\\(\s*(.+?)\s*\\\)", r"BEGINMATH''\1''ENDMATH"])
        config["postproc"].append([r"BEGINMATH(.+)ENDMATH", r"$\1$"])

        # Fix utf8 quotations - „, “ and ” cause problems compiling the latex document.
        config["postproc"].extend([["„", '"'], ["”", '"'], ["“", '"']])

        # Enable index.
        config["style"].append("makeidx")
        config["postproc"].append([r"BEGININDEX(.+?)ENDINDEX", r"\\index{\1}"])
        config["postproc"].append(["begin{document}", "makeindex\n\\\\begin{document}"])
        config["postproc"].append(["end{document}", "printindex\n\n\\\\end{document}"])

        config["postproc"].append([COLOR_ESCAPED, r"\\textcolor{\2}{\1}"])

    elif target == "txt":
        # Line breaks
        config["postproc"].append([r"LINEBREAK", "\n"])

        # Apply image resizing ([WIDTH400-file:///pathtoimage.jpg])
        config["postproc"].append([r"\[WIDTH(\d+)-(.+)\]", r"[\2?\1]"])

    # Entry references
    if target in ["xhtml", "html"]:
        # txt2tags will generate links to the named entry references because they share common bracket
        # notation used by the URIs. We just need to add our internal schema to make it a proper URI.
        config["preproc"].append(
            [
                r"\[(?P<name>.+)\s+(?P<date>\d{4}-\d{2}-\d{2})\s*\]",
                r"[\g<name> #\g<date>]",
            ]
        )

        # Convert bracketed dates into named references where the date itself is being used as a name.
        # For example:
        # "Today is [2019-10-20]" will be converted into "Today is [2019-10-20 #2019-10-20]"
        config["preproc"].append(
            [r"\[(?P<date>\d{4}-\d{2}-\d{2})\]", r"[\g<date> #\g<date>]"]
        )
    else:
        # Links to entry references are not supported for targets other than (X)HTML
        config["preproc"].append(
            [r"\[(?P<name>.+)\s+(?P<date>\d{4}-\d{2}-\d{2})\]", r"\g<name> (\g<date>)"]
        )

    # Allow resizing images by changing
    # [filename.png?width] to [WIDTHwidth-filename.png]
    img_ext = r"png|jpe?g|gif|eps|bmp|svg"
    img_name = r"\S.*\S|\S"

    # Apply this prepoc only after the latex image quotes have been added
    config["preproc"].append(
        [r"\[({}\.({}))\?(\d+)\]".format(img_name, img_ext), r"[WIDTH\3-\1]"]
    )

    # Disable colors for all other targets.
    config["postproc"].append([COLOR_ESCAPED, r"\1"])

    config.update(options)

    return config
Beispiel #4
0
def _get_config(target, options):

    config = {}

    # Set the configuration on the 'config' dict.
    config = txt2tags.ConfigMaster()._get_defaults()

    config['outfile'] = txt2tags.MODULEOUT  # results as list
    config['target'] = target

    # The Pre (and Post) processing config is a list of lists:
    # [ [this, that], [foo, bar], [patt, replace] ]
    config['postproc'] = []
    config['preproc'] = []
    config['style'] = []

    # Allow line breaks, r'\\\\' are 2 \ for regexes
    config['preproc'].append([REGEX_LINEBREAK, 'LINEBREAK'])

    # Highlight hashtags.
    if target == 'tex':
        config['preproc'].append(
            [HASHTAG.pattern, r'\1{\2\3BEGININDEX\3ENDINDEX|color:red}'])
    else:
        config['preproc'].append([HASHTAG.pattern, r'\1{\2\3|color:red}'])

    # Escape color markup.
    config['preproc'].append([r'\{(.*?)\|color:(.+?)\}', ESCAPE_COLOR])

    if target in ['xhtml', 'html']:
        config['encoding'] = 'UTF-8'  # document encoding
        config['toc'] = 0
        config['css-sugar'] = 1

        # Fix encoding for export opened in firefox
        config['postproc'].append([r'<head>', '<head>' + CHARSET_UTF8])

        # Line breaks
        config['postproc'].append([r'LINEBREAK', '<br />'])

        # Apply image resizing
        config['postproc'].append([r'src=\"WIDTH(\d+)-', r'width="\1" src="'])

        # {{red text|color:red}} -> <span style="color:red">red text</span>
        config['postproc'].append(
            [COLOR_ESCAPED, r'<span style="color:\2">\1</span>'])

    elif target == 'tex':
        config['encoding'] = 'utf8'
        config['preproc'].append(['€', 'Euro'])

        # Latex only allows whitespace and underscores in filenames if
        # the filename is surrounded by "...". This is in turn only possible
        # if the extension is omitted.
        config['preproc'].append([r'\[""', r'["""'])
        config['preproc'].append([r'""\.', r'""".'])

        scheme = 'file:///' if sys.platform == 'win32' else 'file://'

        # For images we have to omit the file:// prefix
        config['postproc'].append(
            [r'includegraphics\{(.*)"%s' % scheme, r'includegraphics{"\1'])

        # Special handling for LOCAL file links (Omit scheme, add run:)
        # \htmladdnormallink{file.txt}{file:///home/user/file.txt}
        # -->
        # \htmladdnormallink{file.txt}{run:/home/user/file.txt}
        config['postproc'].append([
            r'htmladdnormallink\{(.*)\}\{%s(.*)\}' % scheme,
            r'htmladdnormallink{\1}{run:\2}'
        ])

        # Line breaks
        config['postproc'].append([r'LINEBREAK', r'\\\\'])

        # Apply image resizing
        config['postproc'].append([
            r'includegraphics\{("?)WIDTH(\d+)-',
            r'includegraphics[width=\2px]{\1'
        ])

        # We want the plain latex formulas unescaped.
        # Allowed formulas: $$...$$, \[...\], \(...\)
        config['preproc'].append(
            [r'\\\[\s*(.+?)\s*\\\]', r"BEGINEQUATION''\1''ENDEQUATION"])
        config['preproc'].append(
            [r'\$\$\s*(.+?)\s*\$\$', r"BEGINEQUATION''\1''ENDEQUATION"])
        config['postproc'].append([r'BEGINEQUATION(.+)ENDEQUATION', r'$$\1$$'])
        config['preproc'].append(
            [r'\\\(\s*(.+?)\s*\\\)', r"BEGINMATH''\1''ENDMATH"])
        config['postproc'].append([r'BEGINMATH(.+)ENDMATH', r'$\1$'])

        # Fix utf8 quotations - „, “ and ” cause problems compiling the latex document.
        config['postproc'].extend([[u'„', '"'], [u'”', '"'], [u'“', '"']])

        # Enable index.
        config['style'].append('makeidx')
        config['postproc'].append([r'BEGININDEX(.+?)ENDINDEX', r'\\index{\1}'])
        config['postproc'].append(
            ['begin{document}', 'makeindex\n\\\\begin{document}'])
        config['postproc'].append(
            ['end{document}', 'printindex\n\n\\\\end{document}'])

        config['postproc'].append([COLOR_ESCAPED, r'\\textcolor{\2}{\1}'])

    elif target == 'txt':
        # Line breaks
        config['postproc'].append([r'LINEBREAK', '\n'])

        # Apply image resizing ([WIDTH400-file:///pathtoimage.jpg])
        config['postproc'].append([r'\[WIDTH(\d+)-(.+)\]', r'[\2?\1]'])

    # Allow resizing images by changing
    # [filename.png?width] to [WIDTHwidth-filename.png]
    img_ext = r'png|jpe?g|gif|eps|bmp|svg'
    img_name = r'\S.*\S|\S'

    # Apply this prepoc only after the latex image quotes have been added
    config['preproc'].append(
        [r'\[(%s\.(%s))\?(\d+)\]' % (img_name, img_ext), r'[WIDTH\3-\1]'])

    # Disable colors for all other targets.
    config['postproc'].append([COLOR_ESCAPED, r'\1'])

    # MathJax
    if options.pop('add_mathjax'):
        config['postproc'].append([r'</body>', MATHJAX + '</body>'])

    config['postproc'].append([r'</body>', PRINT_FUNCTION + '</body>'])

    # Custom css
    fonts = options.pop('font', 'sans-serif')
    if 'html' in target:
        css = CSS % {'font': fonts, 'table_head_bg': TABLE_HEAD_BG}
        config['postproc'].append([r'</head>', css + '</head>'])

    config.update(options)

    return config
Beispiel #5
0
def _get_config(type):

    config = {}

    # Set the configuration on the 'config' dict.
    config = txt2tags.ConfigMaster()._get_defaults()

    # The Pre (and Post) processing config is a list of lists:
    # [ [this, that], [foo, bar], [patt, replace] ]
    config['postproc'] = []
    config['preproc'] = []

    if type == 'xhtml' or type == 'html':
        config['encoding'] = 'UTF-8'  # document encoding
        config['toc'] = 0
        config['style'] = [
            os.path.join(filesystem.files_dir, 'stylesheet.css')
        ]
        config['css-inside'] = 1
        config['css-sugar'] = 1

        # keepnote only recognizes "<strike>"
        config['postproc'].append(['(?i)(</?)s>', r'\1strike>'])

        # Allow line breaks, r'\\\\' are 2 \ for regexes
        config['postproc'].append([r'\\\\', '<br />'])

        # Apply image resizing
        config['postproc'].append([r'src=\"WIDTH(\d+)-', r'width="\1" src="'])

    elif type == 'tex':
        config['encoding'] = 'utf8'
        config['preproc'].append(['€', 'Euro'])

        # Latex only allows whitespace and underscores in filenames if
        # the filename is surrounded by "...". This is in turn only possible
        # if the extension is omitted
        config['preproc'].append([r'\[""', r'["""'])
        config['preproc'].append([r'""\.', r'""".'])

        scheme = 'file:///' if sys.platform == 'win32' else 'file://'

        # For images we have to omit the file:// prefix
        config['postproc'].append(
            [r'includegraphics\{(.*)"%s' % scheme, r'includegraphics{"\1'])
        #config['postproc'].append([r'includegraphics\{"file://', r'includegraphics{"'])

        # Special handling for LOCAL file links (Omit scheme, add run:)
        # \htmladdnormallink{file.txt}{file:///home/user/file.txt}
        # -->
        # \htmladdnormallink{file.txt}{run:/home/user/file.txt}
        config['postproc'].append([
            r'htmladdnormallink\{(.*)\}\{%s(.*)\}' % scheme,
            r'htmladdnormallink{\1}{run:\2}'
        ])

        # Allow line breaks, r'\\\\' are 2 \ for regexes
        config['postproc'].append([r'\$\\backslash\$\$\\backslash\$', r'\\\\'])

        # Apply image resizing
        config['postproc'].append([
            r'includegraphics\{("?)WIDTH(\d+)-',
            r'includegraphics[width=\2px]{\1'
        ])

    elif type == 'txt':
        # Allow line breaks, r'\\\\' are 2 \ for regexes
        config['postproc'].append([r'\\\\', '\n'])

        # Apply image resizing ([WIDTH400-file:///pathtoimage.jpg])
        config['postproc'].append([r'\[WIDTH(\d+)-(.+)\]', r'[\2?\1]'])

    # Allow resizing images by changing
    # [filename.png?width] to [WIDTHwidth-filename.png]
    img_ext = r'png|jpe?g|gif|eps|bmp'
    img_name = r'\S.*\S|\S'

    # Apply this prepoc only after the latex image quotes have been added
    config['preproc'].append(
        [r'\[(%s\.(%s))\?(\d+)\]' % (img_name, img_ext), r'[WIDTH\3-\1]'])

    return config