Skip to content

Commit

Permalink
add new pandoc_with_filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
mpacer committed Jan 4, 2017
1 parent 0896b8b commit c61bdda
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
3 changes: 1 addition & 2 deletions nbconvert/filters/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,5 @@ def rawlatex2math(text):
json_data = json.loads(text, object_hook=rawlatex2math_hook)
return json.dumps(json_data)

temp_text =

return convert_pandoc(source, 'markdown', 'rst', extra_args=extra_args, filter_func=rawlatex2math)
return convert_pandoc(source, 'markdown', 'rst', extra_args=extra_args)
Binary file removed nbconvert/utils/.pandoc.py.swp
Binary file not shown.
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

0 comments on commit c61bdda

Please sign in to comment.