Example #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
Example #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
Example #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
Example #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
Example #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
Example #6
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