eric6/QScintilla/Exporters/ExporterHTML.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8043
0acf98cd089a
parent 8259
2bbec88047dd
equal deleted inserted replaced
8190:fb0ef164f536 8273:698ae46f40a4
11 # Original code: Copyright 1998-2006 by Neil Hodgson <neilh@scintilla.org> 11 # Original code: Copyright 1998-2006 by Neil Hodgson <neilh@scintilla.org>
12 12
13 import os 13 import os
14 import sys 14 import sys
15 import io 15 import io
16 import contextlib
16 17
17 from PyQt5.QtGui import QFontInfo 18 from PyQt5.QtGui import QFontInfo
18 from PyQt5.QtWidgets import QInputDialog 19 from PyQt5.QtWidgets import QInputDialog
19 from PyQt5.Qsci import QsciScintilla 20 from PyQt5.Qsci import QsciScintilla
20 21
25 26
26 import Preferences 27 import Preferences
27 import Utilities 28 import Utilities
28 29
29 30
30 class HTMLGenerator(object): 31 class HTMLGenerator:
31 """ 32 """
32 Class implementing an HTML generator for exporting source code. 33 Class implementing an HTML generator for exporting source code.
33 """ 34 """
34 def __init__(self, editor): 35 def __init__(self, editor):
35 """ 36 """
119 '''//-->\n''' 120 '''//-->\n'''
120 '''</script>\n''' 121 '''</script>\n'''
121 ) 122 )
122 123
123 lex = self.editor.getLexer() 124 lex = self.editor.getLexer()
124 if lex: 125 bgColour = (
125 bgColour = lex.paper(QsciScintilla.STYLE_DEFAULT).name() 126 lex.paper(QsciScintilla.STYLE_DEFAULT).name()
126 else: 127 if lex else
127 bgColour = self.editor.paper().name() 128 self.editor.paper().name()
129 )
128 130
129 html += '''<style type="text/css">\n''' 131 html += '''<style type="text/css">\n'''
130 if lex: 132 if lex:
131 istyle = 0 133 istyle = 0
132 while istyle <= QsciScintilla.STYLE_MAX: 134 while istyle <= QsciScintilla.STYLE_MAX:
186 html += ' color: {0};\n'.format(colour.name()) 188 html += ' color: {0};\n'.format(colour.name())
187 if wysiwyg: 189 if wysiwyg:
188 html += ' font-size: {0:d}pt;\n'.format( 190 html += ' font-size: {0:d}pt;\n'.format(
189 QFontInfo(font).pointSize()) 191 QFontInfo(font).pointSize())
190 html += '}\n' 192 html += '}\n'
193 # __IGNORE_WARNING_Y113__
191 else: 194 else:
192 styleIsUsed[istyle] = False 195 styleIsUsed[istyle] = False
193 istyle += 1 196 istyle += 1
194 else: 197 else:
195 colour = self.editor.color() 198 colour = self.editor.color()
413 filename = self._getFileName(self.tr("HTML Files (*.html)")) 416 filename = self._getFileName(self.tr("HTML Files (*.html)"))
414 if not filename: 417 if not filename:
415 return 418 return
416 419
417 fn = self.editor.getFileName() 420 fn = self.editor.getFileName()
418 if fn: 421 extension = os.path.normcase(os.path.splitext(fn)[1][1:]) if fn else ""
419 extension = os.path.normcase(os.path.splitext(fn)[1][1:])
420 else:
421 extension = ""
422 422
423 if ( 423 if (
424 extension in Preferences.getEditor( 424 extension in Preferences.getEditor(
425 "PreviewMarkdownFileNameExtensions") or 425 "PreviewMarkdownFileNameExtensions") or
426 self.editor.getLanguage().lower() == "markdown" 426 self.editor.getLanguage().lower() == "markdown"
473 onlyStylesUsed=onlyStylesUsed, 473 onlyStylesUsed=onlyStylesUsed,
474 titleFullPath=titleFullPath 474 titleFullPath=titleFullPath
475 ) 475 )
476 476
477 if html: 477 if html:
478 try: 478 with E5OverrideCursor(), open(filename, "w", encoding="utf-8"
479 with E5OverrideCursor(): 479 ) as f:
480 with open(filename, "w", encoding="utf-8") as f: 480 try:
481 f.write(html) 481 f.write(html)
482 except OSError as err: 482 except OSError as err:
483 E5MessageBox.critical( 483 E5MessageBox.critical(
484 self.editor, 484 self.editor,
485 self.tr("Export source"), 485 self.tr("Export source"),
486 self.tr( 486 self.tr(
487 """<p>The source could not be exported to""" 487 """<p>The source could not be exported to"""
488 """ <b>{0}</b>.</p><p>Reason: {1}</p>""") 488 """ <b>{0}</b>.</p><p>Reason: {1}</p>""")
489 .format(filename, str(err))) 489 .format(filename, str(err)))
490 else: 490 else:
491 E5MessageBox.critical( 491 E5MessageBox.critical(
492 self.editor, 492 self.editor,
493 self.tr("Export source"), 493 self.tr("Export source"),
494 self.tr( 494 self.tr(
566 extensions = [] 566 extensions = []
567 567
568 text = self.editor.text() 568 text = self.editor.text()
569 569
570 mermaidNeeded = False 570 mermaidNeeded = False
571 if Preferences.getEditor("PreviewMarkdownMermaid"): 571 if (
572 if MarkdownExtensions.MermaidRegexFullText.search(text): 572 Preferences.getEditor("PreviewMarkdownMermaid") and
573 extensions.append(MarkdownExtensions.MermaidExtension()) 573 MarkdownExtensions.MermaidRegexFullText.search(text)
574 mermaidNeeded = True 574 ):
575 extensions.append(MarkdownExtensions.MermaidExtension())
576 mermaidNeeded = True
575 577
576 if Preferences.getEditor("PreviewMarkdownNLtoBR"): 578 if Preferences.getEditor("PreviewMarkdownNLtoBR"):
577 extensions.append('nl2br') 579 extensions.append('nl2br')
578 580
579 pyMdown = False 581 pyMdown = False
580 if Preferences.getEditor("PreviewMarkdownUsePyMdownExtensions"): 582 if Preferences.getEditor("PreviewMarkdownUsePyMdownExtensions"):
581 try: 583 with contextlib.suppress(ImportError):
582 import pymdownx # __IGNORE_EXCEPTION__ __IGNORE_WARNING__ 584 import pymdownx # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
583 # PyPI package is 'pymdown-extensions' 585 # PyPI package is 'pymdown-extensions'
584 586
585 extensions.extend([ 587 extensions.extend([
586 'toc', 588 'toc',
587 'pymdownx.extra', 'pymdownx.caret', 'pymdownx.emoji', 589 'pymdownx.extra', 'pymdownx.caret', 'pymdownx.emoji',
588 'pymdownx.mark', 'pymdownx.tilde', 'pymdownx.keys', 590 'pymdownx.mark', 'pymdownx.tilde', 'pymdownx.keys',
589 'pymdownx.tasklist', 'pymdownx.smartsymbols', 591 'pymdownx.tasklist', 'pymdownx.smartsymbols',
590 ]) 592 ])
591 pyMdown = True 593 pyMdown = True
592 except ImportError:
593 pass
594 594
595 if not pyMdown: 595 if not pyMdown:
596 extensions.extend(['extra', 'toc']) 596 extensions.extend(['extra', 'toc'])
597 597
598 # version 2.0 supports only extension names, not instances 598 # version 2.0 supports only extension names, not instances
646 mermaid_initialize = "" 646 mermaid_initialize = ""
647 647
648 htmlFormat = Preferences.getEditor("PreviewMarkdownHTMLFormat").lower() 648 htmlFormat = Preferences.getEditor("PreviewMarkdownHTMLFormat").lower()
649 body = markdown.markdown(text, extensions=extensions, 649 body = markdown.markdown(text, extensions=extensions,
650 output_format=htmlFormat) 650 output_format=htmlFormat)
651 if useDarkScheme: 651 style = (
652 style = ( 652 (PreviewerHTMLStyles.css_markdown_dark +
653 PreviewerHTMLStyles.css_markdown_dark + 653 PreviewerHTMLStyles.css_pygments_dark)
654 PreviewerHTMLStyles.css_pygments_dark 654 if useDarkScheme else
655 ) 655 (PreviewerHTMLStyles.css_markdown_light +
656 else: 656 PreviewerHTMLStyles.css_pygments_light)
657 style = ( 657 )
658 PreviewerHTMLStyles.css_markdown_light +
659 PreviewerHTMLStyles.css_pygments_light
660 )
661 658
662 if htmlFormat == "xhtml1": 659 if htmlFormat == "xhtml1":
663 head = ( 660 head = (
664 '''<!DOCTYPE html PUBLIC "-//W3C//DTD''' 661 '''<!DOCTYPE html PUBLIC "-//W3C//DTD'''
665 ''' XHTML 1.0 Transitional//EN"\n''' 662 ''' XHTML 1.0 Transitional//EN"\n'''

eric ide

mercurial