eric6/UI/Previewers/PreviewerHTML.py

changeset 7304
b072a364dd8d
parent 7301
6df711503ec0
child 7309
5a434813eef3
diff -r b30e4fd161b8 -r b072a364dd8d eric6/UI/Previewers/PreviewerHTML.py
--- 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