Mon, 02 Jan 2017 12:49:28 +0100
Continued implementing a format button bar and provider classes for various markup languages.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QScintilla/MarkupProviders/HtmlProvider.py Mon Jan 02 12:49:28 2017 +0100 @@ -0,0 +1,103 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing the HTML markup provider. +""" + +from __future__ import unicode_literals + +from .MarkupBase import MarkupBase + + +class HtmlProvider(MarkupBase): + """ + Class implementing the HTML markup provider. + """ + def __init__(self): + """ + Constructor + """ + super(HtmlProvider, self).__init__() + + def hasBold(self): + """ + Public method to indicate the availability of bold markup. + + @return flag indicating the availability of bold markup + @rtype bool + """ + return True + + def hasItalic(self): + """ + Public method to indicate the availability of italic markup. + + @return flag indicating the availability of italic markup + @rtype bool + """ + return True + + def hasStrikethrough(self): + """ + Public method to indicate the availability of strikethrough markup. + + @return flag indicating the availability of strikethrough markup + @rtype bool + """ + return True + + def bold(self, editor): + """ + Public method to generate bold text. + + @param editor reference to the editor to work on + @type Editor + """ + self.__insertMarkup("b", editor) + + def italic(self, editor): + """ + Public method to generate italic text. + + @param editor reference to the editor to work on + @type Editor + """ + self.__insertMarkup("i", editor) + + def strikethrough(self, editor): + """ + Public method to generate strikethrough text. + + @param editor reference to the editor to work on + @type Editor + """ + self.__insertMarkup("del", editor) + + def __insertMarkup(self, markup, editor): + """ + Private method to insert the specified markup. + + If the editor has selected text, this text is enclosed by the given + markup. If no text is selected, the markup is inserted at the cursor + position and the cursor is positioned in between. + + @param markup markup string to be inserted + @type str + @param editor reference to the editor to work on + @type Editor + """ + if editor is None: + return + + editor.beginUndoAction() + if editor.hasSelectedText(): + newText = "<{0}>{1}</{0}>".format(markup, editor.selectedText()) + editor.replaceSelectedText(newText) + else: + editor.insert("<{0}></{0}>".format(markup)) + cline, cindex = editor.getCursorPosition() + editor.setCursorPosition(cline, cindex + len(markup) + 2) + editor.endUndoAction()
--- a/QScintilla/MarkupProviders/MarkdownProvider.py Mon Jan 02 12:49:04 2017 +0100 +++ b/QScintilla/MarkupProviders/MarkdownProvider.py Mon Jan 02 12:49:28 2017 +0100 @@ -4,7 +4,7 @@ # """ -Module implementing the base class for the markup providers. +Module implementing the Markdown markup provider. """ from __future__ import unicode_literals @@ -14,11 +14,7 @@ class MarkdownProvider(MarkupBase): """ - Class implementing the base class for the markup providers. - - Note: Derived classes need only implement those method they provide - functionality for. This base class implements do nothing variants for - all methods. + Class implementing the Markdown markup provider. """ def __init__(self): """
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QScintilla/MarkupProviders/RestructuredTextProvider.py Mon Jan 02 12:49:28 2017 +0100 @@ -0,0 +1,85 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing the reStructured Text markup provider. +""" + +from __future__ import unicode_literals + +from .MarkupBase import MarkupBase + + +class RestructuredTextProvider(MarkupBase): + """ + Class implementing the reStructured Text markup provider. + """ + def __init__(self): + """ + Constructor + """ + super(RestructuredTextProvider, self).__init__() + + def hasBold(self): + """ + Public method to indicate the availability of bold markup. + + @return flag indicating the availability of bold markup + @rtype bool + """ + return True + + def hasItalic(self): + """ + Public method to indicate the availability of italic markup. + + @return flag indicating the availability of italic markup + @rtype bool + """ + return True + + def bold(self, editor): + """ + Public method to generate bold text. + + @param editor reference to the editor to work on + @type Editor + """ + self.__insertMarkup("**", editor) + + def italic(self, editor): + """ + Public method to generate italic text. + + @param editor reference to the editor to work on + @type Editor + """ + self.__insertMarkup("*", editor) + + def __insertMarkup(self, markup, editor): + """ + Private method to insert the specified markup. + + If the editor has selected text, this text is enclosed by the given + markup. If no text is selected, the markup is inserted at the cursor + position and the cursor is positioned in between. + + @param markup markup string to be inserted + @type str + @param editor reference to the editor to work on + @type Editor + """ + if editor is None: + return + + editor.beginUndoAction() + if editor.hasSelectedText(): + newText = "{0}{1}{0}".format(markup, editor.selectedText()) + editor.replaceSelectedText(newText) + else: + editor.insert(2 * markup) + cline, cindex = editor.getCursorPosition() + editor.setCursorPosition(cline, cindex + len(markup)) + editor.endUndoAction()
--- a/QScintilla/MarkupProviders/__init__.py Mon Jan 02 12:49:04 2017 +0100 +++ b/QScintilla/MarkupProviders/__init__.py Mon Jan 02 12:49:28 2017 +0100 @@ -33,8 +33,8 @@ if extension in \ Preferences.getEditor("PreviewHtmlFileNameExtensions") or \ editor.getLanguage() == "HTML": - # TODO: implement this - pass + from .HtmlProvider import HtmlProvider + return HtmlProvider() elif extension in \ Preferences.getEditor("PreviewMarkdownFileNameExtensions") or \ editor.getLanguage().lower() == "markdown": @@ -43,8 +43,8 @@ elif extension in \ Preferences.getEditor("PreviewRestFileNameExtensions") or \ editor.getLanguage().lower() == "restructuredtext": - # TODO: implement this - pass + from .RestructuredTextProvider import RestructuredTextProvider + return RestructuredTextProvider() # no supported markup provider identified from .MarkupBase import MarkupBase
--- a/eric6.e4p Mon Jan 02 12:49:04 2017 +0100 +++ b/eric6.e4p Mon Jan 02 12:49:28 2017 +0100 @@ -882,8 +882,10 @@ <Source>QScintilla/Lexers/LexerXML.py</Source> <Source>QScintilla/Lexers/LexerYAML.py</Source> <Source>QScintilla/Lexers/__init__.py</Source> + <Source>QScintilla/MarkupProviders/HtmlProvider.py</Source> <Source>QScintilla/MarkupProviders/MarkdownProvider.py</Source> <Source>QScintilla/MarkupProviders/MarkupBase.py</Source> + <Source>QScintilla/MarkupProviders/RestructuredTextProvider.py</Source> <Source>QScintilla/MarkupProviders/__init__.py</Source> <Source>QScintilla/MiniEditor.py</Source> <Source>QScintilla/Printer.py</Source>