Beispiel #1
0
def fallback_trans(x):
    """For translating placeholder strings the user should never see
    in raw form, such as 'funny 500 message'.  If the string does not
    translate in the current language, falls back on the 'en'
    translation that we've hopefully already provided"""
    t = _(x)
    if t == x:
        l = h.get_lang()
        h.set_lang('en', graceful_fail = True)
        t = _(x)
        if l and l[0] != 'en':
            h.set_lang(l[0])
    return t
Beispiel #2
0
def fallback_trans(x):
    """For translating placeholder strings the user should never see
    in raw form, such as 'funny 500 message'.  If the string does not
    translate in the current language, falls back on the 'en'
    translation that we've hopefully already provided"""
    t = _(x)
    if t == x:
        l = h.get_lang()
        h.set_lang('en', graceful_fail = True)
        t = _(x)
        if l and l[0] != 'en':
            h.set_lang(l[0])
    return t
    def __init__(self, pathnameSrc=None):
        super(MarkdownEditor, self).__init__('Markdown Editor', 800, 400)
        tmpfile = helpers.mktemp(prefix='markdown-editor', suffix='.html')
        self.box = widgets.Box(QBoxLayout.TopToBottom, self)
        self.subBox = widgets.Box(QBoxLayout.LeftToRight, self.box)
        self.textFileChooser = dialogs.TextFileChooser(self)
        if pathnameSrc:
            self.pathnameSrc = helpers.Path(pathnameSrc).absolute()
            self.documentTitle = self.pathnameSrc.name
        else:
            self.pathnameSrc = None
            self.documentTitle = MarkdownEditor.documentTitleDefault

        self.labelDocumentState = QLabel(self.box)
        self.box.addWidget(self.labelDocumentState)

        self.addAction(
            'file-document-new',
            object.Action('New document', self.newDocument, 'Ctrl+N',
                          'document-new'))
        self.addAction(
            'file-document-open',
            object.Action('Open document', self.triggeredOpenDocument,
                          'Ctrl+O', 'document-open'))
        self.addAction(
            'file-document-save',
            object.Action('Save document', self.triggeredSaveDocument,
                          'Ctrl+S', 'document-save'))
        self.addAction(
            'file-document-save-as',
            object.Action('Save document as', self.triggeredSaveAsDocument,
                          'Ctrl+Shift+S', 'document-save-as'))
        self.addAction('file-export-html',
                       object.Action('Export in html', self.triggeredExport))
        self.addAction(
            'edit-undo',
            object.Action('Undo', self.triggeredUndo, 'Ctrl+Z', 'edit-undo'))
        self.addAction(
            'edit-redo',
            object.Action('Redo', self.triggeredRedo, 'Ctrl+Y', 'edit-redo'))
        self.addAction(
            'edit-cut',
            object.Action('Cut', self.triggeredCut, 'Ctrl+X', 'edit-cut'))
        self.addAction(
            'edit-copy',
            object.Action('Copy', self.triggeredCopy, 'Ctrl+C', 'edit-copy'))
        self.addAction(
            'edit-paste',
            object.Action('Paste', self.triggeredPaste, 'Ctrl+V',
                          'edit-paste'))
        self.addAction(
            'edit-preference',
            object.Action('Preferences', self.triggeredPreference, 'Ctrl+,',
                          'preferences-system'))
        self.addAction('view-refresh',
                       object.Action('Refresh Preview', self.triggeredPreview))
        self.addAction('view-about', object.Action('About',
                                                   self.triggeredAbout))

        with helpers.joinpath_to_cwd('requirements.txt').open(
                encoding='utf8') as requirements:
            self.aboutDialog = dialogs.About(
                self,
                copyright='Loïc Penaud ©',
                programName='Markdown-Editor',
                version='1.2',
                website='https://github.com/lpenaud/markdown-editor-qt',
                websiteLabel='Github',
                comments=
                'A markdown editor written in Python3.7 with Qt5 and pandoc2',
                licenseName='GPL-3.0',
                licenseUrl=helpers.joinpath_to_cwd('LICENSE').as_uri(),
                authors=('Loïc Penaud', ),
                dependencies=[l.strip() for l in requirements.readlines()],
            )

        self.pandoc = Pandoc(
            'markdown',
            'html5',
            template=str(helpers.joinpath_to_cwd('template', 'default.html')),
            lang=helpers.get_lang(),
            inline_css=helpers.joinpath_to_cwd('template',
                                               'default.css').read_text(),
            toc=True,
            toc_title=True)
        self.thread = threads.PandocThread(self, tmpfile)

        self.saveThread = threads.SaveThread(self)
        self.saveThreading = True

        self.preferenceDialog = dialogs.Preferences(self)
        self.preferenceDialog.themeChooser.themeChanged.connect(
            self.triggeredThemeChanged)
        self.preferenceDialog.saveOption.saveOptionChanged.connect(
            self.triggeredSaveOptionChanged)

        self.menubar = widgets.MenuBar(self)
        self.menubar.appendMenu('File')
        self.menubar.addActionsToMenu('File', self.findActionsLike('file'))
        self.menubar.insertSeparatorToMenu('File',
                                           self.actions['file-document-save'])
        self.menubar.insertSeparatorToMenu('File',
                                           self.actions['file-export-html'])
        self.menubar.appendMenu('Edit')
        self.menubar.addActionsToMenu('Edit', self.findActionsLike('edit'))
        self.menubar.insertSeparatorToMenu('Edit', self.actions['edit-cut'])
        self.menubar.insertSeparatorToMenu('Edit',
                                           self.actions['edit-preference'])
        self.menubar.appendMenu('View')
        self.menubar.addActionToMenu('View', self.actions['view-refresh'])
        self.setMenuBar(self.menubar)

        self.toolbar = QToolBar(self)
        self.toolbar.addActions(self.actions.values())
        self.toolbar.insertSeparator(self.actions['file-export-html'])
        self.toolbar.insertSeparator(self.actions['edit-cut'])
        self.toolbar.insertSeparator(self.actions['edit-undo'])
        self.toolbar.insertSeparator(self.actions['view-refresh'])
        self.toolbar.removeAction(self.actions['edit-preference'])
        self.addToolBar(self.toolbar)

        self.textEditor = widgets.TextEditor(self.subBox)
        self.textEditor.setDocumentTitle('New document')
        self.textEditor.timeout.connect(self.triggeredTextTimeout)
        self.textEditor.textChanged.connect(self.triggeredTextChanged)
        if self.pathnameSrc:
            self.textEditor.textContent = self.pathnameSrc.read_text(
                encoding='utf8')
        else:
            self.newDocument()

        self.webview = widgets.WebView(self.subBox)
        self.triggeredPreview()
        self.webview.url = tmpfile.as_uri()

        self.subBox.addWidget(self.textEditor)
        self.subBox.addWidget(self.webview)

        self.box.addWidget(self.subBox)
        self.setCentralWidget(self.box)
        self.documentIsSave = True
        self.documentIsSaveSig.connect(self.documentIsSaveSigCb)