latex.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. """
  3. Latex Plugin For Pelican
  4. ========================
  5. This plugin allows you to write mathematical equations in your articles using Latex.
  6. It uses the MathJax Latex JavaScript library to render latex that is embedded in
  7. between `$..$` for inline math and `$$..$$` for displayed math. It also allows for
  8. writing equations in by using `\begin{equation}`...`\end{equation}`.
  9. """
  10. from pelican import signals
  11. # Reference about dynamic loading of MathJax can be found at http://docs.mathjax.org/en/latest/dynamic.html
  12. # The https cdn address can be found at http://www.mathjax.org/resources/faqs/#problem-https
  13. latexScript = """
  14. <script type= "text/javascript">
  15. var s = document.createElement('script');
  16. s.type = 'text/javascript';
  17. s.src = 'https:' == document.location.protocol ? 'https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js' : 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML';
  18. s[(window.opera ? "innerHTML" : "text")] =
  19. "MathJax.Hub.Config({" +
  20. " config: ['MMLorHTML.js']," +
  21. " jax: ['input/TeX','input/MathML','output/HTML-CSS','output/NativeMML']," +
  22. " TeX: { extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'], equationNumbers: { autoNumber: 'AMS' } }," +
  23. " extensions: ['tex2jax.js','mml2jax.js','MathMenu.js','MathZoom.js']," +
  24. " tex2jax: { " +
  25. " inlineMath: [ [\'$\',\'$\'] ], " +
  26. " displayMath: [ [\'$$\',\'$$\'] ]," +
  27. " processEscapes: true }, " +
  28. " 'HTML-CSS': { " +
  29. " styles: { '.MathJax .mo, .MathJax .mi': {color: 'black ! important'}} " +
  30. " } " +
  31. "}); ";
  32. (document.body || document.getElementsByTagName('head')[0]).appendChild(s);
  33. </script>
  34. """
  35. def addLatex(gen, metadata):
  36. """
  37. The registered handler for the latex plugin. It will add
  38. the latex script to the article metadata
  39. """
  40. if 'LATEX' in gen.settings.keys() and gen.settings['LATEX'] == 'article':
  41. if 'latex' in metadata.keys():
  42. metadata['latex'] = latexScript
  43. else:
  44. metadata['latex'] = latexScript
  45. def register():
  46. """
  47. Plugin registration
  48. """
  49. signals.article_generator_context.connect(addLatex)
  50. signals.page_generator_context.connect(addLatex)