Exemplo n.º 1
0
    def __init__(self, settings, name, filename, plain_text=False, parent=None):
        super().__init__(parent=parent)
        splitty = QSplitter(self)
        self.setWidget(splitty)
        self.setWindowTitle(os.path.basename(name))
        self.setFloating(True)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.plain_text = plain_text
        self.notePath = settings.notePath

        fh = QFile(filename)
        try:
            if not fh.open(QIODevice.ReadOnly):
                raise IOError(fh.errorString())
        except IOError as e:
            QMessageBox.warning(self, self.tr("Read Error"),
                                self.tr("Failed to open %s: %s") % (filename, e))
        finally:
            if fh is not None:
                noteBody = QTextStream(fh).readAll()
                fh.close()
                self.tocw = TocTree(self)
                splitty.addWidget(self.tocw)

                strip_math_for_header_parsing = False
                strip_fence_for_header_parsing = False

                self.tocw.itemClicked.connect(self.tocNavigate)
                if 'asciimathml' in settings.extensions:
                    stuff=JSCRIPT_TPL.format(settings.mathjax)
                    strip_math_for_header_parsing = True
                else:
                    stuff=''
                if 'fenced_code' in settings.extensions or 'extra' in settings.extensions:
                    strip_fence_for_header_parsing = True
                if plain_text:
                    note_view = QPlainTextEdit(self)
                    qfnt = QFont()
                    qfnt.setFamily('monospace')
                    note_view.setFont(qfnt)
                    note_view.setPlainText(noteBody)
                else:
                    note_view = QWebView(self)
                    note_view.setHtml(settings.md.reset().convert(noteBody)+stuff)
                    note_view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
                    note_view.linkClicked.connect(self.linkClicked)
                    note_view.settings().setUserStyleSheetUrl( 
                     QUrl('file://'+self.parent().settings.cssfile))
                self.note_view = note_view
                splitty.addWidget(note_view)
                self.tocw.updateToc(os.path.basename(name), 
                    parseHeaders(noteBody, strip_fenced_block=strip_fence_for_header_parsing,
                        strip_ascii_math=strip_math_for_header_parsing))
Exemplo n.º 2
0
    def __init__(self,
                 settings,
                 name,
                 filename,
                 plain_text=False,
                 parent=None):
        super().__init__(parent=parent)
        splitty = QSplitter(self)
        self.setWidget(splitty)
        self.setWindowTitle(os.path.basename(name))
        self.setFloating(True)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.plain_text = plain_text
        self.notePath = settings.notePath

        fh = QFile(filename)
        try:
            if not fh.open(QIODevice.ReadOnly):
                raise IOError(fh.errorString())
        except IOError as e:
            QMessageBox.warning(
                self, self.tr("Read Error"),
                self.tr("Failed to open %s: %s") % (filename, e))
        finally:
            if fh is not None:
                noteBody = QTextStream(fh).readAll()
                fh.close()
                self.tocw = TocTree(self)
                splitty.addWidget(self.tocw)

                strip_math_for_header_parsing = False
                strip_fence_for_header_parsing = False

                self.tocw.itemClicked.connect(self.tocNavigate)
                if 'asciimathml' in settings.extensions:
                    stuff = JSCRIPT_TPL.format(settings.mathjax)
                    strip_math_for_header_parsing = True
                else:
                    stuff = ''
                if 'fenced_code' in settings.extensions or 'extra' in settings.extensions:
                    strip_fence_for_header_parsing = True
                if plain_text:
                    note_view = QPlainTextEdit(self)
                    qfnt = QFont()
                    qfnt.setFamily('monospace')
                    note_view.setFont(qfnt)
                    note_view.setPlainText(noteBody)
                else:
                    note_view = QWebView(self)
                    note_view.setHtml(settings.md.reset().convert(noteBody) +
                                      stuff)
                    note_view.page().setLinkDelegationPolicy(
                        QWebPage.DelegateAllLinks)
                    note_view.linkClicked.connect(self.linkClicked)
                    note_view.settings().setUserStyleSheetUrl(
                        QUrl('file://' + self.parent().settings.cssfile))
                self.note_view = note_view
                splitty.addWidget(note_view)
                self.tocw.updateToc(
                    os.path.basename(name),
                    parseHeaders(
                        noteBody,
                        strip_fenced_block=strip_fence_for_header_parsing,
                        strip_ascii_math=strip_math_for_header_parsing))