diff --git a/api/exporter.py b/api/exporter.py index 17f1367..ef1bf48 100755 --- a/api/exporter.py +++ b/api/exporter.py @@ -344,7 +344,7 @@ class Exporter(Configurable): c.ExtractFigureTransformer.display_data_priority=['latex', 'svg', 'png', 'jpg', 'jpeg'] c.ExtractFigureTransformer.extra_ext_map={'svg':'pdf'} c.ExtractFigureTransformer.enabled=True - + # Enable latex transformers (make markdown2latex work with math $.) c.LatexTransformer.enabled=True c.SphinxTransformer.enabled = True @@ -352,7 +352,7 @@ class Exporter(Configurable): elif export_format == 'markdown': c.NbconvertApp.fileext='md' c.ExtractFigureTransformer.enabled=True - + elif export_format == 'python': c.NbconvertApp.fileext='py' diff --git a/utils/datatypefilter.py b/filters/datatypefilter.py similarity index 100% rename from utils/datatypefilter.py rename to filters/datatypefilter.py diff --git a/filters/latex.py b/filters/latex.py new file mode 100644 index 0000000..205dea6 --- /dev/null +++ b/filters/latex.py @@ -0,0 +1,72 @@ +"""Latex transformer. + +Module that allows latex output notebooks to be conditioned before +they are converted. +""" +#----------------------------------------------------------------------------- +# Copyright (c) 2013, the IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- +from __future__ import print_function + +#----------------------------------------------------------------------------- +# Functions +#----------------------------------------------------------------------------- +def rm_math_space(text): + """ + Remove the space between latex math commands and enclosing $ symbols. + """ + + # First, scan through the markdown looking for $. If + # a $ symbol is found, without a preceding \, assume + # it is the start of a math block. UNLESS that $ is + # not followed by another within two math_lines. + math_regions = [] + math_lines = 0 + within_math = False + math_start_index = 0 + ptext = '' + last_character = "" + skip = False + for index, char in enumerate(text): + + #Make sure the character isn't preceeded by a backslash + if (char == "$" and last_character != "\\"): + + # Close the math region if this is an ending $ + if within_math: + within_math = False + skip = True + ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$' + math_regions.append([math_start_index, index+1]) + else: + + # Start a new math region + within_math = True + math_start_index = index + math_lines = 0 + + # If we are in a math region, count the number of lines parsed. + # Cancel the math region if we find two line breaks! + elif char == "\n": + if within_math: + math_lines += 1 + if math_lines > 1: + within_math = False + ptext = ptext+text[math_start_index:index] + + # Remember the last character so we can easily watch + # for backslashes + last_character = char + if not within_math and not skip: + ptext = ptext+char + if skip: + skip = False + return ptext diff --git a/utils/markdown.py b/filters/markdown.py similarity index 100% rename from utils/markdown.py rename to filters/markdown.py diff --git a/utils/strings.py b/filters/strings.py similarity index 100% rename from utils/strings.py rename to filters/strings.py diff --git a/transformers/latex.py b/transformers/latex.py index 772d4cd..b6a7294 100644 --- a/transformers/latex.py +++ b/transformers/latex.py @@ -40,58 +40,3 @@ class LatexTransformer(ActivatableTransformer): if hasattr(cell, "source") and cell.cell_type == "markdown": cell.source = rm_math_space(cell.source) return cell, other - -#----------------------------------------------------------------------------- -# Functions -#----------------------------------------------------------------------------- -def rm_math_space(text): - """ - Remove the space between latex math commands and enclosing $ symbols. - """ - - # First, scan through the markdown looking for $. If - # a $ symbol is found, without a preceding \, assume - # it is the start of a math block. UNLESS that $ is - # not followed by another within two math_lines. - math_regions = [] - math_lines = 0 - within_math = False - math_start_index = 0 - ptext = '' - last_character = "" - skip = False - for index, char in enumerate(text): - - #Make sure the character isn't preceeded by a backslash - if (char == "$" and last_character != "\\"): - - # Close the math region if this is an ending $ - if within_math: - within_math = False - skip = True - ptext = ptext+'$'+text[math_start_index+1:index].strip()+'$' - math_regions.append([math_start_index, index+1]) - else: - - # Start a new math region - within_math = True - math_start_index = index - math_lines = 0 - - # If we are in a math region, count the number of lines parsed. - # Cancel the math region if we find two line breaks! - elif char == "\n": - if within_math: - math_lines += 1 - if math_lines > 1: - within_math = False - ptext = ptext+text[math_start_index:index] - - # Remember the last character so we can easily watch - # for backslashes - last_character = char - if not within_math and not skip: - ptext = ptext+char - if skip: - skip = False - return ptext