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

Link resolution #458

Merged
merged 16 commits into from
Nov 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion nbconvert/exporters/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from traitlets.config import Config

from nbconvert.filters.highlight import Highlight2Latex
from nbconvert.filters.filter_links import resolve_references
from .templateexporter import TemplateExporter


class LatexExporter(TemplateExporter):
"""
Exports to a Latex template. Inherit from this class if your template is
Expand Down Expand Up @@ -44,6 +44,11 @@ def _template_skeleton_path_default(self):

output_mimetype = 'text/latex'

def default_filters(self):
for x in super(LatexExporter, self).default_filters():
yield x
yield ('resolve_references', resolve_references)

@property
def default_config(self):
c = Config({
Expand Down
38 changes: 38 additions & 0 deletions nbconvert/filters/filter_links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
"""A pandoc filter used in converting notebooks to Latex.
Converts links between notebooks to Latex cross-references.
"""
import re

from pandocfilters import RawInline, applyJSONFilters

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)

def resolve_one_reference(key, val, fmt, meta):
"""
This takes a tuple of arguments that are compatible with ``pandocfilters.walk()`` that
allows identifying hyperlinks in the document and transforms them into valid LaTeX
\\ref{} calls so that linking to headers between cells is possible.

See the documentation in ``pandocfilters.walk()`` for further information on the meaning
and specification of ``key``, ``val``, ``fmt``, and ``meta``.
"""
Copy link
Member

Choose a reason for hiding this comment

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

Let's either put a docstring in or remove the empty docstring (I'm happy for this to have no docstring).


if key == 'Link':
target = val[2][0]
m = re.match(r'#(.+)$', target)
if m:
# pandoc automatically makes labels for headings.
label = m.group(1).lower()
label = re.sub(r'[^\w-]+', '', label) # Strip HTML entities
return RawInline('tex', r'Section \ref{%s}' % label)

# Other elements will be returned unchanged.

2 changes: 1 addition & 1 deletion nbconvert/templates/latex/document_contents.tplx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

% Render markdown
((* block markdowncell scoped *))
((( cell.source | citation2latex | strip_files_prefix | convert_pandoc('markdown', 'latex') )))
((( cell.source | citation2latex | strip_files_prefix | convert_pandoc('markdown', 'json',extra_args=[]) | resolve_references | convert_pandoc('json','latex'))))
((* endblock markdowncell *))

% Don't display unknown types
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def run(self):
'nbformat',
'entrypoints',
'bleach',
'pandocfilters>=1.4.1',
]

extras_require = setuptools_args['extras_require'] = {
Expand Down