eric6/UI/Previewers/PreviewerHTML.py

changeset 7304
b072a364dd8d
parent 7301
6df711503ec0
child 7309
5a434813eef3
equal deleted inserted replaced
7303:b30e4fd161b8 7304:b072a364dd8d
675 """<p>Markdown preview requires the <b>Markdown</b> """ 675 """<p>Markdown preview requires the <b>Markdown</b> """
676 """package.<br/>Install it with your package manager,""" 676 """package.<br/>Install it with your package manager,"""
677 """ 'pip install Markdown' or see """ 677 """ 'pip install Markdown' or see """
678 """<a href="http://pythonhosted.org/Markdown/install.html">""" 678 """<a href="http://pythonhosted.org/Markdown/install.html">"""
679 """installation instructions.</a></p>""") 679 """installation instructions.</a></p>""")
680
681 try:
682 import mdx_mathjax # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
683 except ImportError:
684 # mathjax doesn't require import statement if installed
685 # as extension
686 pass
687 680
688 extensions = [] 681 extensions = []
689 if usePyMdownExtensions: 682 if usePyMdownExtensions:
690 try: 683 try:
691 import pymdownx # __IGNORE_EXCEPTION__ __IGNORE_WARNING__ 684 import pymdownx # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
779 772
780 extensions.extend([ 773 extensions.extend([
781 _TildeExtension(), _CaretExtension(), _MarkExtension() 774 _TildeExtension(), _CaretExtension(), _MarkExtension()
782 ]) 775 ])
783 776
784 try: 777 if Preferences.getEditor("PreviewMarkdownMathJax"):
785 return markdown.markdown( 778 mathjax = (
786 text, extensions=extensions + ['mdx_mathjax'], 779 "<script type='text/javascript' id='MathJax-script' async"
787 output_format=htmlFormat.lower()) 780 " src='https://cdn.jsdelivr.net/npm/mathjax@3/es5/"
788 except (ImportError, ValueError): 781 "tex-chtml.js'>\n"
789 # markdown raises ValueError or ImportError, depends on version 782 "</script>\n"
790 # It is not clear, how to distinguish missing mathjax from other 783 )
791 # errors. So keep going without mathjax. 784 else:
792 return markdown.markdown( 785 mathjax = ""
793 text, extensions=extensions, 786
794 output_format=htmlFormat.lower()) 787 htmlFormat = Preferences.getEditor("PreviewMarkdownHTMLFormat").lower()
788 body = markdown.markdown(text, extensions=extensions,
789 output_format=htmlFormat.lower())
790
791 if htmlFormat == "xhtml1":
792 head = (
793 '''<!DOCTYPE html PUBLIC "-//W3C//DTD'''
794 ''' XHTML 1.0 Transitional//EN"\n'''
795 ''' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional'''
796 '''.dtd">\n'''
797 '''<html xmlns="http://www.w3.org/1999/xhtml">\n'''
798 )
799 elif htmlFormat == "html5":
800 head = (
801 '''<!DOCTYPE html>\n'''
802 '''<html lang="EN">\n'''
803 )
804 else:
805 head = '<html lang="EN">\n'
806 head += '''<head>\n'''
807 head += (
808 '''<meta name="Generator" content="eric6" />\n'''
809 '''<meta http-equiv="Content-Type" '''
810 '''content="text/html; charset=utf-8" />\n'''
811 '''{0}'''
812 '''</head>\n'''
813 '''<body>\n'''
814 ).format(mathjax)
815
816 foot = '''\n</body>\n</html>\n'''
817
818 return head + body + foot

eric ide

mercurial