123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- """
- Math Render Plugin for Pelican
- ==============================
- This plugin allows your site to render Math. It uses
- the MathJax JavaScript engine.
- For markdown, the plugin works by creating a Markdown
- extension which is used during the markdown compilation stage.
- Math therefore gets treated like a "first class citizen" in Pelican
- For reStructuredText, the plugin instructs the rst engine
- to output Mathjax for for math.
- The mathjax script is automatically inserted into the HTML.
- Typogrify Compatibility
- -----------------------
- This plugin now plays nicely with Typogrify, but it requires
- Typogrify version 2.04 or above.
- User Settings
- -------------
- Users are also able to pass a dictionary of settings in the settings file which
- will control how the MathJax library renders things. This could be very useful
- for template builders that want to adjust the look and feel of the math.
- See README for more details.
- """
- import os
- import sys
- from pelican import signals
- from . pelican_mathjax_markdown_extension import PelicanMathJaxExtension
- def process_settings(pelicanobj):
- """Sets user specified MathJax settings (see README for more details)"""
- mathjax_settings = {}
-
-
-
-
-
- mathjax_settings['align'] = 'center'
- mathjax_settings['indent'] = '0em'
- mathjax_settings['show_menu'] = 'true'
- mathjax_settings['process_escapes'] = 'true'
- mathjax_settings['latex_preview'] = 'TeX'
- mathjax_settings['color'] = 'black'
-
- mathjax_settings['source'] = "'//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'"
-
- try:
- settings = pelicanobj.settings['MATH_JAX']
- except:
- settings = None
-
- if not isinstance(settings, dict):
- return mathjax_settings
-
- for key, value in ((key, settings[key]) for key in settings):
-
-
- if key == 'align' and isinstance(value, basestring):
- if value == 'left' or value == 'right' or value == 'center':
- mathjax_settings[key] = value
- else:
- mathjax_settings[key] = 'center'
- if key == 'indent':
- mathjax_settings[key] = value
- if key == 'show_menu' and isinstance(value, bool):
- mathjax_settings[key] = 'true' if value else 'false'
- if key == 'process_escapes' and isinstance(value, bool):
- mathjax_settings[key] = 'true' if value else 'false'
- if key == 'latex_preview' and isinstance(value, basestring):
- mathjax_settings[key] = value
- if key == 'color' and isinstance(value, basestring):
- mathjax_settings[key] = value
- return mathjax_settings
- def configure_typogrify(pelicanobj, mathjax_settings):
- """Instructs Typogrify to ignore math tags - which allows Typogfrify
- to play nicely with math related content"""
-
- if not pelicanobj.settings.get('TYPOGRIFY', False):
- return
- try:
- import typogrify
- from distutils.version import LooseVersion
- if LooseVersion(typogrify.__version__) < LooseVersion('2.0.7'):
- raise TypeError('Incorrect version of Typogrify')
- from typogrify.filters import typogrify
-
-
-
-
- pelicanobj.settings['TYPOGRIFY_IGNORE_TAGS'].extend(['.math', 'script'])
- except (ImportError, TypeError, KeyError) as e:
- pelicanobj.settings['TYPOGRIFY'] = False
- if isinstance(e, ImportError):
- print("\nTypogrify is not installed, so it is being ignored.\nIf you want to use it, please install via: pip install typogrify\n")
- if isinstance(e, TypeError):
- print("\nA more recent version of Typogrify is needed for the render_math module.\nPlease upgrade Typogrify to the latest version (anything equal or above version 2.0.7 is okay).\nTypogrify will be turned off due to this reason.\n")
- if isinstance(e, KeyError):
- print("\nA more recent version of Pelican is needed for Typogrify to work with render_math.\nPlease upgrade Pelican to the latest version or clone it directly from the master GitHub branch\nTypogrify will be turned off due to this reason\n")
- def process_mathjax_script(mathjax_settings):
- """Load the mathjax script template from file, and render with the settings"""
-
- with open (os.path.dirname(os.path.realpath(__file__))+'/mathjax_script_template', 'r') as mathjax_script_template:
- mathjax_template = mathjax_script_template.read()
- return mathjax_template.format(**mathjax_settings)
- def mathjax_for_markdown(pelicanobj, mathjax_settings):
- """Instantiates a customized markdown extension for handling mathjax
- related content"""
-
- config = {}
- config['mathjax_script'] = process_mathjax_script(mathjax_settings)
- config['math_tag_class'] = 'math'
-
- try:
- pelicanobj.settings['MD_EXTENSIONS'].append(PelicanMathJaxExtension(config))
- except:
- sys.excepthook(*sys.exc_info())
- sys.stderr.write("\nError - the pelican mathjax markdown extension failed to configure. MathJax is non-functional.\n")
- sys.stderr.flush()
- def mathjax_for_rst(pelicanobj, mathjax_settings):
- pelicanobj.settings['DOCUTILS_SETTINGS'] = {'math_output': 'MathJax'}
- rst_add_mathjax.mathjax_script = process_mathjax_script(mathjax_settings)
- def pelican_init(pelicanobj):
- """Loads the mathjax script according to the settings. Instantiate the Python
- markdown extension, passing in the mathjax script as config parameter
- """
-
- mathjax_settings = process_settings(pelicanobj)
-
- configure_typogrify(pelicanobj, mathjax_settings)
-
- mathjax_for_markdown(pelicanobj, mathjax_settings)
-
- mathjax_for_rst(pelicanobj, mathjax_settings)
- def rst_add_mathjax(instance):
- _, ext = os.path.splitext(os.path.basename(instance.source_path))
- if ext != '.rst':
- return
-
- if 'class="math"' in instance._content:
- instance._content += "<script type='text/javascript'>%s</script>" % rst_add_mathjax.mathjax_script
- def register():
- """Plugin registration"""
- signals.initialized.connect(pelican_init)
- signals.content_object_init.connect(rst_add_mathjax)
|