UI/Previewers/PreviewerHTML.py

changeset 5837
9ef6a28f1694
parent 5801
d3548bec88d1
child 5845
f3d2172d663e
--- a/UI/Previewers/PreviewerHTML.py	Mon Aug 07 12:19:34 2017 +0200
+++ b/UI/Previewers/PreviewerHTML.py	Mon Aug 07 18:00:51 2017 +0200
@@ -10,7 +10,6 @@
 from __future__ import unicode_literals
 
 try:  # Only for Py2
-    basestring
     import StringIO as io   # __IGNORE_EXCEPTION__
 except (ImportError, NameError):
     import io       # __IGNORE_WARNING__
@@ -192,7 +191,10 @@
                 self.__processingThread.process(
                     fn, language, editor.text(),
                     self.ssiCheckBox.isChecked(), rootPath,
-                    Preferences.getEditor("PreviewRestUseSphinx"))
+                    Preferences.getEditor("PreviewRestUseSphinx"),
+                    Preferences.getEditor("PreviewMarkdownNLtoBR"),
+                    Preferences.getEditor("PreviewMarkdownHTMLFormat"),
+                    Preferences.getEditor("PreviewRestDocutilsHTMLFormat"))
 
     def __setHtml(self, filePath, html):
         """
@@ -357,7 +359,8 @@
         self.__lock = threading.Lock()
     
     def process(self, filePath, language, text, ssiEnabled, rootPath,
-                useSphinx):
+                useSphinx, convertNewLineToBreak, markdownHtmlFormat,
+                restDocutilsHtmlFormat):
         """
         Public method to convert the given text to HTML.
         
@@ -369,6 +372,12 @@
         @param rootPath root path to be used for SSI processing (str)
         @param useSphinx flag indicating to use Sphinx to generate the
             ReST preview (boolean)
+        @param convertNewLineToBreak flag indicating to convert new lines
+            to HTML break (Markdown only) (boolean)
+        @param markdownHtmlFormat HTML format to be generated by markdown
+            (string)
+        @param restDocutilsHtmlFormat HTML format to be generated by docutils
+            (string)
         """
         with self.__lock:
             self.__filePath = filePath
@@ -378,6 +387,9 @@
             self.__rootPath = rootPath
             self.__haveData = True
             self.__useSphinx = useSphinx
+            self.__convertNewLineToBreak = convertNewLineToBreak
+            self.__markdownHtmlFormat = markdownHtmlFormat
+            self.__restDocutilsHtmlFormat = restDocutilsHtmlFormat
             if not self.isRunning():
                 self.start(QThread.LowPriority)
     
@@ -394,10 +406,15 @@
                 ssiEnabled = self.__ssiEnabled
                 rootPath = self.__rootPath
                 useSphinx = self.__useSphinx
-                self.__haveData = False
+                convertNewLineToBreak = self.__convertNewLineToBreak
+                markdownHtmlFormat = self.__markdownHtmlFormat
+                restDocutilsHtmlFormat = self.__restDocutilsHtmlFormat
             
+                self.__haveData = False
+
             html = self.__getHtml(language, text, ssiEnabled, filePath,
-                                  rootPath, useSphinx)
+                                  rootPath, useSphinx, convertNewLineToBreak,
+                                  markdownHtmlFormat, restDocutilsHtmlFormat)
             
             with self.__lock:
                 if not self.__haveData:
@@ -406,7 +423,8 @@
                 # else - next iteration
     
     def __getHtml(self, language, text, ssiEnabled, filePath, rootPath,
-                  useSphinx):
+                  useSphinx, convertNewLineToBreak, markdownHtmlFormat,
+                  restDocutilsHtmlFormat):
         """
         Private method to process the given text depending upon the given
         language.
@@ -419,6 +437,12 @@
         @param rootPath root path to be used for SSI processing (str)
         @param useSphinx flag indicating to use Sphinx to generate the
             ReST preview (boolean)
+        @param convertNewLineToBreak flag indicating to convert new lines
+            to HTML break (Markdown only) (boolean)
+        @param markdownHtmlFormat HTML format to be generated by markdown
+            (string)
+        @param restDocutilsHtmlFormat HTML format to be generated by docutils
+            (string)
         @return processed HTML text (string)
         """
         if language == "HTML":
@@ -427,9 +451,10 @@
             else:
                 return text
         elif language == "Markdown":
-            return self.__convertMarkdown(text)
+            return self.__convertMarkdown(text, convertNewLineToBreak,
+                                          markdownHtmlFormat)
         elif language == "ReST":
-            return self.__convertReST(text, useSphinx)
+            return self.__convertReST(text, useSphinx, restDocutilsHtmlFormat)
         else:
             return self.tr(
                 "<p>No preview available for this type of file.</p>")
@@ -481,19 +506,21 @@
         
         return txt
     
-    def __convertReST(self, text, useSphinx):
+    def __convertReST(self, text, useSphinx, restDocutilsHtmlFormat):
         """
         Private method to convert ReST text into HTML.
         
         @param text text to be processed (string)
         @param useSphinx flag indicating to use Sphinx to generate the
             ReST preview (boolean)
+        @param restDocutilsHtmlFormat HTML format to be generated by docutils
+            (string)
         @return processed HTML (string)
         """
         if useSphinx:
             return self.__convertReSTSphinx(text)
         else:
-            return self.__convertReSTDocutils(text)
+            return self.__convertReSTDocutils(text, restDocutilsHtmlFormat)
     
     def __convertReSTSphinx(self, text):
         """
@@ -543,11 +570,12 @@
         
         return html
     
-    def __convertReSTDocutils(self, text):
+    def __convertReSTDocutils(self, text, htmlFormat):
         """
         Private method to convert ReST text into HTML using 'docutils'.
         
         @param text text to be processed (string)
+        @param htmlFormat HTML format to be generated (string)
         @return processed HTML (string)
         """
         if 'sphinx' in sys.modules:
@@ -570,16 +598,19 @@
         # redirect sys.stderr because we are not interested in it here
         origStderr = sys.stderr
         sys.stderr = io.StringIO()
-        html = docutils.core.publish_string(text, writer_name='html')\
-            .decode("utf-8")
+        html = docutils.core.publish_string(
+            text, writer_name=htmlFormat.lower()).decode("utf-8")
         sys.stderr = origStderr
         return html
     
-    def __convertMarkdown(self, text):
+    def __convertMarkdown(self, text, convertNewLineToBreak, htmlFormat):
         """
         Private method to convert Markdown text into HTML.
         
         @param text text to be processed (string)
+        @param convertNewLineToBreak flag indicating to convert new lines
+            to HTML break (Markdown only) (boolean)
+        @param htmlFormat HTML format to be generated by markdown (string)
         @return processed HTML (string)
         """
         try:
@@ -597,8 +628,11 @@
         except ImportError:
             #mathjax doesn't require import statement if installed as extension
             pass
-
-        extensions = ['fenced_code', 'nl2br', 'extra']
+        
+        if convertNewLineToBreak:
+            extensions = ['fenced_code', 'nl2br', 'extra']
+        else:
+            extensions = ['fenced_code', 'extra']
         
         # version 2.0 supports only extension names, not instances
         if markdown.version_info[0] > 2 or \
@@ -625,9 +659,11 @@
             extensions.append(_StrikeThroughExtension())
 
         try:
-            return markdown.markdown(text, extensions + ['mathjax'])
+            return markdown.markdown(text, extensions=extensions + ['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)
+            return markdown.markdown(text, extensions=extensions,
+                                     output_format=htmlFormat.lower())

eric ide

mercurial