QScintilla/MarkupProviders/RestructuredTextProvider.py

changeset 5404
6b19ad5470a3
parent 5402
ce21a78a5fcf
child 5407
f833f89571b8
--- a/QScintilla/MarkupProviders/RestructuredTextProvider.py	Sat Jan 07 19:00:58 2017 +0100
+++ b/QScintilla/MarkupProviders/RestructuredTextProvider.py	Sat Jan 07 19:35:40 2017 +0100
@@ -9,6 +9,8 @@
 
 from __future__ import unicode_literals
 
+from PyQt5.QtWidgets import QDialog
+
 from .MarkupBase import MarkupBase
 
 
@@ -120,6 +122,42 @@
         """
         self.__insertMarkup("``", editor)
     
+    def hasCodeBlock(self):
+        """
+        Public method to indicate the availability of code block markup.
+        
+        @return flag indicating the availability of code block markup
+        @rtype bool
+        """
+        return True
+    
+    def codeBlock(self, editor):
+        """
+        Public method to generate code block text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        if editor is None:
+            return
+        
+        lineSeparator = editor.getLineSeparator()
+        editor.beginUndoAction()
+        if editor.hasSelectedText():
+            sline, sindex, eline, eindex = editor.getSelection()
+            if not editor.text(sline).startswith((" ", "\t")):
+                # assume that all selected lines need indentation,
+                # if first line needs it
+                endLine = eline if eindex > 0 else eline - 1
+                for line in range(sline, endLine + 1):
+                    editor.insertAt("    ", line, 0)
+            editor.insertAt("::{0}{0}".format(lineSeparator), sline, 0)
+        else:
+            editor.insert("::{0}{0}    ".format(lineSeparator))
+            cline, cindex = editor.getCursorPosition()
+            editor.setCursorPosition(cline + 2, 4)
+        editor.endUndoAction()
+    
     def __insertMarkup(self, markup, editor):
         """
         Private method to insert the specified markup.
@@ -145,3 +183,88 @@
             cline, cindex = editor.getCursorPosition()
             editor.setCursorPosition(cline, cindex + len(markup))
         editor.endUndoAction()
+    
+    def hasHyperlink(self):
+        """
+        Public method to indicate the availability of hyperlink markup.
+        
+        @return flag indicating the availability of hyperlink markup
+        @rtype bool
+        """
+        return True
+    
+    def hyperlink(self, editor):
+        """
+        Public method to generate hyperlink text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        from .HyperlinkMarkupDialog import HyperlinkMarkupDialog
+        dlg = HyperlinkMarkupDialog(False, True, noTitle=True)
+        if dlg.exec_() == QDialog.Accepted:
+            text,  target, _ = dlg.getData()
+            
+            link1 = "`{0}`_".format(text)
+            link2 = ".. _`{0}`:".format(text)
+            if target:
+                link2 = "{0} {1}".format(link2, target)
+            
+            lineSeparator = editor.getLineSeparator()
+            editor.beginUndoAction()
+            cline, cindex = editor.getCursorPosition()
+            editor.insert(link1)
+            
+            line = cline
+            while line < editor.lines():
+                if editor.text(line).strip() == "":
+                    # found end of block
+                    break
+                line += 1
+            if line == editor.lines():
+                # reached end of document
+                editor.insertAt(2 * lineSeparator, line, 0)
+                editor.insertAt(link2, line + 2, 0)
+            else:
+                # find end of link block or start of next block
+                line += 1
+                while line < editor.lines():
+                    if not editor.text(line).startswith(".. _"):
+                        break
+                    line += 1
+                print("x", editor.text(line), "x")
+                if editor.text(line).strip():
+                    sep = 2 * lineSeparator
+                else:
+                    sep = lineSeparator
+                editor.insertAt("{0}{1}".format(link2, sep), line, 0)
+            
+            editor.setCursorPosition(cline, cindex + len(link1))
+            editor.endUndoAction()
+    
+    def hasLine(self):
+        """
+        Public method to indicate the availability of a horizontal line markup.
+        
+        @return flag indicating the availability of a horizontal line markup
+        @rtype bool
+        """
+        return True
+    
+    def line(self, editor):
+        """
+        Public method to generate a horizontal line text.
+        
+        @param editor reference to the editor to work on
+        @type Editor
+        """
+        if editor is None:
+            return
+        
+        lineSeparator = editor.getLineSeparator()
+        editor.beginUndoAction()
+        markup = "{0}-----{0}{0}".format(lineSeparator)
+        editor.insert(markup)
+        cline, cindex = editor.getCursorPosition()
+        editor.setCursorPosition(cline + 3, 0)
+        editor.endUndoAction()

eric ide

mercurial