eric6/QScintilla/Exporters/ExporterHTML.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8043
0acf98cd089a
parent 8259
2bbec88047dd
--- a/eric6/QScintilla/Exporters/ExporterHTML.py	Fri Apr 02 11:59:41 2021 +0200
+++ b/eric6/QScintilla/Exporters/ExporterHTML.py	Sat May 01 14:27:20 2021 +0200
@@ -13,6 +13,7 @@
 import os
 import sys
 import io
+import contextlib
 
 from PyQt5.QtGui import QFontInfo
 from PyQt5.QtWidgets import QInputDialog
@@ -27,7 +28,7 @@
 import Utilities
 
 
-class HTMLGenerator(object):
+class HTMLGenerator:
     """
     Class implementing an HTML generator for exporting source code.
     """
@@ -121,10 +122,11 @@
             )
         
         lex = self.editor.getLexer()
-        if lex:
-            bgColour = lex.paper(QsciScintilla.STYLE_DEFAULT).name()
-        else:
-            bgColour = self.editor.paper().name()
+        bgColour = (
+            lex.paper(QsciScintilla.STYLE_DEFAULT).name()
+            if lex else
+            self.editor.paper().name()
+        )
         
         html += '''<style type="text/css">\n'''
         if lex:
@@ -188,6 +190,7 @@
                                 html += '    font-size: {0:d}pt;\n'.format(
                                         QFontInfo(font).pointSize())
                             html += '}\n'
+                            # __IGNORE_WARNING_Y113__
                     else:
                         styleIsUsed[istyle] = False
                 istyle += 1
@@ -415,10 +418,7 @@
             return
         
         fn = self.editor.getFileName()
-        if fn:
-            extension = os.path.normcase(os.path.splitext(fn)[1][1:])
-        else:
-            extension = ""
+        extension = os.path.normcase(os.path.splitext(fn)[1][1:]) if fn else ""
         
         if (
             extension in Preferences.getEditor(
@@ -475,18 +475,18 @@
                 )
         
         if html:
-            try:
-                with E5OverrideCursor():
-                    with open(filename, "w", encoding="utf-8") as f:
-                        f.write(html)
-            except OSError as err:
-                E5MessageBox.critical(
-                    self.editor,
-                    self.tr("Export source"),
-                    self.tr(
-                        """<p>The source could not be exported to"""
-                        """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
-                    .format(filename, str(err)))
+            with E5OverrideCursor(), open(filename, "w", encoding="utf-8"
+                                          ) as f:
+                try:
+                    f.write(html)
+                except OSError as err:
+                    E5MessageBox.critical(
+                        self.editor,
+                        self.tr("Export source"),
+                        self.tr(
+                            """<p>The source could not be exported to"""
+                            """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
+                        .format(filename, str(err)))
         else:
             E5MessageBox.critical(
                 self.editor,
@@ -568,17 +568,19 @@
         text = self.editor.text()
         
         mermaidNeeded = False
-        if Preferences.getEditor("PreviewMarkdownMermaid"):
-            if MarkdownExtensions.MermaidRegexFullText.search(text):
-                extensions.append(MarkdownExtensions.MermaidExtension())
-                mermaidNeeded = True
+        if (
+            Preferences.getEditor("PreviewMarkdownMermaid") and
+            MarkdownExtensions.MermaidRegexFullText.search(text)
+        ):
+            extensions.append(MarkdownExtensions.MermaidExtension())
+            mermaidNeeded = True
         
         if Preferences.getEditor("PreviewMarkdownNLtoBR"):
             extensions.append('nl2br')
         
         pyMdown = False
         if Preferences.getEditor("PreviewMarkdownUsePyMdownExtensions"):
-            try:
+            with contextlib.suppress(ImportError):
                 import pymdownx     # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
                 # PyPI package is 'pymdown-extensions'
                 
@@ -589,8 +591,6 @@
                     'pymdownx.tasklist', 'pymdownx.smartsymbols',
                 ])
                 pyMdown = True
-            except ImportError:
-                pass
         
         if not pyMdown:
             extensions.extend(['extra', 'toc'])
@@ -648,16 +648,13 @@
         htmlFormat = Preferences.getEditor("PreviewMarkdownHTMLFormat").lower()
         body = markdown.markdown(text, extensions=extensions,
                                  output_format=htmlFormat)
-        if useDarkScheme:
-            style = (
-                PreviewerHTMLStyles.css_markdown_dark +
-                PreviewerHTMLStyles.css_pygments_dark
-            )
-        else:
-            style = (
-                PreviewerHTMLStyles.css_markdown_light +
-                PreviewerHTMLStyles.css_pygments_light
-            )
+        style = (
+            (PreviewerHTMLStyles.css_markdown_dark +
+             PreviewerHTMLStyles.css_pygments_dark)
+            if useDarkScheme else
+            (PreviewerHTMLStyles.css_markdown_light +
+             PreviewerHTMLStyles.css_pygments_light)
+        )
         
         if htmlFormat == "xhtml1":
             head = (

eric ide

mercurial