Browse Source

Grammar and PEP8 fixes for render_math plugin

Justin Mayer 11 years ago
parent
commit
c38851cf9c
1 changed files with 33 additions and 27 deletions
  1. 33 27
      render_math/math.py

+ 33 - 27
render_math/math.py

@@ -1,38 +1,42 @@
-from __future__ import print_function
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 """
 """
-Math Render Plugin For Pelican
+Math Render Plugin for Pelican
 ==============================
 ==============================
-This plugin allows your site to render Math. It supports both LaTex and MathML
+This plugin allows your site to render Math. It supports both LaTeX and MathML
 using the MathJax JavaScript engine.
 using the MathJax JavaScript engine.
 
 
 Typogrify Compatibility
 Typogrify Compatibility
 -----------------------
 -----------------------
-This plugin now plays nicely with typogrify, but it requires
-typogrify version 2.04 or above.
+This plugin now plays nicely with Typogrify, but it requires
+Typogrify version 2.04 or above.
 
 
 User Settings
 User Settings
 -------------
 -------------
 Users are also able to pass a dictionary of settings in the settings file which
 Users are also able to pass a dictionary of settings in the settings file which
-will control how the mathjax library renders thing. This could be very useful
-for template builders that want to adjust look and feel of the math.
+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.
 See README for more details.
 """
 """
 
 
+from __future__ import print_function
+import os
+import re
+
 from pelican import signals
 from pelican import signals
 from pelican import contents
 from pelican import contents
-import re, os
+
 
 
 # Global Variables
 # Global Variables
-_TYPOGRIFY = None  # if typogrify is enabled, this is set to the typogrify.filter function
-_WRAP_LATEX = None  # the tag to wrap LaTex math in (needed to play nicely with typogrify or for template designers)
-_MATH_REGEX = re.compile(r'(\$\$|\$|\\begin\{(.+?)\}|<(math)(?:\s.*?)?>).*?(\1|\\end\{\2\}|</\3>)', re.DOTALL | re.IGNORECASE) #  used to detect math
+_TYPOGRIFY = None  # if Typogrify is enabled, this is set to the typogrify.filter function
+_WRAP_LATEX = None  # the tag to wrap LaTeX math in (needed to play nicely with Typogrify or for template designers)
+_MATH_REGEX = re.compile(r'(\$\$|\$|\\begin\{(.+?)\}|<(math)(?:\s.*?)?>).*?(\1|\\end\{\2\}|</\3>)', re.DOTALL | re.IGNORECASE)  # used to detect math
 _MATH_SUMMARY_REGEX = None  # used to match math in summary
 _MATH_SUMMARY_REGEX = None  # used to match math in summary
 _MATH_INCOMPLETE_TAG_REGEX = None  # used to match math that has been cut off in summary
 _MATH_INCOMPLETE_TAG_REGEX = None  # used to match math that has been cut off in summary
 _MATHJAX_SETTINGS = {}  # settings that can be specified by the user, used to control mathjax script settings
 _MATHJAX_SETTINGS = {}  # settings that can be specified by the user, used to control mathjax script settings
 with open (os.path.dirname(os.path.realpath(__file__))+'/mathjax_script.txt', 'r') as mathjax_script:  # Read the mathjax javascript from file
 with open (os.path.dirname(os.path.realpath(__file__))+'/mathjax_script.txt', 'r') as mathjax_script:  # Read the mathjax javascript from file
     _MATHJAX_SCRIPT=mathjax_script.read()
     _MATHJAX_SCRIPT=mathjax_script.read()
 
 
+
 # Python standard library for binary search, namely bisect is cool but I need
 # Python standard library for binary search, namely bisect is cool but I need
 # specific business logic to evaluate my search predicate, so I am using my
 # specific business logic to evaluate my search predicate, so I am using my
 # own version
 # own version
@@ -82,7 +86,7 @@ def ignore_content(content):
 def wrap_math(content, ignore_within):
 def wrap_math(content, ignore_within):
     """Wraps math in user specified tags.
     """Wraps math in user specified tags.
 
 
-    This is needed for typogrify to play nicely with math but it can also be
+    This is needed for Typogrify to play nicely with math but it can also be
     styled by template providers
     styled by template providers
     """
     """
 
 
@@ -116,7 +120,7 @@ def process_summary(instance, ignore_within):
 
 
     process_summary.altered_summary = False
     process_summary.altered_summary = False
     insert_mathjax = False
     insert_mathjax = False
-    end_tag = '</%s>' % _WRAP_LATEX if _WRAP_LATEX != None else ''
+    end_tag = '</%s>' % _WRAP_LATEX if _WRAP_LATEX is not None else ''
 
 
     # use content's _get_summary method to obtain summary
     # use content's _get_summary method to obtain summary
     summary = instance._get_summary()
     summary = instance._get_summary()
@@ -181,6 +185,7 @@ def process_summary(instance, ignore_within):
 
 
     return None  # Making it explicit that summary was not altered
     return None  # Making it explicit that summary was not altered
 
 
+
 def process_settings(settings):
 def process_settings(settings):
     """Sets user specified MathJax settings (see README for more details)"""
     """Sets user specified MathJax settings (see README for more details)"""
 
 
@@ -211,7 +216,7 @@ def process_settings(settings):
     # Setting this value to false means the template must be altered if this
     # Setting this value to false means the template must be altered if this
     # plugin is to work, and so it is only recommended for the template
     # plugin is to work, and so it is only recommended for the template
     # designer who wants maximum control.
     # designer who wants maximum control.
-    _MATHJAX_SETTINGS['auto_insert'] = True # controls whether mathjax script is automatically inserted into the content
+    _MATHJAX_SETTINGS['auto_insert'] = True  # controls whether mathjax script is automatically inserted into the content
 
 
     if not isinstance(settings, dict):
     if not isinstance(settings, dict):
         return
         return
@@ -251,8 +256,9 @@ def process_settings(settings):
             if value == 'force':
             if value == 'force':
                 _MATHJAX_SETTINGS['source'] = "'https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'"
                 _MATHJAX_SETTINGS['source'] = "'https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'"
 
 
+
 def process_content(instance):
 def process_content(instance):
-    """Processes content, with logic to ensure that typogrify does not clash
+    """Processes content, with logic to ensure that Typogrify does not clash
     with math.
     with math.
 
 
     In addition, mathjax script is inserted at the end of the content thereby
     In addition, mathjax script is inserted at the end of the content thereby
@@ -269,13 +275,13 @@ def process_content(instance):
     else:
     else:
         math = True if _MATH_REGEX.search(instance._content) else False
         math = True if _MATH_REGEX.search(instance._content) else False
 
 
-    # The user initially set typogrify to be True, but since it would clash
+    # The user initially set Typogrify to be True, but since it would clash
     # with math, we set it to False. This means that the default reader will
     # with math, we set it to False. This means that the default reader will
-    # not call typogrify, so it is called here, where we are able to control
+    # not call Typogrify, so it is called here, where we are able to control
     # logic for it ignore math if necessary
     # logic for it ignore math if necessary
     if _TYPOGRIFY:
     if _TYPOGRIFY:
-        # Tell typogrify to ignore the tags that math has been wrapped in
-        # also, typogrify must always ignore mml (math) tags
+        # Tell Typogrify to ignore the tags that math has been wrapped in
+        # also, Typogrify must always ignore mml (math) tags
         ignore_tags = [_WRAP_LATEX,'math'] if _WRAP_LATEX else ['math']
         ignore_tags = [_WRAP_LATEX,'math'] if _WRAP_LATEX else ['math']
 
 
         # Exact copy of the logic as found in the default reader
         # Exact copy of the logic as found in the default reader
@@ -295,7 +301,7 @@ def process_content(instance):
         # The summary needs special care because math math cannot just be cut
         # The summary needs special care because math math cannot just be cut
         # off
         # off
         summary = process_summary(instance, ignore_within)
         summary = process_summary(instance, ignore_within)
-        if summary != None:
+        if summary is not None:
             instance._summary = summary
             instance._summary = summary
 
 
 
 
@@ -316,13 +322,13 @@ def pelican_init(pelicanobj):
 
 
     process_settings(settings)
     process_settings(settings)
 
 
-    # Allows mathjax script to be accessed from template should it be needed
+    # Allows MathJax script to be accessed from template should it be needed
     pelicanobj.settings['MATHJAXSCRIPT'] = _MATHJAX_SCRIPT.format(**_MATHJAX_SETTINGS)
     pelicanobj.settings['MATHJAXSCRIPT'] = _MATHJAX_SCRIPT.format(**_MATHJAX_SETTINGS)
 
 
-    # If typogrify set to True, then we need to handle it manually so it does
-    # not conflict with Latex
+    # If Typogrify set to True, then we need to handle it manually so it does
+    # not conflict with LaTeX
     try:
     try:
-        if pelicanobj.settings['TYPOGRIFY'] == True:
+        if pelicanobj.settings['TYPOGRIFY'] is True:
             pelicanobj.settings['TYPOGRIFY'] = False
             pelicanobj.settings['TYPOGRIFY'] = False
             try:
             try:
                 from typogrify.filters import typogrify
                 from typogrify.filters import typogrify
@@ -331,7 +337,7 @@ def pelican_init(pelicanobj):
                 import inspect
                 import inspect
                 typogrify_args = inspect.getargspec(typogrify).args
                 typogrify_args = inspect.getargspec(typogrify).args
                 if len(typogrify_args) < 2 or 'ignore_tags' not in typogrify_args:
                 if len(typogrify_args) < 2 or 'ignore_tags' not in typogrify_args:
-                    raise TypeError('Incorrect version of typogrify')
+                    raise TypeError('Incorrect version of Typogrify')
 
 
                 # At this point, we are happy to use Typogrify, meaning
                 # At this point, we are happy to use Typogrify, meaning
                 # it is installed and it is a recent enough version
                 # it is installed and it is a recent enough version
@@ -339,9 +345,9 @@ def pelican_init(pelicanobj):
                 _TYPOGRIFY = typogrify
                 _TYPOGRIFY = typogrify
                 _WRAP_LATEX = 'mathjax' # default to wrap mathjax content inside of
                 _WRAP_LATEX = 'mathjax' # default to wrap mathjax content inside of
             except ImportError:
             except ImportError:
-                print("\nTypogrify is not installed, so it is being ignored.\nPlease install it if you want to use it: pip install typogrify\n")
+                print("\nTypogrify is not installed, so it is being ignored.\nIf you want to use it, please install via: pip install typogrify\n")
             except TypeError:
             except TypeError:
-                print("\nA more recent versio of Typogrify is needed for the render_math module.\nPlease upgrade the typogrify to the latest version (anything above version 2.04 is okay).\nTypogrify will be turned off due to this reason\n")
+                print("\nA more recent version of Typogrify is needed for the render_math module.\nPlease upgrade Typogrify to the latest version (anything above version 2.04 is okay).\nTypogrify will be turned off due to this reason.\n")
     except KeyError:
     except KeyError:
         pass
         pass