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