Exemple #1
0
def convert(txt, target, headers=None, options=None):
    '''
    Code partly taken from txt2tags tarball
    '''

    # 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 = _get_config(target)

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

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

    # Let's do the conversion
    try:
        headers   = txt2tags.doHeader(headers, config)
        body, toc = txt2tags.convert(txt, config)
        footer  = txt2tags.doFooter(config)
        toc = txt2tags.toc_tagger(toc, config)
        toc = txt2tags.toc_formatter(toc, config)
        full_doc  = headers + toc + body + footer
        finished  = txt2tags.finish_him(full_doc, config)
        result = '\n'.join(finished)

    # Txt2tags error, show the messsage to the user
    except txt2tags.error, msg:
        logging.error(msg)
        result = msg
Exemple #2
0
def convert(txt, target, data_dir, headers=None, options=None):
    """
    Code partly taken from txt2tags tarball
    """
    data_dir = str(data_dir)
    options = options or {}

    # Only add MathJax code if there is a formula.
    options["add_mathjax"] = (
        FORMULAS_SUPPORTED
        and "html" in target
        and any(x in txt for x in MATHJAX_DELIMITERS)
    )
    logging.debug("Add mathjax code: %s" % options["add_mathjax"])

    # Turn relative paths into absolute paths.
    txt = _convert_paths(txt, data_dir)

    # The body text must be a list.
    txt = txt.split("\n")

    # Set the three header fields
    if headers is None:
        if target == "tex":
            # LaTeX requires a title if \maketitle is used
            headers = ["RedNotebook", "", ""]
        else:
            headers = ["", "", ""]

    config = _get_config(target, options)

    # Let's do the conversion
    try:
        headers = txt2tags.doHeader(headers, config)
        body, toc = txt2tags.convert(txt, config)
        footer = txt2tags.doFooter(config)
        toc = txt2tags.toc_tagger(toc, config)
        toc = txt2tags.toc_formatter(toc, config)
        full_doc = headers + toc + body + footer
        finished = txt2tags.finish_him(full_doc, config)
        result = "\n".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 = (
            "<b>Error</b>: This day contains invalid "
            '<a href="http://txt2tags.org/markup.html">txt2tags markup</a>. '
            "You can help us fix this by submitting a bugreport in the "
            '<a href="https://code.google.com/p/txt2tags/issues/list">'
            "txt2tags bugtracker</a>. Please append the day's text to the issue."
        )
        logging.error("Invalid markup:\n%s" % txt2tags.getUnknownErrorMessage())
    return result
Exemple #3
0
def convert(txt, target, headers=None, options=None, append_whitespace=False):
    '''
    Code partly taken from txt2tags tarball
    '''

    # Here is the marked body text, it must be a list.
    txt = txt.split('\n')
    '''
    Without this HACK "First Line\n_second Line" is rendered to
    "First LineSecond Line", but we want "First Line Second Line"

    We only need this for the keepnote input, the exports work fine
    '''
    #TODO: Remove once keepnote files have been removed
    if append_whitespace:

        def add_whitespace(line):
            if line.rstrip().endswith(r'\\'):
                return line.rstrip()
            else:
                return line + ' '

        txt = map(add_whitespace, txt)

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

    config = _get_config(target)

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

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

    # Let's do the conversion
    try:
        headers = txt2tags.doHeader(headers, config)
        body, toc = txt2tags.convert(txt, config)
        footer = txt2tags.doFooter(config)
        toc = txt2tags.toc_tagger(toc, config)
        toc = txt2tags.toc_formatter(toc, config)
        full_doc = headers + toc + body + footer
        finished = txt2tags.finish_him(full_doc, config)
        result = '\n'.join(finished)

    # Txt2tags error, show the messsage to the user
    except txt2tags.error, msg:
        logging.error(msg)
        result = msg
Exemple #4
0
def convert(txt, target, headers=None, options=None, append_whitespace=False):
    '''
    Code partly taken from txt2tags tarball
    '''

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

    '''
    Without this HACK "First Line\n_second Line" is rendered to
    "First LineSecond Line", but we want "First Line Second Line"

    We only need this for the keepnote input, the exports work fine
    '''
    #TODO: Remove once keepnote files have been removed
    if append_whitespace:
        def add_whitespace(line):
            if line.rstrip().endswith(r'\\'):
                return line.rstrip()
            else:
                return line + ' '
        txt = map(add_whitespace, txt)

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

    config = _get_config(target)

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

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

    # Let's do the conversion
    try:
        headers   = txt2tags.doHeader(headers, config)
        body, toc = txt2tags.convert(txt, config)
        footer  = txt2tags.doFooter(config)
        toc = txt2tags.toc_tagger(toc, config)
        toc = txt2tags.toc_formatter(toc, config)
        full_doc  = headers + toc + body + footer
        finished  = txt2tags.finish_him(full_doc, config)
        result = '\n'.join(finished)

    # Txt2tags error, show the messsage to the user
    except txt2tags.error, msg:
        logging.error(msg)
        result = msg
Exemple #5
0
def convert(txt, target, data_dir, headers=None, options=None):
    '''
    Code partly taken from txt2tags tarball
    '''
    # Only add MathJax code if there is a formula.
    add_mathjax = (FORMULAS_SUPPORTED and 'html' in target and
                   any(x in txt for x in MATHJAX_DELIMITERS))
    logging.debug('add_mathjax: %s' % add_mathjax)

    # Turn relative paths into absolute paths.
    txt = _convert_paths(txt, data_dir)

    # The body text must be a list.
    txt = txt.split('\n')

    # Set the three header fields
    if headers is None:
        if target == 'tex':
            # LaTeX requires a title if \maketitle is used
            headers = ['RedNotebook', '', '']
        else:
            headers = ['', '', '']

    config = _get_config(target)

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

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

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

    # Let's do the conversion
    try:
        headers = txt2tags.doHeader(headers, config)
        body, toc = txt2tags.convert(txt, config)
        footer = txt2tags.doFooter(config)
        toc = txt2tags.toc_tagger(toc, config)
        toc = txt2tags.toc_formatter(toc, config)
        full_doc = headers + toc + body + footer
        finished = txt2tags.finish_him(full_doc, config)
        result = '\n'.join(finished)
    # Txt2tags error, show the messsage to the user
    except txt2tags.error, msg:
        logging.error(msg)
        result = msg
Exemple #6
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 "&amp;" would become
    # "&amp;amp;"
    config['preproc'].append([r'&amp;', '&'])

    # 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
Exemple #7
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 "&amp;" would become
    # "&amp;amp;"
    config['preproc'].append([r'&amp;', '&'])

    # 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
Exemple #8
0
def convert(txt, target, data_dir, headers=None, options=None):
    '''
    Code partly taken from txt2tags tarball
    '''
    options = options or {}

    # Only add MathJax code if there is a formula.
    options['add_mathjax'] = (FORMULAS_SUPPORTED and 'html' in target
                              and any(x in txt for x in MATHJAX_DELIMITERS))
    logging.debug('Add mathjax code: %s' % options['add_mathjax'])

    # Turn relative paths into absolute paths.
    txt = _convert_paths(txt, data_dir)

    # The body text must be a list.
    txt = txt.split('\n')

    # Set the three header fields
    if headers is None:
        if target == 'tex':
            # LaTeX requires a title if \maketitle is used
            headers = ['RedNotebook', '', '']
        else:
            headers = ['', '', '']

    config = _get_config(target, options)

    # Let's do the conversion
    try:
        headers = txt2tags.doHeader(headers, config)
        body, toc = txt2tags.convert(txt, config)
        footer = txt2tags.doFooter(config)
        toc = txt2tags.toc_tagger(toc, config)
        toc = txt2tags.toc_formatter(toc, config)
        full_doc = headers + toc + body + footer
        finished = txt2tags.finish_him(full_doc, config)
        result = '\n'.join(finished)
    # Txt2tags error, show the messsage to the user
    except txt2tags.error, msg:
        logging.error(msg)
        result = msg
Exemple #9
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 "&amp;" would become
    # "&amp;amp;"
    config["preproc"].append([r"&amp;", "&"])

    # 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