PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.

Wed, 23 Oct 2019 19:57:12 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 23 Oct 2019 19:57:12 +0200
changeset 7304
b072a364dd8d
parent 7303
b30e4fd161b8
child 7305
928858251f7a

PreviewerHTML: more enhancments for the Markdown previewer related to MathJax.

eric6/Preferences/ConfigurationPages/EditorFilePage.py file | annotate | diff | comparison | revisions
eric6/Preferences/ConfigurationPages/EditorFilePage.ui file | annotate | diff | comparison | revisions
eric6/Preferences/__init__.py file | annotate | diff | comparison | revisions
eric6/UI/Previewers/PreviewerHTML.py file | annotate | diff | comparison | revisions
--- a/eric6/Preferences/ConfigurationPages/EditorFilePage.py	Wed Oct 23 19:48:42 2019 +0200
+++ b/eric6/Preferences/ConfigurationPages/EditorFilePage.py	Wed Oct 23 19:57:12 2019 +0200
@@ -88,26 +88,31 @@
         
         self.previewHtmlExtensionsEdit.setText(
             " ".join(Preferences.getEditor("PreviewHtmlFileNameExtensions")))
+        
         self.previewMarkdownExtensionsEdit.setText(
             " ".join(
                 Preferences.getEditor("PreviewMarkdownFileNameExtensions")))
-        self.previewRestExtensionsEdit.setText(
-            " ".join(Preferences.getEditor("PreviewRestFileNameExtensions")))
-        self.previewQssExtensionsEdit.setText(
-            " ".join(Preferences.getEditor("PreviewQssFileNameExtensions")))
         self.previewRestSphinxCheckBox.setChecked(
             Preferences.getEditor("PreviewRestUseSphinx"))
         self.previewMarkdownNLtoBreakCheckBox.setChecked(
             Preferences.getEditor("PreviewMarkdownNLtoBR"))
         self.previewMarkdownPyMdownCheckBox.setChecked(
             Preferences.getEditor("PreviewMarkdownUsePyMdownExtensions"))
+        self.previewMarkdownMathJaxCheckBox.setChecked(
+            Preferences.getEditor("PreviewMarkdownMathJax"))
         index = self.previewMarkdownHTMLFormatComboBox.findText(
             Preferences.getEditor("PreviewMarkdownHTMLFormat"))
         self.previewMarkdownHTMLFormatComboBox.setCurrentIndex(index)
+        
+        self.previewRestExtensionsEdit.setText(
+            " ".join(Preferences.getEditor("PreviewRestFileNameExtensions")))
         index = self.previewRestDocutilsHTMLFormatComboBox.findText(
             Preferences.getEditor("PreviewRestDocutilsHTMLFormat"))
         self.previewRestDocutilsHTMLFormatComboBox.setCurrentIndex(index)
         
+        self.previewQssExtensionsEdit.setText(
+            " ".join(Preferences.getEditor("PreviewQssFileNameExtensions")))
+        
     def save(self):
         """
         Public slot to save the Editor File Handling configuration.
@@ -165,19 +170,12 @@
             "PreviewHtmlFileNameExtensions",
             [ext.strip() for ext in
              self.previewHtmlExtensionsEdit.text().split()])
+        
         Preferences.setEditor(
             "PreviewMarkdownFileNameExtensions",
             [ext.strip() for ext in
              self.previewMarkdownExtensionsEdit.text().split()])
         Preferences.setEditor(
-            "PreviewRestFileNameExtensions",
-            [ext.strip() for ext in
-             self.previewRestExtensionsEdit.text().split()])
-        Preferences.setEditor(
-            "PreviewQssFileNameExtensions",
-            [ext.strip() for ext in
-             self.previewQssExtensionsEdit.text().split()])
-        Preferences.setEditor(
             "PreviewRestUseSphinx",
             self.previewRestSphinxCheckBox.isChecked())
         Preferences.setEditor(
@@ -187,11 +185,24 @@
             "PreviewMarkdownUsePyMdownExtensions",
             self.previewMarkdownPyMdownCheckBox.isChecked())
         Preferences.setEditor(
+            "PreviewMarkdownMathJax",
+            self.previewMarkdownMathJaxCheckBox.isChecked())
+        Preferences.setEditor(
             "PreviewMarkdownHTMLFormat",
             self.previewMarkdownHTMLFormatComboBox.currentText())
+        
+        Preferences.setEditor(
+            "PreviewRestFileNameExtensions",
+            [ext.strip() for ext in
+             self.previewRestExtensionsEdit.text().split()])
         Preferences.setEditor(
             "PreviewRestDocutilsHTMLFormat",
             self.previewRestDocutilsHTMLFormatComboBox.currentText())
+        
+        Preferences.setEditor(
+            "PreviewQssFileNameExtensions",
+            [ext.strip() for ext in
+             self.previewQssExtensionsEdit.text().split()])
     
     def __setDefaultFiltersLists(self, keepSelection=False):
         """
--- a/eric6/Preferences/ConfigurationPages/EditorFilePage.ui	Wed Oct 23 19:48:42 2019 +0200
+++ b/eric6/Preferences/ConfigurationPages/EditorFilePage.ui	Wed Oct 23 19:57:12 2019 +0200
@@ -523,8 +523,8 @@
           </layout>
          </item>
          <item>
-          <layout class="QHBoxLayout" name="horizontalLayout_5">
-           <item>
+          <layout class="QGridLayout" name="gridLayout_5">
+           <item row="0" column="0">
             <widget class="QCheckBox" name="previewMarkdownNLtoBreakCheckBox">
              <property name="toolTip">
               <string>Select this to convert a new line character to an HTML &amp;lt;br/&amp;gt; tag.</string>
@@ -534,7 +534,7 @@
              </property>
             </widget>
            </item>
-           <item>
+           <item row="0" column="1">
             <widget class="QCheckBox" name="previewMarkdownPyMdownCheckBox">
              <property name="toolTip">
               <string>Select to enable the use of the PyMdown extensions</string>
@@ -548,6 +548,16 @@
              </property>
             </widget>
            </item>
+           <item row="1" column="0">
+            <widget class="QCheckBox" name="previewMarkdownMathJaxCheckBox">
+             <property name="toolTip">
+              <string>Select to enable Math support using MathJax</string>
+             </property>
+             <property name="text">
+              <string>Enable MathJax support</string>
+             </property>
+            </widget>
+           </item>
           </layout>
          </item>
         </layout>
@@ -683,6 +693,7 @@
   <tabstop>previewMarkdownHTMLFormatComboBox</tabstop>
   <tabstop>previewMarkdownNLtoBreakCheckBox</tabstop>
   <tabstop>previewMarkdownPyMdownCheckBox</tabstop>
+  <tabstop>previewMarkdownMathJaxCheckBox</tabstop>
   <tabstop>previewRestExtensionsEdit</tabstop>
   <tabstop>previewRestSphinxCheckBox</tabstop>
   <tabstop>previewRestDocutilsHTMLFormatComboBox</tabstop>
--- a/eric6/Preferences/__init__.py	Wed Oct 23 19:48:42 2019 +0200
+++ b/eric6/Preferences/__init__.py	Wed Oct 23 19:57:12 2019 +0200
@@ -458,6 +458,8 @@
         "SpellCheckingDefaultLanguage": "en_US",
         "SpellCheckingPersonalWordList": "",
         "SpellCheckingPersonalExcludeList": "",
+        "FullSpellCheckExtensions": ["md", "markdown", "rst", "txt"],
+        "FullSpellCheckUnknown": False,
         
         "DefaultEncoding": "utf-8",
         "DefaultOpenFilter": QCoreApplication.translate(
@@ -476,6 +478,7 @@
         "PreviewRestUseSphinx": False,
         "PreviewMarkdownNLtoBR": True,
         "PreviewMarkdownUsePyMdownExtensions": True,
+        "PreviewMarkdownMathJax": True,
         "PreviewMarkdownHTMLFormat": "HTML5",  # XHTML1, HTML4, HTML5
         "PreviewRestDocutilsHTMLFormat": "HTML5",   # HTML4, HTML5
         
@@ -2043,7 +2046,8 @@
                  "PreviewMarkdownFileNameExtensions",
                  "PreviewRestFileNameExtensions",
                  "PreviewHtmlFileNameExtensions",
-                 "PreviewQssFileNameExtensions"]:
+                 "PreviewQssFileNameExtensions",
+                 "FullSpellCheckExtensions"]:
         return toList(prefClass.settings.value(
             "Editor/" + key, prefClass.editorDefaults[key]))
     elif key in ["PythonBadIndentation"]:
--- a/eric6/UI/Previewers/PreviewerHTML.py	Wed Oct 23 19:48:42 2019 +0200
+++ b/eric6/UI/Previewers/PreviewerHTML.py	Wed Oct 23 19:57:12 2019 +0200
@@ -678,13 +678,6 @@
                 """<a href="http://pythonhosted.org/Markdown/install.html">"""
                 """installation instructions.</a></p>""")
         
-        try:
-            import mdx_mathjax  # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
-        except ImportError:
-            # mathjax doesn't require import statement if installed
-            # as extension
-            pass
-        
         extensions = []
         if usePyMdownExtensions:
             try:
@@ -781,14 +774,45 @@
                     _TildeExtension(), _CaretExtension(), _MarkExtension()
                 ])
 
-        try:
-            return markdown.markdown(
-                text, extensions=extensions + ['mdx_mathjax'],
-                output_format=htmlFormat.lower())
-        except (ImportError, ValueError):
-            # markdown raises ValueError or ImportError, depends on version
-            # It is not clear, how to distinguish missing mathjax from other
-            # errors. So keep going without mathjax.
-            return markdown.markdown(
-                text, extensions=extensions,
-                output_format=htmlFormat.lower())
+        if Preferences.getEditor("PreviewMarkdownMathJax"):
+            mathjax = (
+                "<script type='text/javascript' id='MathJax-script' async"
+                " src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/"
+                "tex-chtml.js'>\n"
+                "</script>\n"
+            )
+        else:
+            mathjax = ""
+        
+        htmlFormat = Preferences.getEditor("PreviewMarkdownHTMLFormat").lower()
+        body = markdown.markdown(text, extensions=extensions,
+                                 output_format=htmlFormat.lower())
+        
+        if htmlFormat == "xhtml1":
+            head = (
+                '''<!DOCTYPE html PUBLIC "-//W3C//DTD'''
+                ''' XHTML 1.0 Transitional//EN"\n'''
+                ''' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional'''
+                '''.dtd">\n'''
+                '''<html xmlns="http://www.w3.org/1999/xhtml">\n'''
+            )
+        elif htmlFormat == "html5":
+            head = (
+                '''<!DOCTYPE html>\n'''
+                '''<html lang="EN">\n'''
+            )
+        else:
+            head = '<html lang="EN">\n'
+        head += '''<head>\n'''
+        head += (
+            '''<meta name="Generator" content="eric6" />\n'''
+            '''<meta http-equiv="Content-Type" '''
+            '''content="text/html; charset=utf-8" />\n'''
+            '''{0}'''
+            '''</head>\n'''
+            '''<body>\n'''
+        ).format(mathjax)
+        
+        foot = '''\n</body>\n</html>\n'''
+        
+        return head + body + foot

eric ide

mercurial