Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @mgeier's math tagging for rst conversion[WIP] #416

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions nbconvert/filters/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def markdown2html_mistune(source):
% _mistune_import_error)

from .pandoc import convert_pandoc

import json

__all__ = [
'markdown2html',
Expand Down Expand Up @@ -89,6 +89,8 @@ def markdown2rst(source, extra_args=None):

This function will raise an error if pandoc is not installed.
Any error messages generated by pandoc are printed to stderr.

It will interpret LaTeX environment declarations as ::math not ::raw.

Parameters
----------
Expand All @@ -100,4 +102,21 @@ def markdown2rst(source, extra_args=None):
out : string
Output as returned by pandoc.
"""
return convert_pandoc(source, 'markdown', 'rst', extra_args=extra_args)
def rawlatex2math_hook(obj):
if obj.get('t') == 'RawBlock' and obj['c'][0] == 'latex':
obj['t'] = 'Para'
obj['c'] = [{
't': 'Math',
'c': [
{'t': 'DisplayMath', 'c': []},
obj['c'][1],
]
}]
return obj

def rawlatex2math(text):
json_data = json.loads(text, object_hook=rawlatex2math_hook)
return json.dumps(json_data)


return convert_pandoc(source, 'markdown', 'rst', extra_args=extra_args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to use the new pandoc_with_filter function you defined here? At the moment, I don't think it's using the functions functions you define above.

40 changes: 40 additions & 0 deletions nbconvert/utils/pandoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@ def pandoc(source, fmt, to, extra_args=None, encoding='utf-8'):
out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
return out.rstrip('\n')

def pandoc_with_filter(source, fmt, to, filter_func=None, extra_args=None, encoding='utf-8'):
"""Convert an input string using pandoc.

Pandoc converts an input string `from` a format `to` a target format.

Parameters
----------
source : string
Input string, assumed to be valid format `from`.
fmt : string
The name of the input format (markdown, etc.)
to : string
The name of the output format (html, etc.)

Returns
-------
out : unicode
Output as returned by pandoc.

Raises
------
PandocMissing
If pandoc is not installed.

Any error messages generated by pandoc are printed to stderr.

"""
cmd = ['pandoc', '-f', fmt, '-t', to]
if extra_args:
cmd.extend(extra_args)

# this will raise an exception that will pop us out of here
check_pandoc_version()

# we can safely continue
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out, _ = p.communicate(cast_bytes(source, encoding))
out = TextIOWrapper(BytesIO(out), encoding, 'replace').read()
return out.rstrip('\n')


def get_pandoc_version():
"""Gets the Pandoc version if Pandoc is installed.
Expand Down