Пример #1
0
def convert_with_tex(content):
    json = convert(content, '--from', 'markdown+smart', '--to', 'json',
                   '--katex')

    altered = applyJSONFilters([tex_filter], json)

    return convert(altered, '--from', 'json', '--to', 'html')
def resolve_references(source):
    """
    This applies the resolve_one_reference to the text passed in via the source argument.

    This expects content in the form of a string encoded JSON object as represented
    internally in ``pandoc``.
    """
    return applyJSONFilters([resolve_one_reference], source)
Пример #3
0
def test_nested_envir_2():
    test_file_exa = r'''
    \begin{document}

    \begin{lemma}
        \label{lem:a_lemma}
        \begin{equation}
            1 = 1
        \end{equation}
        \begin{proof}
            Obviously true!
        \end{proof}
    \end{lemma}

    \begin{remark}
        \label{rem:a_remark}
        The property in \Cref{lem:a_lemma} can used with other\dots
    \end{remark}

    \end{document}
    '''
    json_res = pypandoc.convert_text(
        test_file_exa,
        # 'markdown_github+markdown_in_html_blocks',
        'json',
        format='latex',
        extra_args=('-s', '-R', '--wrap=none'),
    )
    # json_json_res = json.loads(json_res)

    pynoweb_tools.pandoc_utils.processed_figures = dict()

    filter_res = pandocfilters.applyJSONFilters([latex_prefilter],
                                                json_res,
                                                format='json')

    # filter_json_res = json.loads(filter_res)

    pandoc_res = pypandoc.convert_text(
        filter_res,
        'markdown_github+markdown_in_html_blocks',
        format='json',
        extra_args=('-s', '-R', '--wrap=none'),
    )

    # print(pandoc_res)

    # Make sure our nested div environments made it into the output.
    div_env_str = (r'<div class="proof" markdown="" env-number="1"'
                   r' title-name="">')
    assert div_env_str in pandoc_res

    # Make sure our nested lemma reference made it in.
    div_lemma_str = r'Lemma $\\eqref{lem:a\_lemma}$ '
    assert div_lemma_str in pandoc_res
Пример #4
0
def test_inline_plot_mpl_multiples():
    MD_SAMPLE = '''

# Creates Three figures Matplotlib figure

```python
#filter: {.run caption="Figure Number One" label="my_fig" hide_code=true title_as_caption=true figattr="#fig:1 width=5in"}
import matplotlib
matplotlib.use('AGG')
from matplotlib import pyplot as plt
plt.figure()
plt.plot([1, 2], [-1, -4], 'or-')
plt.title('This is figure caption from python')
```

```{.python .run caption="Second Figure" caption2="Third Figure" label="my_fig_2" label2="my_fig_3" hide_code=true figattr="#fig:2 width=8in" figattr2="#fig:3 width=3in tag='B.1'"}
# import matplotlib
# matplotlib.use('AGG')
# from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

# First Figure
plt.figure()
plt.plot([1, 2], [3, 4], 'dg-')


fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
Z = np.sin(np.sqrt(X**2 + Y**2))
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm)
fig.colorbar(surf, shrink=0.5, aspect=5)
```


'''
    ast_string = filter_pandoc_run_py.run_pandoc(MD_SAMPLE)
    processed = applyJSONFilters([filter_pandoc_run_py.run_py_code_block],
                                 ast_string)
    d = json.loads(processed)
    assert d['blocks'][1]['c'][0]['t'] == 'Image'
Пример #5
0
def xper():
    authors = Author.objects.all()
    if not len(authors):
        print("No hay autores")
        return
    author = authors[0]
    node = Node(
        node_type = 1,
        title = "Título",
        content = "# Heading\n\nEste es un párrafo.\n\n# Otro heading\n\nOtro párrafo.\n",
        author = author,
        modification_date = timezone,
        parent = None
    )
    
    print("Node created")
    print(node)
    print("   OK")
    
    tree = t.fromMarkdown(node.content)
    
    print(tree.json)
    
    print("------------------")
    
    print(tree.toHtml())
    
    def toCaps(key, value, format, meta):
        if key == 'Str':
            return Str(value.upper())
    
    jsonText = applyJSONFilters([toCaps], tree.json, "")
    
    class Obj:
        def __init__(self, str):
            self.str = str
        def read(self):
            return self.str
    
    obj = Obj(jsonText)
    
    jsonTree = json.load(obj)
    
    return jsonTree
Пример #6
0
def toJSONFilters(actions, input=sys.stdin.buffer, output=sys.stdout):
    """Generate a JSON-to-JSON filter from stdin to stdout
    The filter:
    * reads a JSON-formatted pandoc document from stdin
    * transforms it by walking the tree and performing the actions
    * returns a new JSON-formatted pandoc document to stdout
    The argument `actions` is a list of functions of the form
    `action(key, value, format, meta)`, as described in more
    detail under `walk`.
    This function calls `applyJSONFilters`, with the `format`
    argument provided by the first command-line argument,
    if present.  (Pandoc sets this by default when calling
    filters.)
    """
    # Modified from pandocfilters to work with IOBytes
    source = input.read()
    format = ""

    output.write(applyJSONFilters(actions, source, format))
Пример #7
0
def process_latex_envs(key, value, oformat, meta):
    r''' Check LaTeX RawBlock AST objects for environments (i.e.
    `\begin{env_name}` and `\end{env_name}`) and converts
    them to Div's with class attribute set to their LaTeX names
    (i.e. `env_name`).

    The new Div has a `markdown` attribute set so that its contents
    can be processed again by Pandoc.  This is needed for custom environments
    (e.g. and example environment with more text and math to be processed),
    which also means that *recursive Pandoc calls are needed* (since Pandoc
    already stopped short producing the RawBlocks we start with).
    For the recursive Pandoc calls to work, we need the Pandoc extension
    `+markdown_in_html_blocks` enabled, as well.
    '''

    if key != 'RawBlock' or value[0] != 'latex':
        return None

    global environment_counters

    env_info = env_pattern.search(value[1])
    if env_info is not None:
        env_groups = env_info.groups()
        env_name = env_groups[0]
        env_name = env_conversions.get(env_name, env_name)
        env_title = env_groups[1]

        if env_title is None:
            env_title = ""

        env_body = env_groups[2]

        env_num = environment_counters.get(env_name, 0)
        env_num += 1
        environment_counters[env_name] = env_num

        label_info = label_pattern.search(env_body)
        env_label = ""
        label_div = None
        if label_info is not None:
            env_label = label_info.group(2)
            label_div = label_to_mathjax(env_label, env_tag=env_num)

            # XXX: For the Pandoc-types we've been using, there's
            # a strict need to make Div values Block elements and not
            # Inlines, which Span is.  We wrap the Span in Para to
            # produce the requisite Block value.
            label_div = Para([label_div])

            # Now, remove the latex label string from the original
            # content:
            env_body = env_body.replace(label_info.group(1), '')

        # Div AST objects:
        # type Attr = (String, [String], [(String, String)])
        # Attributes: identifier, classes, key-value pairs
        div_attr = [env_label, [env_name], [['markdown', ''],
                                            ["env-number", str(env_num)],
                                            ['title-name', env_title]
                                            ]]

        pandoc_logger.debug(u"env_body (pre-processed): {}\n".format(
            str(env_body)))

        # XXX: Nested processing!
        env_body_proc = pypandoc.convert_text(env_body, 'json',
                                              format='latex',
                                              extra_args=(
                                                  '-s', '-R',
                                                  '--wrap=none'),
                                              )

        pandoc_logger.debug(u"env_body (pandoc processed): {}\n".format(
            env_body_proc))

        env_body_filt = applyJSONFilters(
            [latex_prefilter], env_body_proc, format='json')

        div_blocks = json.loads(env_body_filt)['blocks']

        if label_div is not None:
            div_blocks = [label_div] + div_blocks

        div_res = Div(div_attr, div_blocks)

        pandoc_logger.debug("div_res: {}\n".format(div_res))

        return div_res
    else:
        return []
Пример #8
0
        #
        # return RawInline("latex", "example~\\ref{{{}}}".format(label))

        # # create a linguex style gloss
        # # a) prepare the label
        # label = value[0][0]
        # if label != "": label = "\\label{{{}}}".format(label)
        # # b) prepare the text
        # lines = value[1].splitlines()
        # lines = "\\\\\n".join(lines)
        # # c) put it all together
        # latex_gloss = "\\egx. {} {}\n\n".format(label, lines)
        #
        # return(pandoc.RawBlock("latex", latex_gloss))


# if __name__ == "__main__":
#   pandoc.toJSONFilters([make_gloss, parse_references])

#
input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
input = input_stream.read()
# print(input, file=sys.stderr)
# sys.exit(1)

output = applyJSONFilters([make_gloss, parse_references], input, "")
# print(output, file=sys.stderr)
# sys.exit(1)

sys.stdout.write(output)
def convert_links(source):
    return applyJSONFilters([convert_link], source)
Пример #10
0
def debug_example():

    # pypandoc.__pandoc_path = '/home/bwillar0/.cabal/bin/pandoc'

    test_file = r'''
    \begin{document}
    %\graphicspath{{/tmp/bwillar0/}{../figures/}{./figures/}{./}}
    %\ref{fig:figure_with_label}

    \begin{figure}[htpb]
        %\center
        \includegraphics{figure_with_label.png}
        \caption{A figure caption!}
        \label{fig:figure_with_label}
    \end{figure}

    %\ref{fig:figure_with_label}

    \end{document}
    '''

    test_filename = '/home/bwillar0/projects/websites/brandonwillard.github.io/content/articles/src/more_proximal_estimation.tex'
    with open(test_filename, 'r') as f:
        test_file = ''.join(f.readlines())

    # For better filter debugging:
    json_res = pypandoc.convert_text(
        test_file,
        'json',
        format='latex',
        extra_args=
        ('-s', '-R', '--wrap=none',
         '--metadata=bibliography=/home/bwillar0/projects/websites/brandonwillard.github.io/content/articles/src/more-proximal-estimation.bib'
         ),
        filters=['pandoc-citeproc', 'PynowebFilter'],
    )

    import json
    json_json_res = json.loads(json_res)

    # filter_res = json.dumps(json_json_res)

    # Apply our filter...
    # Make sure we clear out the global(s)...
    pynoweb_tools.pandoc_utils.processed_figures = dict()
    from pynoweb_tools.pandoc_utils import latex_prefilter
    filter_res = pandocfilters.applyJSONFilters([latex_prefilter],
                                                json_res,
                                                format='json')

    filter_json_res = json.loads(filter_res)

    # Change some things...
    # Say we get a Pandoc error message with:
    #   $.blocks[47].c[1][0].c[1][0]
    problem_item = filter_json_res['blocks'][47]['c'][1][0]['c'][1][0]

    # problem_item['c'][0] = [u"pg_ls_plot", [], []]
    # problem_item['c'][1] = []
    # problem_item['c'][1] = [problem_item['c'][1]]

    # Hand-craft a test object...
    # from pandocfilters import (Math, Image, Div, RawInline, Span)
    # filter_json_res = {'blocks': [Para([Span(['blah', [], []], [Str('hi')])])],
    #                    'meta': {}, 'pandoc-api-version': [1, 17, 0, 5]}
    # filter_res = json.dumps(filter_json_res)

    html_res = pypandoc.convert_text(
        filter_res,
        'markdown_github+markdown_in_html_blocks+mmd_title_block+tex_math_dollars+tex_math_double_backslash+implicit_figures+citations+yaml_metadata_block+link_attributes+raw_html+raw_tex',
        format='json',
        extra_args=('-s', '-R', '--wrap=none', '--verbose'))

    print(html_res)