QScintilla/MarkupProviders/RestructuredTextProvider.py

changeset 5404
6b19ad5470a3
parent 5402
ce21a78a5fcf
child 5407
f833f89571b8
equal deleted inserted replaced
5403:d6b43ecf2488 5404:6b19ad5470a3
6 """ 6 """
7 Module implementing the reStructured Text markup provider. 7 Module implementing the reStructured Text markup provider.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11
12 from PyQt5.QtWidgets import QDialog
11 13
12 from .MarkupBase import MarkupBase 14 from .MarkupBase import MarkupBase
13 15
14 16
15 class RestructuredTextProvider(MarkupBase): 17 class RestructuredTextProvider(MarkupBase):
118 @param editor reference to the editor to work on 120 @param editor reference to the editor to work on
119 @type Editor 121 @type Editor
120 """ 122 """
121 self.__insertMarkup("``", editor) 123 self.__insertMarkup("``", editor)
122 124
125 def hasCodeBlock(self):
126 """
127 Public method to indicate the availability of code block markup.
128
129 @return flag indicating the availability of code block markup
130 @rtype bool
131 """
132 return True
133
134 def codeBlock(self, editor):
135 """
136 Public method to generate code block text.
137
138 @param editor reference to the editor to work on
139 @type Editor
140 """
141 if editor is None:
142 return
143
144 lineSeparator = editor.getLineSeparator()
145 editor.beginUndoAction()
146 if editor.hasSelectedText():
147 sline, sindex, eline, eindex = editor.getSelection()
148 if not editor.text(sline).startswith((" ", "\t")):
149 # assume that all selected lines need indentation,
150 # if first line needs it
151 endLine = eline if eindex > 0 else eline - 1
152 for line in range(sline, endLine + 1):
153 editor.insertAt(" ", line, 0)
154 editor.insertAt("::{0}{0}".format(lineSeparator), sline, 0)
155 else:
156 editor.insert("::{0}{0} ".format(lineSeparator))
157 cline, cindex = editor.getCursorPosition()
158 editor.setCursorPosition(cline + 2, 4)
159 editor.endUndoAction()
160
123 def __insertMarkup(self, markup, editor): 161 def __insertMarkup(self, markup, editor):
124 """ 162 """
125 Private method to insert the specified markup. 163 Private method to insert the specified markup.
126 164
127 If the editor has selected text, this text is enclosed by the given 165 If the editor has selected text, this text is enclosed by the given
143 else: 181 else:
144 editor.insert(2 * markup) 182 editor.insert(2 * markup)
145 cline, cindex = editor.getCursorPosition() 183 cline, cindex = editor.getCursorPosition()
146 editor.setCursorPosition(cline, cindex + len(markup)) 184 editor.setCursorPosition(cline, cindex + len(markup))
147 editor.endUndoAction() 185 editor.endUndoAction()
186
187 def hasHyperlink(self):
188 """
189 Public method to indicate the availability of hyperlink markup.
190
191 @return flag indicating the availability of hyperlink markup
192 @rtype bool
193 """
194 return True
195
196 def hyperlink(self, editor):
197 """
198 Public method to generate hyperlink text.
199
200 @param editor reference to the editor to work on
201 @type Editor
202 """
203 from .HyperlinkMarkupDialog import HyperlinkMarkupDialog
204 dlg = HyperlinkMarkupDialog(False, True, noTitle=True)
205 if dlg.exec_() == QDialog.Accepted:
206 text, target, _ = dlg.getData()
207
208 link1 = "`{0}`_".format(text)
209 link2 = ".. _`{0}`:".format(text)
210 if target:
211 link2 = "{0} {1}".format(link2, target)
212
213 lineSeparator = editor.getLineSeparator()
214 editor.beginUndoAction()
215 cline, cindex = editor.getCursorPosition()
216 editor.insert(link1)
217
218 line = cline
219 while line < editor.lines():
220 if editor.text(line).strip() == "":
221 # found end of block
222 break
223 line += 1
224 if line == editor.lines():
225 # reached end of document
226 editor.insertAt(2 * lineSeparator, line, 0)
227 editor.insertAt(link2, line + 2, 0)
228 else:
229 # find end of link block or start of next block
230 line += 1
231 while line < editor.lines():
232 if not editor.text(line).startswith(".. _"):
233 break
234 line += 1
235 print("x", editor.text(line), "x")
236 if editor.text(line).strip():
237 sep = 2 * lineSeparator
238 else:
239 sep = lineSeparator
240 editor.insertAt("{0}{1}".format(link2, sep), line, 0)
241
242 editor.setCursorPosition(cline, cindex + len(link1))
243 editor.endUndoAction()
244
245 def hasLine(self):
246 """
247 Public method to indicate the availability of a horizontal line markup.
248
249 @return flag indicating the availability of a horizontal line markup
250 @rtype bool
251 """
252 return True
253
254 def line(self, editor):
255 """
256 Public method to generate a horizontal line text.
257
258 @param editor reference to the editor to work on
259 @type Editor
260 """
261 if editor is None:
262 return
263
264 lineSeparator = editor.getLineSeparator()
265 editor.beginUndoAction()
266 markup = "{0}-----{0}{0}".format(lineSeparator)
267 editor.insert(markup)
268 cline, cindex = editor.getCursorPosition()
269 editor.setCursorPosition(cline + 3, 0)
270 editor.endUndoAction()

eric ide

mercurial