latex.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. latexScript = """
  12. <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type= "text/javascript">
  13. MathJax.Hub.Config({
  14. config: ["MMLorHTML.js"],
  15. jax: ["input/TeX","input/MathML","output/HTML-CSS","output/NativeMML"],
  16. TeX: { extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"], equationNumbers: { autoNumber: "AMS" } },
  17. extensions: ["tex2jax.js","mml2jax.js","MathMenu.js","MathZoom.js"],
  18. tex2jax: {
  19. inlineMath: [ [\'$\',\'$\'] ],
  20. displayMath: [ [\'$$\',\'$$\'] ],
  21. processEscapes: true },
  22. "HTML-CSS": {
  23. styles: { ".MathJax .mo, .MathJax .mi": {color: "black ! important"}}
  24. }
  25. });
  26. </script>
  27. """
  28. def addLatex(gen, metadata):
  29. """
  30. The registered handler for the latex plugin. It will add
  31. the latex script to the article metadata
  32. """
  33. if 'LATEX' in gen.settings.keys() and gen.settings['LATEX'] == 'article':
  34. if 'latex' in metadata.keys():
  35. metadata['latex'] = latexScript
  36. else:
  37. metadata['latex'] = latexScript
  38. def register():
  39. """
  40. Plugin registration
  41. """
  42. signals.article_generate_context.connect(addLatex)