QScintilla/MarkupProviders/HtmlProvider.py

changeset 5411
a163fbbf2bea
parent 5407
f833f89571b8
child 5412
db5a520f69d3
equal deleted inserted replaced
5409:c6f4a6f0d051 5411:a163fbbf2bea
7 Module implementing the HTML markup provider. 7 Module implementing the HTML markup provider.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 from PyQt5.QtWidgets import QDialog 12 from PyQt5.QtCore import QCoreApplication
13 from PyQt5.QtWidgets import QDialog, QInputDialog
13 14
14 from .MarkupBase import MarkupBase 15 from .MarkupBase import MarkupBase
15 16
16 17
17 class HtmlProvider(MarkupBase): 18 class HtmlProvider(MarkupBase):
314 editor.beginUndoAction() 315 editor.beginUndoAction()
315 editor.insert(markup) 316 editor.insert(markup)
316 cline, cindex = editor.getCursorPosition() 317 cline, cindex = editor.getCursorPosition()
317 editor.setCursorPosition(cline, cindex + len(markup)) 318 editor.setCursorPosition(cline, cindex + len(markup))
318 editor.endUndoAction() 319 editor.endUndoAction()
320
321 def hasBulletedList(self):
322 """
323 Public method to indicate the availability of bulleted list markup.
324
325 @return flag indicating the availability of bulleted list markup
326 @rtype bool
327 """
328 return True
329
330 def bulletedList(self, editor):
331 """
332 Public method to generate bulleted list text.
333
334 @param editor reference to the editor to work on
335 @type Editor
336 """
337 self.__makeList(editor, "ul")
338
339 def hasNumberedList(self):
340 """
341 Public method to indicate the availability of numbered list markup.
342
343 @return flag indicating the availability of numbered list markup
344 @rtype bool
345 """
346 return True
347
348 def numberedList(self, editor):
349 """
350 Public method to generate numbered list text.
351
352 @param editor reference to the editor to work on
353 @type Editor
354 """
355 self.__makeList(editor, "ol")
356
357 def __makeList(self, editor, listType):
358 """
359 Private method to generate the desired list markup.
360
361 @param editor reference to the editor to work on
362 @type Editor
363 @param listType type of the desired list (should be ul or ol)
364 @type str
365 """
366 if editor is None:
367 return
368
369 lineSeparator = editor.getLineSeparator()
370 editor.beginUndoAction()
371 if editor.hasSelectedText():
372 startLine, startIndex, endLine, endIndex = \
373 editor.getSelection()
374 if endIndex == 0:
375 endLine -= 1
376 for line in range(startLine, endLine + 1):
377 editor.insertAt("</li>", line, len(editor.text(line).rstrip()))
378 editor.insertAt(" <li>", line, 0)
379 if line == editor.lines() - 1:
380 editor.insertAt(lineSeparator, line, 1000)
381 editor.insertAt("</{1}>{0}".format(lineSeparator, listType),
382 endLine + 1, 0)
383 editor.insertAt("<{1}>{0}".format(lineSeparator, listType),
384 startLine, 0)
385 editor.setCursorPosition(endLine + 3, 0)
386 else:
387 listElements, ok = QInputDialog.getInt(
388 None,
389 QCoreApplication.translate(
390 "HtmlProvider", "Create List"),
391 QCoreApplication.translate(
392 "HtmlProvider", "Enter desired number of list elements:"),
393 0, 0, 99, 1)
394 if ok:
395 if listElements == 0:
396 listElements = 1
397 cline, cindex = editor.getCursorPosition()
398 listBody = \
399 listElements * " <li></li>{0}".format(lineSeparator)
400 markup = "<{1}>{0}{2}</{1}>{0}".format(
401 lineSeparator, listType, listBody)
402 if cindex == 0:
403 editor.insertAt(markup, cline, cindex)
404 editor.setCursorPosition(cline + 1, 6)
405 else:
406 if cline == editor.lines() - 1:
407 editor.insertAt(lineSeparator, cline, 1000)
408 editor.insertAt(markup, cline + 1, 0)
409 editor.setCursorPosition(cline + 2, 6)
410 editor.endUndoAction()

eric ide

mercurial