|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the HTML markup provider. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from .MarkupBase import MarkupBase |
|
13 |
|
14 |
|
15 class HtmlProvider(MarkupBase): |
|
16 """ |
|
17 Class implementing the HTML markup provider. |
|
18 """ |
|
19 def __init__(self): |
|
20 """ |
|
21 Constructor |
|
22 """ |
|
23 super(HtmlProvider, self).__init__() |
|
24 |
|
25 def hasBold(self): |
|
26 """ |
|
27 Public method to indicate the availability of bold markup. |
|
28 |
|
29 @return flag indicating the availability of bold markup |
|
30 @rtype bool |
|
31 """ |
|
32 return True |
|
33 |
|
34 def hasItalic(self): |
|
35 """ |
|
36 Public method to indicate the availability of italic markup. |
|
37 |
|
38 @return flag indicating the availability of italic markup |
|
39 @rtype bool |
|
40 """ |
|
41 return True |
|
42 |
|
43 def hasStrikethrough(self): |
|
44 """ |
|
45 Public method to indicate the availability of strikethrough markup. |
|
46 |
|
47 @return flag indicating the availability of strikethrough markup |
|
48 @rtype bool |
|
49 """ |
|
50 return True |
|
51 |
|
52 def bold(self, editor): |
|
53 """ |
|
54 Public method to generate bold text. |
|
55 |
|
56 @param editor reference to the editor to work on |
|
57 @type Editor |
|
58 """ |
|
59 self.__insertMarkup("b", editor) |
|
60 |
|
61 def italic(self, editor): |
|
62 """ |
|
63 Public method to generate italic text. |
|
64 |
|
65 @param editor reference to the editor to work on |
|
66 @type Editor |
|
67 """ |
|
68 self.__insertMarkup("i", editor) |
|
69 |
|
70 def strikethrough(self, editor): |
|
71 """ |
|
72 Public method to generate strikethrough text. |
|
73 |
|
74 @param editor reference to the editor to work on |
|
75 @type Editor |
|
76 """ |
|
77 self.__insertMarkup("del", editor) |
|
78 |
|
79 def __insertMarkup(self, markup, editor): |
|
80 """ |
|
81 Private method to insert the specified markup. |
|
82 |
|
83 If the editor has selected text, this text is enclosed by the given |
|
84 markup. If no text is selected, the markup is inserted at the cursor |
|
85 position and the cursor is positioned in between. |
|
86 |
|
87 @param markup markup string to be inserted |
|
88 @type str |
|
89 @param editor reference to the editor to work on |
|
90 @type Editor |
|
91 """ |
|
92 if editor is None: |
|
93 return |
|
94 |
|
95 editor.beginUndoAction() |
|
96 if editor.hasSelectedText(): |
|
97 newText = "<{0}>{1}</{0}>".format(markup, editor.selectedText()) |
|
98 editor.replaceSelectedText(newText) |
|
99 else: |
|
100 editor.insert("<{0}></{0}>".format(markup)) |
|
101 cline, cindex = editor.getCursorPosition() |
|
102 editor.setCursorPosition(cline, cindex + len(markup) + 2) |
|
103 editor.endUndoAction() |