15 import sys |
15 import sys |
16 import io |
16 import io |
17 |
17 |
18 from PyQt5.QtCore import Qt |
18 from PyQt5.QtCore import Qt |
19 from PyQt5.QtGui import QCursor, QFontInfo |
19 from PyQt5.QtGui import QCursor, QFontInfo |
20 from PyQt5.QtWidgets import QApplication |
20 from PyQt5.QtWidgets import QApplication, QInputDialog |
21 from PyQt5.Qsci import QsciScintilla |
21 from PyQt5.Qsci import QsciScintilla |
22 |
22 |
23 from E5Gui import E5MessageBox |
23 from E5Gui import E5MessageBox |
24 |
24 |
25 from .ExporterBase import ExporterBase |
25 from .ExporterBase import ExporterBase |
406 extension in Preferences.getEditor( |
406 extension in Preferences.getEditor( |
407 "PreviewMarkdownFileNameExtensions") or |
407 "PreviewMarkdownFileNameExtensions") or |
408 self.editor.getLanguage().lower() == "markdown" |
408 self.editor.getLanguage().lower() == "markdown" |
409 ): |
409 ): |
410 # export markdown to HTML |
410 # export markdown to HTML |
411 html = self.__generateFromMarkdown() |
411 colorSchemes = [ |
|
412 self.tr("Light Background Color"), |
|
413 self.tr("Dark Background Color"), |
|
414 ] |
|
415 QApplication.restoreOverrideCursor() |
|
416 colorScheme, ok = QInputDialog.getItem( |
|
417 None, |
|
418 self.tr("Markdown Export"), |
|
419 self.tr("Select color scheme:"), |
|
420 colorSchemes, |
|
421 0, False) |
|
422 if ok: |
|
423 colorSchemeIndex = colorSchemes.index(colorScheme) |
|
424 else: |
|
425 # light background as default |
|
426 colorSchemeIndex = 0 |
|
427 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) |
|
428 QApplication.processEvents() |
|
429 html = self.__generateFromMarkdown(colorSchemeIndex == 1) |
412 elif ( |
430 elif ( |
413 extension in Preferences.getEditor( |
431 extension in Preferences.getEditor( |
414 "PreviewRestFileNameExtensions") or |
432 "PreviewRestFileNameExtensions") or |
415 self.editor.getLanguage().lower() == "restructuredtext" |
433 self.editor.getLanguage().lower() == "restructuredtext" |
416 ): |
434 ): |
501 html = docutils.core.publish_string( |
519 html = docutils.core.publish_string( |
502 self.editor.text(), writer_name=htmlFormat).decode("utf-8") |
520 self.editor.text(), writer_name=htmlFormat).decode("utf-8") |
503 sys.stderr = origStderr |
521 sys.stderr = origStderr |
504 return html |
522 return html |
505 |
523 |
506 def __generateFromMarkdown(self): |
524 def __generateFromMarkdown(self, useDarkScheme): |
507 """ |
525 """ |
508 Private method to convert Markdown text into HTML. |
526 Private method to convert Markdown text into HTML. |
509 |
527 |
|
528 @param useDarkScheme flag indicating to export using a dark color |
|
529 scheme |
|
530 @type bool |
510 @return processed HTML |
531 @return processed HTML |
511 @rtype str |
532 @rtype str |
512 """ |
533 """ |
513 try: |
534 try: |
514 import markdown # __IGNORE_EXCEPTION__ |
535 import markdown # __IGNORE_EXCEPTION__ |
532 |
553 |
533 text = self.editor.text() |
554 text = self.editor.text() |
534 |
555 |
535 mermaidNeeded = False |
556 mermaidNeeded = False |
536 if Preferences.getEditor("PreviewMarkdownMermaid"): |
557 if Preferences.getEditor("PreviewMarkdownMermaid"): |
537 if MarkdownExtensions.MermaidRegex.search(text): |
558 if MarkdownExtensions.MermaidRegexFullText.search(text): |
538 extensions.append(MarkdownExtensions.MermaidExtension()) |
559 extensions.append(MarkdownExtensions.MermaidExtension()) |
539 mermaidNeeded = True |
560 mermaidNeeded = True |
540 |
561 |
541 if Preferences.getEditor("PreviewMarkdownNLtoBR"): |
562 if Preferences.getEditor("PreviewMarkdownNLtoBR"): |
542 extensions.append('nl2br') |
563 extensions.append('nl2br') |
590 mermaid = ( |
611 mermaid = ( |
591 "<script type='text/javascript' id='Mermaid-script'" |
612 "<script type='text/javascript' id='Mermaid-script'" |
592 " src='https://unpkg.com/mermaid@8/dist/mermaid.min.js'>\n" |
613 " src='https://unpkg.com/mermaid@8/dist/mermaid.min.js'>\n" |
593 "</script>\n" |
614 "</script>\n" |
594 ) |
615 ) |
|
616 if useDarkScheme: |
|
617 mermaid_initialize = ( |
|
618 "<script>mermaid.initialize({" |
|
619 "theme: 'dark', " |
|
620 "startOnLoad:true" |
|
621 "});</script>" |
|
622 ) |
|
623 else: |
|
624 mermaid_initialize = ( |
|
625 "<script>mermaid.initialize({" |
|
626 "theme: 'default', " |
|
627 "startOnLoad:true" |
|
628 "});</script>" |
|
629 ) |
595 else: |
630 else: |
596 mermaid = "" |
631 mermaid = "" |
|
632 mermaid_initialize = "" |
597 |
633 |
598 htmlFormat = Preferences.getEditor("PreviewMarkdownHTMLFormat").lower() |
634 htmlFormat = Preferences.getEditor("PreviewMarkdownHTMLFormat").lower() |
599 body = markdown.markdown(text, extensions=extensions, |
635 body = markdown.markdown(text, extensions=extensions, |
600 output_format=htmlFormat) |
636 output_format=htmlFormat) |
|
637 if useDarkScheme: |
|
638 style = ( |
|
639 PreviewerHTMLStyles.css_markdown_dark + |
|
640 PreviewerHTMLStyles.css_pygments_dark |
|
641 ) |
|
642 else: |
|
643 style = ( |
|
644 PreviewerHTMLStyles.css_markdown_light + |
|
645 PreviewerHTMLStyles.css_pygments_light |
|
646 ) |
601 |
647 |
602 if htmlFormat == "xhtml1": |
648 if htmlFormat == "xhtml1": |
603 head = ( |
649 head = ( |
604 '''<!DOCTYPE html PUBLIC "-//W3C//DTD''' |
650 '''<!DOCTYPE html PUBLIC "-//W3C//DTD''' |
605 ''' XHTML 1.0 Transitional//EN"\n''' |
651 ''' XHTML 1.0 Transitional//EN"\n''' |
630 '''<style type="text/css">''' |
676 '''<style type="text/css">''' |
631 '''{2}''' |
677 '''{2}''' |
632 '''</style>\n''' |
678 '''</style>\n''' |
633 '''</head>\n''' |
679 '''</head>\n''' |
634 '''<body>\n''' |
680 '''<body>\n''' |
635 ).format( |
681 ).format(mathjax, mermaid, style) |
636 mathjax, mermaid, |
|
637 PreviewerHTMLStyles.css_markdown + |
|
638 PreviewerHTMLStyles.css_pygments |
|
639 ) |
|
640 |
682 |
641 foot = '''\n</body>\n</html>\n''' |
683 foot = '''\n</body>\n</html>\n''' |
642 |
684 |
643 return head + body + foot |
685 return head + body + mermaid_initialize + foot |