Browse Source

Merge pull request #516 from howthebodyworks/fix_render_math_2

address #514 by article postprocessing after internal links are resolved
Justin Mayer 9 years ago
parent
commit
f3e4cc3d7f
1 changed files with 39 additions and 19 deletions
  1. 39 19
      render_math/math.py

+ 39 - 19
render_math/math.py

@@ -30,7 +30,7 @@ See README for more details.
 import os
 import sys
 
-from pelican import signals
+from pelican import signals, generators
 
 try:
     from bs4 import BeautifulSoup
@@ -202,7 +202,7 @@ def process_summary(article):
         article._summary = "%s<script type='text/javascript'>%s</script>" % (summary, process_summary.mathjax_script)
 
 def configure_typogrify(pelicanobj, mathjax_settings):
-    """Instructs Typogrify to ignore math tags - which allows Typogfrify
+    """Instructs Typogrify to ignore math tags - which allows Typogrify
     to play nicely with math related content"""
 
     # If Typogrify is not being used, then just exit
@@ -240,7 +240,8 @@ def process_mathjax_script(mathjax_settings):
     """Load the mathjax script template from file, and render with the settings"""
     
     # Read the mathjax javascript template from file
-    with open (os.path.dirname(os.path.realpath(__file__))+'/mathjax_script_template', 'r') as mathjax_script_template:
+    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)
@@ -270,8 +271,10 @@ def mathjax_for_rst(pelicanobj, mathjax_script):
     rst_add_mathjax.mathjax_script = mathjax_script
 
 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
+    """
+    Loads the mathjax script according to the settings.
+    Instantiate the Python markdown extension, passing in the mathjax
+    script as config parameter.
     """
 
     # Process settings, and set global var
@@ -295,29 +298,46 @@ def pelican_init(pelicanobj):
     if mathjax_settings['process_summary']:
         process_summary.mathjax_script = mathjax_script
 
-def rst_add_mathjax(article):
-    """Adds mathjax script for RST"""
+def rst_add_mathjax(content):
+    """Adds mathjax script for reStructuredText"""
 
     # This is only value for .rst files
-    _, ext = os.path.splitext(os.path.basename(article.source_path))
+    _, ext = os.path.splitext(os.path.basename(content.source_path))
     if ext != '.rst':
         return
 
     # If math class is present in text, add the javascript
-    if 'class="math"' in article._content:
-        article._content += "<script type='text/javascript'>%s</script>" % rst_add_mathjax.mathjax_script
-
-def parse_articles(article_generator):
-    """Adds mathjax script to RST and processes summaries"""
+    if 'class="math"' in content._content:
+        content._content += "<script type='text/javascript'>%s</script>" % rst_add_mathjax.mathjax_script
 
-    for article in article_generator.articles:
-        
-        rst_add_mathjax(article)
+def process_rst_and_summaries(content_generators):
+    """
+    Ensure mathjax script is applied to RST and summaries are
+    corrected if specified in user settings.
+    
+    Handle content attached to ArticleGenerator and PageGenerator objects,
+    since we don't know how to handle other Generator types.
+    
+    For reStructuredText content, examine both articles and pages.
+    If article or page is reStructuredText and there is math present,
+    append the mathjax script.
 
-        if process_summary.mathjax_script is not None:
-            process_summary(article)
+    Also process summaries if present (only applies to articles)
+    and user wants summaries processed (via user settings)
+    """
+    
+    for generator in content_generators:
+        if isinstance(generator, generators.ArticlesGenerator):
+            for article in generator.articles:
+                rst_add_mathjax(article)
+                #optionally fix truncated formulae in summaries.
+                if process_summary.mathjax_script is not None:
+                    process_summary(article)
+        elif isinstance(generator, generators.PagesGenerator):
+            for page in generator.pages:
+                rst_add_mathjax(page)
 
 def register():
     """Plugin registration"""
     signals.initialized.connect(pelican_init)
-    signals.article_generator_finalized.connect(parse_articles)
+    signals.all_generators_finalized.connect(process_rst_and_summaries)