52 Private method to initialize the tags menu. |
54 Private method to initialize the tags menu. |
53 |
55 |
54 @return generated menu (QMenu) |
56 @return generated menu (QMenu) |
55 """ |
57 """ |
56 menu = QMenu(self.tr("Tags")) |
58 menu = QMenu(self.tr("Tags")) |
57 menu.addAction(self.tr("autoescape - Auto Escape Characters"), |
59 menu.addAction( |
58 lambda: self.__applyTemplate("autoescape")) |
60 self.tr("autoescape - Auto Escape Characters"), |
59 menu.addSeparator() |
61 lambda: self.__applyTemplate("autoescape")) |
60 menu.addAction(self.tr("block - Named Block"), |
62 menu.addSeparator() |
61 lambda: self.__applyTemplate("block")) |
63 menu.addAction( |
62 menu.addSeparator() |
64 self.tr("block - Named Block"), |
63 menu.addAction(self.tr("comment - Multiline Comment"), |
65 lambda: self.__applyTemplate("block")) |
64 lambda: self.__applyTemplate("comment")) |
66 menu.addSeparator() |
|
67 menu.addAction( |
|
68 self.tr("comment - Multiline Comment"), |
|
69 lambda: self.__applyTemplate("comment")) |
65 menu.addAction( |
70 menu.addAction( |
66 self.tr( "csrf_token - Cross Site Request Forgery Token"), |
71 self.tr( "csrf_token - Cross Site Request Forgery Token"), |
67 lambda: self.__applyTemplate("csrf_token")) |
72 lambda: self.__applyTemplate("csrf_token")) |
68 menu.addAction(self.tr("cycle - Cycle variables each time used"), |
73 menu.addAction( |
69 lambda: self.__applyTemplate("cycle")) |
74 self.tr("cycle - Cycle variables each time used"), |
70 menu.addSeparator() |
75 lambda: self.__applyTemplate("cycle")) |
71 menu.addAction(self.tr("debug - Output Debug Information"), |
76 menu.addSeparator() |
72 lambda: self.__applyTemplate("debug")) |
77 menu.addAction( |
|
78 self.tr("debug - Output Debug Information"), |
|
79 lambda: self.__applyTemplate("debug")) |
73 menu.addSeparator() |
80 menu.addSeparator() |
74 menu.addAction( |
81 menu.addAction( |
75 self.tr("extends - Extend a template with variable contents"), |
82 self.tr("extends - Extend a template with variable contents"), |
76 lambda: self.__applyTemplate("extendsvariable")) |
83 lambda: self.__applyTemplate("extendsvariable")) |
77 menu.addAction( |
84 menu.addAction( |
78 self.tr("extends - Extend a template with file"), |
85 self.tr("extends - Extend a template with file"), |
79 lambda: self.__applyTemplate("extendsfile")) |
86 lambda: self.__applyTemplate("extendsfile")) |
|
87 menu.addSeparator() |
|
88 menu.addAction( |
|
89 self.tr("filter - Filtered Block for one or more filters"), |
|
90 lambda: self.__applyTemplate("filter")) |
|
91 menu.addAction( |
|
92 self.tr("firstof - Outputs first argument variable that is True"), |
|
93 lambda: self.__applyTemplate("firstof")) |
|
94 menu.addAction( |
|
95 self.tr("for - For Loop"), |
|
96 lambda: self.__applyTemplate("for")) |
|
97 menu.addAction( |
|
98 self.tr("for...empty - For Loop with fallback for empty loop"), |
|
99 lambda: self.__applyTemplate("for...empty")) |
|
100 menu.addSeparator() |
|
101 # TODO: add 'if...' templates here |
|
102 menu.addAction( |
|
103 self.tr("include - Render template given by variable"), |
|
104 lambda: self.__applyTemplate("includevariable")) |
|
105 menu.addAction( |
|
106 self.tr("include - Render template given by file name"), |
|
107 lambda: self.__applyTemplate("includefile")) |
80 |
108 |
81 self.__tagsMenu = menu |
109 self.__tagsMenu = menu |
82 return menu |
110 return menu |
83 |
111 |
84 def __findTemplateTag(self): |
112 def __findTemplateTag(self): |
104 return |
132 return |
105 |
133 |
106 templateText, replace = self.__generateTemplateText( |
134 templateText, replace = self.__generateTemplateText( |
107 tag, editor.selectedText()) |
135 tag, editor.selectedText()) |
108 |
136 |
109 editor.beginUndoAction() |
137 if templateText: |
110 if replace: |
138 editor.beginUndoAction() |
111 editor.replaceSelectedText(templateText) |
139 if replace: |
112 else: |
140 editor.replaceSelectedText(templateText) |
113 editor.insert(templateText) |
141 else: |
114 editor.endUndoAction() |
142 editor.insert(templateText) |
|
143 editor.endUndoAction() |
115 |
144 |
116 def __generateTemplateText(self, tag, selectedText): |
145 def __generateTemplateText(self, tag, selectedText): |
117 """ |
146 """ |
118 Private slot to generate the template text. |
147 Private slot to generate the template text. |
119 |
148 |
120 @param tag name of the tag to insert (string) |
149 @param tag name of the tag to insert (string) |
121 @param selectedText selected text of the current editor (string) |
150 @param selectedText selected text of the current editor (string) |
122 @return tuple of generated template text (string) and a flag indicating |
151 @return tuple of generated template text (string), a flag indicating |
123 to perform a replace operation (boolean) |
152 to perform a replace operation (boolean) |
124 """ |
153 """ |
125 # TODO: complete the tag generation logic |
154 # TODO: complete the tag generation logic |
126 replace = False # safe value |
155 replace = False # safe value |
|
156 ok = True |
|
157 templateText = "" |
127 |
158 |
128 #################################################### |
159 #################################################### |
129 ## Template Tags ## |
160 ## Template Tags ## |
130 #################################################### |
161 #################################################### |
131 |
162 |
132 if tag == "autoescape": |
163 if tag == "autoescape": |
133 templateText = ("{% autoescape on %} " + |
164 templateText = ( |
134 selectedText + |
165 "{% autoescape on %} " + selectedText + " {% endautoescape %}") |
135 " {% endautoescape %}") |
|
136 replace = True |
166 replace = True |
137 elif tag == "block": |
167 elif tag == "block": |
138 blockName, ok = QInputDialog.getText( |
168 data, ok = DjangoTagInputDialog.getText( |
139 None, |
169 None, |
140 self.tr("Named Block"), |
170 self.tr("Named Block"), |
141 self.tr("Enter block name:"), |
171 [self.tr("Enter block name:")], |
142 QLineEdit.Normal, |
172 ["block_name"]) |
143 "block_name") |
173 if ok: |
144 templateText = ("{% block " + |
174 templateText = ( |
145 blockName.strip() + |
175 "{% block " + data[0] + " %} " + selectedText + |
146 " %} " + |
176 " {% endblock %}") |
147 selectedText + |
177 replace = True |
148 " {% endblock %}") |
|
149 replace = True |
|
150 elif tag == "comment": |
178 elif tag == "comment": |
151 templateText = ("{% comment %} " + |
179 templateText = ( |
152 selectedText + |
180 "{% comment %} " + selectedText + " {% endcomment %}") |
153 " {% endcomment %}") |
|
154 replace = True |
181 replace = True |
155 elif tag == "csrf_token": |
182 elif tag == "csrf_token": |
156 templateText = "{% csrf_token %}" |
183 templateText = ("{% csrf_token %}") |
157 elif tag == "cycle": |
184 elif tag == "cycle": |
158 cycleItems, ok = QInputDialog.getText( |
185 data, ok = DjangoTagInputDialog.getText( |
159 None, |
186 None, |
160 self.tr("Cycle Variables"), |
187 self.tr("Cycle Variables"), |
161 self.tr("Enter items to cycle separated by space"), |
188 [self.tr("Enter items to cycle, space separated")], |
162 QLineEdit.Normal, |
189 ["item1 item2 item3"]) |
163 "item1 item2 item3") |
190 if ok: |
164 templateText = ("{% cycle " + cycleItems.strip() + " %} ") |
191 templateText = ("{% cycle " + data[0] + " %} ") |
165 elif tag == "debug": |
192 elif tag == "debug": |
166 templateText = "{% debug %}" |
193 templateText = ("{% debug %}") |
167 elif tag == "extendsvariable": |
194 elif tag == "extendsvariable": |
168 extends, ok = QInputDialog.getText( |
195 data, ok = DjangoTagInputDialog.getText( |
169 None, |
196 None, |
170 self.tr("Extends Variable"), |
197 self.tr("Extends"), |
171 self.tr("Enter variable name:"), |
198 [self.tr("Enter variable name:")], |
172 QLineEdit.Normal, |
199 ["variable"]) |
173 "variable") |
200 if ok: |
174 templateText = '{% extends ' + extends.strip() + ' %} ' |
201 templateText = ('{% extends ' + data[0] + ' %} ') |
175 elif tag == "extendsfile": |
202 elif tag == "extendsfile": |
176 extends, ok = QInputDialog.getText( |
203 data, ok = DjangoTagInputDialog.getText( |
177 None, |
204 None, |
178 self.tr("Extends Parent"), |
205 self.tr("Extends"), |
179 self.tr("Enter parent file name:"), |
206 [self.tr("Enter parent file name:")], |
180 QLineEdit.Normal, |
207 ["base.html"]) |
181 "base.html") |
208 if ok: |
182 templateText = '{% extends "' + extends.strip() + '" %} ' |
209 templateText = ('{% extends "' + data[0] + '" %} ') |
|
210 elif tag == "filter": |
|
211 data, ok = DjangoTagInputDialog.getText( |
|
212 None, |
|
213 self.tr("Tag Filters"), |
|
214 [self.tr("Multiple filters with arguments, pipes separated:")], |
|
215 ["lower|safe"]) |
|
216 if ok: |
|
217 templateText = ( |
|
218 "{% filter " + data[0] + " %} " + selectedText + |
|
219 " {% endfilter %}") |
|
220 replace = True |
|
221 elif tag == "firstof": |
|
222 data, ok = DjangoTagInputDialog.getText( |
|
223 None, |
|
224 self.tr("First Of"), |
|
225 [self.tr("Enter multiple variables, space separated:"), |
|
226 self.tr("Enter fallback value:")], |
|
227 ["var1 var2", "fallback_value"]) |
|
228 if ok: |
|
229 templateText = ( |
|
230 "{% filter force_escape %}{% firstof " + data[0] + |
|
231 ' "' + data[1] + '" %} ' + selectedText + |
|
232 " {% endfilter %}") |
|
233 replace = True |
|
234 elif tag == "for": |
|
235 data, ok = DjangoTagInputDialog.getText( |
|
236 None, |
|
237 self.tr("For Loop"), |
|
238 [self.tr("Enter variable to use for iteration:"), |
|
239 self.tr("Enter sequence to iterate over:")], |
|
240 ["item", "values"]) |
|
241 if ok: |
|
242 templateText = ( |
|
243 "{% for " + data[0] + " in " + data[1] + |
|
244 " %} " + selectedText + " {% endfor %}") |
|
245 replace = True |
|
246 elif tag == "for...empty": |
|
247 data, ok = DjangoTagInputDialog.getText( |
|
248 None, |
|
249 self.tr("For Loop"), |
|
250 [self.tr("Enter variable to use for iteration:"), |
|
251 self.tr("Enter sequence to iterate over:"), |
|
252 self.tr("Enter output to use if loop is empty:")], |
|
253 ["item", "values", '"Nothing."']) |
|
254 if ok: |
|
255 templateText = ( |
|
256 "{% for " + data[0] + " in " + data[1] + " %} " + |
|
257 selectedText + " {% empty %} " + data[2] + " {% endfor %}") |
|
258 replace = True |
|
259 elif tag == "includevariable": |
|
260 data, ok = DjangoTagInputDialog.getText( |
|
261 None, |
|
262 self.tr("Include"), |
|
263 [self.tr("Enter variable name:")], |
|
264 ["variable"]) |
|
265 if ok: |
|
266 templateText = ('{% include ' + data[0] + ' %} ') |
|
267 elif tag == "includefile": |
|
268 data, ok = DjangoTagInputDialog.getText( |
|
269 None, |
|
270 self.tr("Include"), |
|
271 [self.tr("Enter file name:")], |
|
272 ["other.html"]) |
|
273 if ok: |
|
274 templateText = ('{% include "' + data[0] + '" %} ') |
183 |
275 |
184 #################################################### |
276 #################################################### |
185 ## Fallback: return just the tag name ## |
277 ## Fallback: return just the tag name ## |
186 #################################################### |
278 #################################################### |
187 |
279 |
188 else: |
280 else: |
189 templateText = tag |
281 templateText = tag |
|
282 |
190 return templateText, replace |
283 return templateText, replace |