QScintilla/MarkupProviders/RestructuredTextProvider.py

Fri, 06 Jan 2017 20:00:52 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 06 Jan 2017 20:00:52 +0100
changeset 5402
ce21a78a5fcf
parent 5398
1f4509cf8f35
child 5404
6b19ad5470a3
permissions
-rw-r--r--

Continued implementing a format button bar and provider classes for various markup languages.

# -*- 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__()
        
        self.__headerChars = ["=", "-", "~", "+", "#", "^"]
    
    def kind(self):
        """
        Public method to get the markup kind.
        
        @return string with markup kind
        @rtype str
        """
        return "rest"
    
    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 bold(self, editor):
        """
        Public method to generate bold text.
        
        @param editor reference to the editor to work on
        @type Editor
        """
        self.__insertMarkup("**", editor)
    
    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 italic(self, editor):
        """
        Public method to generate italic text.
        
        @param editor reference to the editor to work on
        @type Editor
        """
        self.__insertMarkup("*", editor)
    
    def headerLevels(self):
        """
        Public method to determine the available header levels.
        
        @return supported header levels
        @rtype int
        """
        return len(self.__headerChars)
    
    def header(self, editor, level):
        """
        Public method to generate a header.
        
        @param editor reference to the editor to work on
        @type Editor
        @param level header level
        @type int
        """
        if editor is None or level > self.headerLevels():
            return
        
        editor.beginUndoAction()
        cline, cindex = editor.getCursorPosition()
        lineSeparator = editor.getLineSeparator()
        if not editor.text(cline).endswith(lineSeparator):
            editor.insertAt(lineSeparator, cline, len(editor.text(cline)))
        lineLength = len(editor.text(cline)) - len(lineSeparator)
        editor.insertAt(
            lineLength * self.__headerChars[level - 1] + lineSeparator,
            cline + 1, 0)
        editor.setCursorPosition(cline + 2, 0)
        editor.endUndoAction()
    
    def hasCode(self):
        """
        Public method to indicate the availability of inline code markup.
        
        @return flag indicating the availability of inline code markup
        @rtype bool
        """
        return True
    
    def code(self, editor):
        """
        Public method to generate inline code 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()

eric ide

mercurial