Skip to content
Permalink
Browse files

Generalizing some functionality from formula rendering plugins to all…

… plugin kinds. Allowing to extend the parsing environment.
  • Loading branch information
felixfontein committed Jan 15, 2017
1 parent a0d7cff commit 745a14d59e5a26b63065e333013def72dde9746b
@@ -172,6 +172,7 @@ def set_site(self, site):
else:
LOGGER.warn('Found unknown LaTeX page compiler plugin {0}!'.format(plugin.name))
other_plugins[plugin.name] = plugin
plugin.plugin_object.initialize(self, self.__parsing_environment)
except:
LOGGER.error('Found broken LaTeX page compiler plugin {0}!'.format(plugin.name))

@@ -182,14 +183,22 @@ def set_site(self, site):
self.__formula_renderer = renderer_plugins[renderer_name].plugin_object
self.__formula_renderer_name = renderer_name
self.__plugins = list(other_plugins.values())
self.__all_plugins = [self.__formula_renderer] + self.__plugins

# Configure plugins
for plugin in self.__all_plugins:
plugin.initialize(self, self.__parsing_environment)

def _get_dep_filename(self, post, lang):
"""Retrieve dependency filename."""
return post.translated_base_path(lang) + '.ltxdep'

def get_extra_targets(self, post, lang, dest):
"""Retrieve extra targets generated by page compiler."""
return [self._get_dep_filename(post, lang)] + self.__formula_renderer.get_extra_targets(post, lang, dest)
result = [self._get_dep_filename(post, lang)]
for plugin in self.__all_plugins:
result += plugin.get_extra_targets(post, lang, dest)
return result

def _read_extra_deps(self, post, lang):
"""Read extra dependencies from JSON file."""
@@ -223,8 +232,7 @@ def _add_extra_deps(self, post, lang, what, where):
'theorem_names': self._get_theorem_names(lang),
}, 'latex_page_compiler:config'))
# Add plugin dependencies
result.extend(self.__formula_renderer.add_extra_deps(post, lang, what, where))
for plugin in self.__plugins:
for plugin in self.__all_plugins:
result.extend(plugin.add_extra_deps(post, lang, what, where))
return result

@@ -247,7 +255,8 @@ def _format_data(self, data, latex_context):
"""Parse and HTMLify data from string, given LaTeX context."""
tree = parser.parse(data, self.__parsing_environment, filename=latex_context.name)
result = htmlify.HTMLify(tree, self.__formula_renderer, latex_context, beautify=self.__beautify, outer_indent=0)
result = self.__formula_renderer.modify_result(result, latex_context)
for plugin in self.__all_plugins:
result = plugin.modify_html_output(result, latex_context)
return result

def _get_theorem_names(self, lang):
@@ -266,9 +275,11 @@ def _compile_string_impl(self, data, source_path=None, is_two_file=True, post=No
if link_providers:
for link_provider in link_providers:
latex_context.add_link_provider(link_provider)
self.__formula_renderer.before_processing(latex_context, source_path, post)
for plugin in self.__all_plugins:
plugin.before_processing(latex_context, source_path, post)
output = self._format_data(data, latex_context)
self.__formula_renderer.after_processing(latex_context, source_path, post)
for plugin in self.__all_plugins:
plugin.after_processing(latex_context, source_path, post)
return (output, latex_context, []) # last part are shortcode dependencies
except Exception as e:
import traceback
@@ -327,7 +338,8 @@ def compile(self, source, dest, is_two_file=True, post=None, lang=None):
post.add_dependency_uptodate(nikola.utils.config_changed(uptodate_data['deps'], uptodate_data['name']), add='fragment', lang=lang)
for uptodate_data in latex_context.get_uptodate_dependencies_page():
post.add_dependency_uptodate(nikola.utils.config_changed(uptodate_data['deps'], uptodate_data['name']), add='page', lang=lang)
self.__formula_renderer.write_extra_targets(post, lang, dest, latex_context)
for plugin in self.__all_plugins:
plugin.write_extra_targets(post, lang, dest, latex_context)
except:
# If an exception was raised, remove output file and re-raise it
try:
@@ -129,12 +129,24 @@ def set_site(self, site):
site.latex_formula_collectors = []
site.latex_formula_collectors.append(self._collect_formulas)

def initialize(self, latex_compiler, latex_parsing_environment):
"""Initialize plugin.
Called after set_site and before anything else is done.
This can be used to extend the LaTeX parsing environment.
"""
pass

def create_context(self):
"""Create a FormulaContext object."""
"""Create a FormulaContext object.
Only used for formula rendering plugins (i.e.
when ``latex_plugin_type == 'formula_renderer'``).
"""
return FormulaContext(self.__formula_scale, self.__formula_color)

def get_extra_targets(self, post, lang, dest):
"""Return a list of extra formula-related targets."""
"""Return a list of extra targets."""
return [self._get_formulae_filename(post, lang)]

def add_extra_deps(self, post, lang, what, where):
@@ -153,7 +165,7 @@ def _write_formulae(self, latex_context, filename):
f.write(json.dumps(formulae, sort_keys=True).encode('utf-8'))

def write_extra_targets(self, post, lang, dest, latex_context):
"""Write extra formula-related targets."""
"""Write extra targets."""
self._write_formulae(latex_context, self._get_formulae_filename(post, lang))

def before_processing(self, latex_context, source_path=None, post=None):
@@ -167,7 +179,7 @@ def after_processing(self, latex_context, source_path=None, post=None):
nikola.utils.makedirs(os.path.split(fn)[0])
self._write_formulae(latex_context, fn)

def modify_result(self, output, latex_context):
def modify_html_output(self, output, latex_context):
"""Modify generated HTML output."""
return output

@@ -178,6 +190,9 @@ def render(self, formula, formula_context, formula_type, latex_context):
formula_context: a FormulaContext object created by this object (or a clone of it)
formula_type: one of 'inline', 'display', 'align', 'pstricks', 'tikzpicture'
latex_context: the LaTeX context object
Only used for formula rendering plugins (i.e.
when ``latex_plugin_type == 'formula_renderer'``).
"""
try:
lfr = self.site.latex_formula_renderer
@@ -63,12 +63,24 @@ def set_site(self, site):
super(MathJaxFormulaRenderer, self).set_site(site)
self.__script_origin = site.config.get('LATEX_MATHJAX_SCRIPT_ORIGIN', self.__script_origin)

def initialize(self, latex_compiler, latex_parsing_environment):
"""Initialize plugin.
Called after set_site and before anything else is done.
This can be used to extend the LaTeX parsing environment.
"""
pass

def create_context(self):
"""Create a FormulaContext object."""
"""Create a FormulaContext object.
Only used for formula rendering plugins (i.e.
when ``latex_plugin_type == 'formula_renderer'``).
"""
return FormulaContext()

def get_extra_targets(self, post, lang, dest):
"""Return a list of extra formula-related targets."""
"""Return a list of extra targets."""
return []

def add_extra_deps(self, post, lang, what, where):
@@ -81,7 +93,7 @@ def add_extra_deps(self, post, lang, what, where):
return []

def write_extra_targets(self, post, lang, dest, latex_context):
"""Write extra formula-related targets."""
"""Write extra targets."""
pass

def before_processing(self, latex_context, source_path=None, post=None):
@@ -92,7 +104,7 @@ def after_processing(self, latex_context, source_path=None, post=None):
"""Retrieve information from context after post is processed."""
pass

def modify_result(self, output, latex_context):
def modify_html_output(self, output, latex_context):
"""Modify generated HTML output."""
prefix = '''<script type="text/x-mathjax-config">MathJax.Hub.Config({tex2jax: {inlineMath: [['\\\\(','\\\\)']]}});</script>'''
prefix += '''<script type="application/javascript" src="''' + self.__script_origin + '''?config=TeX-AMS_HTML-full"></script>'''
@@ -105,6 +117,9 @@ def render(self, formula, formula_context, formula_type, latex_context):
formula_context: a FormulaContext object created by this object (or a clone of it)
formula_type: one of 'inline', 'display', 'align', 'pstricks', 'tikzpicture'
latex_context: the LaTeX context object
Only used for formula rendering plugins (i.e.
when ``latex_plugin_type == 'formula_renderer'``).
"""
if formula_type not in self.__delimiters:
raise NotImplementedError("Formula type '{}' is not supported by MathJax formula rendering backend!".format(formula_type))

0 comments on commit 745a14d

Please sign in to comment.
You can’t perform that action at this time.