math.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. # -*- coding: utf-8 -*-
  2. """
  3. Math Render Plugin for Pelican
  4. ==============================
  5. This plugin allows your site to render Math. It uses
  6. the MathJax JavaScript engine.
  7. For markdown, the plugin works by creating a Markdown
  8. extension which is used during the markdown compilation
  9. stage. Math therefore gets treated like a "first class
  10. citizen" in Pelican
  11. For reStructuredText, the plugin instructs the rst engine
  12. to output Mathjax for all math.
  13. The mathjax script is by default automatically inserted
  14. into the HTML.
  15. Typogrify Compatibility
  16. -----------------------
  17. This plugin now plays nicely with Typogrify, but it
  18. requires Typogrify version 2.07 or above.
  19. User Settings
  20. -------------
  21. Users are also able to pass a dictionary of settings
  22. in the settings file which will control how the MathJax
  23. library renders things. This could be very useful for
  24. template builders that want to adjust the look and feel of
  25. the math. See README for more details.
  26. """
  27. import os
  28. import sys
  29. from pelican import signals, generators
  30. try:
  31. from bs4 import BeautifulSoup
  32. except ImportError as e:
  33. BeautifulSoup = None
  34. try:
  35. from . pelican_mathjax_markdown_extension import PelicanMathJaxExtension
  36. except ImportError as e:
  37. PelicanMathJaxExtension = None
  38. def process_settings(pelicanobj):
  39. """Sets user specified MathJax settings (see README for more details)"""
  40. mathjax_settings = {}
  41. # NOTE TO FUTURE DEVELOPERS: Look at the README and what is happening in
  42. # this function if any additional changes to the mathjax settings need to
  43. # be incorporated. Also, please inline comment what the variables
  44. # will be used for
  45. # Default settings
  46. mathjax_settings['auto_insert'] = True # if set to true, it will insert mathjax script automatically into content without needing to alter the template.
  47. mathjax_settings['align'] = 'center' # controls alignment of of displayed equations (values can be: left, right, center)
  48. mathjax_settings['indent'] = '0em' # if above is not set to 'center', then this setting acts as an indent
  49. mathjax_settings['show_menu'] = 'true' # controls whether to attach mathjax contextual menu
  50. mathjax_settings['process_escapes'] = 'true' # controls whether escapes are processed
  51. mathjax_settings['latex_preview'] = 'TeX' # controls what user sees while waiting for LaTex to render
  52. mathjax_settings['color'] = 'inherit' # controls color math is rendered in
  53. mathjax_settings['linebreak_automatic'] = 'false' # Set to false by default for performance reasons (see http://docs.mathjax.org/en/latest/output.html#automatic-line-breaking)
  54. mathjax_settings['tex_extensions'] = '' # latex extensions that can be embedded inside mathjax (see http://docs.mathjax.org/en/latest/tex.html#tex-and-latex-extensions)
  55. mathjax_settings['responsive'] = 'false' # Tries to make displayed math responsive
  56. mathjax_settings['responsive_break'] = '768' # The break point at which it math is responsively aligned (in pixels)
  57. mathjax_settings['mathjax_font'] = 'default' # forces mathjax to use the specified font.
  58. mathjax_settings['process_summary'] = BeautifulSoup is not None # will fix up summaries if math is cut off. Requires beautiful soup
  59. mathjax_settings['force_tls'] = 'false' # will force mathjax to be served by https - if set as False, it will only use https if site is served using https
  60. mathjax_settings['message_style'] = 'normal' # This value controls the verbosity of the messages in the lower left-hand corner. Set it to "none" to eliminate all messages
  61. # Source for MathJax
  62. mathjax_settings['source'] = "'//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'"
  63. # Get the user specified settings
  64. try:
  65. settings = pelicanobj.settings['MATH_JAX']
  66. except:
  67. settings = None
  68. # If no settings have been specified, then return the defaults
  69. if not isinstance(settings, dict):
  70. return mathjax_settings
  71. # The following mathjax settings can be set via the settings dictionary
  72. for key, value in ((key, settings[key]) for key in settings):
  73. # Iterate over dictionary in a way that is compatible with both version 2
  74. # and 3 of python
  75. if key == 'align':
  76. try:
  77. typeVal = isinstance(value, basestring)
  78. except NameError:
  79. typeVal = isinstance(value, str)
  80. if not typeVal:
  81. continue
  82. if value == 'left' or value == 'right' or value == 'center':
  83. mathjax_settings[key] = value
  84. else:
  85. mathjax_settings[key] = 'center'
  86. if key == 'indent':
  87. mathjax_settings[key] = value
  88. if key == 'source':
  89. mathjax_settings[key] = value
  90. if key == 'show_menu' and isinstance(value, bool):
  91. mathjax_settings[key] = 'true' if value else 'false'
  92. if key == 'message_style':
  93. mathjax_settings[key] = value if value is not None else 'none'
  94. if key == 'auto_insert' and isinstance(value, bool):
  95. mathjax_settings[key] = value
  96. if key == 'process_escapes' and isinstance(value, bool):
  97. mathjax_settings[key] = 'true' if value else 'false'
  98. if key == 'latex_preview':
  99. try:
  100. typeVal = isinstance(value, basestring)
  101. except NameError:
  102. typeVal = isinstance(value, str)
  103. if not typeVal:
  104. continue
  105. mathjax_settings[key] = value
  106. if key == 'color':
  107. try:
  108. typeVal = isinstance(value, basestring)
  109. except NameError:
  110. typeVal = isinstance(value, str)
  111. if not typeVal:
  112. continue
  113. mathjax_settings[key] = value
  114. if key == 'linebreak_automatic' and isinstance(value, bool):
  115. mathjax_settings[key] = 'true' if value else 'false'
  116. if key == 'process_summary' and isinstance(value, bool):
  117. if value and BeautifulSoup is None:
  118. print("BeautifulSoup4 is needed for summaries to be processed by render_math\nPlease install it")
  119. value = False
  120. mathjax_settings[key] = value
  121. if key == 'responsive' and isinstance(value, bool):
  122. mathjax_settings[key] = 'true' if value else 'false'
  123. if key == 'force_tls' and isinstance(value, bool):
  124. mathjax_settings[key] = 'true' if value else 'false'
  125. if key == 'responsive_break' and isinstance(value, int):
  126. mathjax_settings[key] = str(value)
  127. if key == 'tex_extensions' and isinstance(value, list):
  128. # filter string values, then add '' to them
  129. try:
  130. value = filter(lambda string: isinstance(string, basestring), value)
  131. except NameError:
  132. value = filter(lambda string: isinstance(string, str), value)
  133. value = map(lambda string: "'%s'" % string, value)
  134. mathjax_settings[key] = ',' + ','.join(value)
  135. if key == 'mathjax_font':
  136. try:
  137. typeVal = isinstance(value, basestring)
  138. except NameError:
  139. typeVal = isinstance(value, str)
  140. if not typeVal:
  141. continue
  142. value = value.lower()
  143. if value == 'sanserif':
  144. value = 'SansSerif'
  145. elif value == 'fraktur':
  146. value = 'Fraktur'
  147. elif value == 'typewriter':
  148. value = 'Typewriter'
  149. else:
  150. value = 'default'
  151. mathjax_settings[key] = value
  152. return mathjax_settings
  153. def process_summary(article):
  154. """Ensures summaries are not cut off. Also inserts
  155. mathjax script so that math will be rendered"""
  156. summary = article._get_summary()
  157. summary_parsed = BeautifulSoup(summary, 'html.parser')
  158. math = summary_parsed.find_all(class_='math')
  159. if len(math) > 0:
  160. last_math_text = math[-1].get_text()
  161. if len(last_math_text) > 3 and last_math_text[-3:] == '...':
  162. content_parsed = BeautifulSoup(article._content, 'html.parser')
  163. full_text = content_parsed.find_all(class_='math')[len(math)-1].get_text()
  164. math[-1].string = "%s ..." % full_text
  165. summary = summary_parsed.decode()
  166. article._summary = "%s<script type='text/javascript'>%s</script>" % (summary, process_summary.mathjax_script)
  167. def configure_typogrify(pelicanobj, mathjax_settings):
  168. """Instructs Typogrify to ignore math tags - which allows Typogrify
  169. to play nicely with math related content"""
  170. # If Typogrify is not being used, then just exit
  171. if not pelicanobj.settings.get('TYPOGRIFY', False):
  172. return
  173. try:
  174. import typogrify
  175. from distutils.version import LooseVersion
  176. if LooseVersion(typogrify.__version__) < LooseVersion('2.0.7'):
  177. raise TypeError('Incorrect version of Typogrify')
  178. from typogrify.filters import typogrify
  179. # At this point, we are happy to use Typogrify, meaning
  180. # it is installed and it is a recent enough version
  181. # that can be used to ignore all math
  182. # Instantiate markdown extension and append it to the current extensions
  183. pelicanobj.settings['TYPOGRIFY_IGNORE_TAGS'].extend(['.math', 'script']) # ignore math class and script
  184. except (ImportError, TypeError) as e:
  185. pelicanobj.settings['TYPOGRIFY'] = False # disable Typogrify
  186. if isinstance(e, ImportError):
  187. print("\nTypogrify is not installed, so it is being ignored.\nIf you want to use it, please install via: pip install typogrify\n")
  188. if isinstance(e, TypeError):
  189. 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")
  190. def process_mathjax_script(mathjax_settings):
  191. """Load the mathjax script template from file, and render with the settings"""
  192. # Read the mathjax javascript template from file
  193. with open (os.path.dirname(os.path.realpath(__file__))
  194. + '/mathjax_script_template', 'r') as mathjax_script_template:
  195. mathjax_template = mathjax_script_template.read()
  196. return mathjax_template.format(**mathjax_settings)
  197. def mathjax_for_markdown(pelicanobj, mathjax_script, mathjax_settings):
  198. """Instantiates a customized markdown extension for handling mathjax
  199. related content"""
  200. # Create the configuration for the markdown template
  201. config = {}
  202. config['mathjax_script'] = mathjax_script
  203. config['math_tag_class'] = 'math'
  204. config['auto_insert'] = mathjax_settings['auto_insert']
  205. # Instantiate markdown extension and append it to the current extensions
  206. try:
  207. if isinstance(pelicanobj.settings.get('MD_EXTENSIONS'), list): # pelican 3.6.3 and earlier
  208. pelicanobj.settings['MD_EXTENSIONS'].append(PelicanMathJaxExtension(config))
  209. else:
  210. pelicanobj.settings['MARKDOWN'].setdefault('extensions', []).append(PelicanMathJaxExtension(config))
  211. except:
  212. sys.excepthook(*sys.exc_info())
  213. sys.stderr.write("\nError - the pelican mathjax markdown extension failed to configure. MathJax is non-functional.\n")
  214. sys.stderr.flush()
  215. def mathjax_for_rst(pelicanobj, mathjax_script):
  216. """Setup math for RST"""
  217. docutils_settings = pelicanobj.settings.get('DOCUTILS_SETTINGS', {})
  218. docutils_settings['math_output'] = 'MathJax'
  219. pelicanobj.settings['DOCUTILS_SETTINGS'] = docutils_settings
  220. rst_add_mathjax.mathjax_script = mathjax_script
  221. def pelican_init(pelicanobj):
  222. """
  223. Loads the mathjax script according to the settings.
  224. Instantiate the Python markdown extension, passing in the mathjax
  225. script as config parameter.
  226. """
  227. # Process settings, and set global var
  228. mathjax_settings = process_settings(pelicanobj)
  229. # Generate mathjax script
  230. mathjax_script = process_mathjax_script(mathjax_settings)
  231. # Configure Typogrify
  232. configure_typogrify(pelicanobj, mathjax_settings)
  233. # Configure Mathjax For Markdown
  234. if PelicanMathJaxExtension:
  235. mathjax_for_markdown(pelicanobj, mathjax_script, mathjax_settings)
  236. # Configure Mathjax For RST
  237. mathjax_for_rst(pelicanobj, mathjax_script)
  238. # Set process_summary's mathjax_script variable
  239. process_summary.mathjax_script = None
  240. if mathjax_settings['process_summary']:
  241. process_summary.mathjax_script = mathjax_script
  242. def rst_add_mathjax(content):
  243. """Adds mathjax script for reStructuredText"""
  244. # .rst is the only valid extension for reStructuredText files
  245. _, ext = os.path.splitext(os.path.basename(content.source_path))
  246. if ext != '.rst':
  247. return
  248. # If math class is present in text, add the javascript
  249. # note that RST hardwires mathjax to be class "math"
  250. if 'class="math"' in content._content:
  251. content._content += "<script type='text/javascript'>%s</script>" % rst_add_mathjax.mathjax_script
  252. def process_rst_and_summaries(content_generators):
  253. """
  254. Ensure mathjax script is applied to RST and summaries are
  255. corrected if specified in user settings.
  256. Handles content attached to ArticleGenerator and PageGenerator objects,
  257. since the plugin doesn't know how to handle other Generator types.
  258. For reStructuredText content, examine both articles and pages.
  259. If article or page is reStructuredText and there is math present,
  260. append the mathjax script.
  261. Also process summaries if present (only applies to articles)
  262. and user wants summaries processed (via user settings)
  263. """
  264. for generator in content_generators:
  265. if isinstance(generator, generators.ArticlesGenerator):
  266. for article in (
  267. generator.articles +
  268. generator.translations +
  269. generator.drafts):
  270. rst_add_mathjax(article)
  271. #optionally fix truncated formulae in summaries.
  272. if process_summary.mathjax_script is not None:
  273. process_summary(article)
  274. elif isinstance(generator, generators.PagesGenerator):
  275. for page in generator.pages:
  276. rst_add_mathjax(page)
  277. def register():
  278. """Plugin registration"""
  279. signals.initialized.connect(pelican_init)
  280. signals.all_generators_finalized.connect(process_rst_and_summaries)