ProjectDjangoTagsMenu/DjangoTagsMenuHandler.py

changeset 8
7e8f788fe340
parent 7
8d928ad07c0a
child 9
1b11bf54b3b2
equal deleted inserted replaced
7:8d928ad07c0a 8:7e8f788fe340
8 """ 8 """
9 9
10 import os 10 import os
11 11
12 from PyQt4.QtCore import QObject 12 from PyQt4.QtCore import QObject
13 from PyQt4.QtGui import QMenu, QInputDialog 13 from PyQt4.QtGui import QMenu, QInputDialog, QDialog
14 14
15 from E5Gui.E5Application import e5App 15 from E5Gui.E5Application import e5App
16 from E5Gui import E5FileDialog 16 from E5Gui import E5FileDialog
17 17
18 from .DjangoTagInputDialog import DjangoTagInputDialog 18 from .DjangoTagInputDialog import DjangoTagInputDialog
50 mainMenu.addAction(self.tr("Django Template Tags Locator"), 50 mainMenu.addAction(self.tr("Django Template Tags Locator"),
51 self.__findTemplateTag) 51 self.__findTemplateTag)
52 mainMenu.addSeparator() 52 mainMenu.addSeparator()
53 mainMenu.addMenu(self.__initTagsMenu()) 53 mainMenu.addMenu(self.__initTagsMenu())
54 mainMenu.addMenu(self.__initFiltersMenu()) 54 mainMenu.addMenu(self.__initFiltersMenu())
55 mainMenu.addMenu(self.__initHumanizeMenu())
56 mainMenu.addMenu(self.__initWebDesignMenu())
57 mainMenu.addMenu(self.__initStaticMenu())
55 58
56 def __initTagsMenu(self): 59 def __initTagsMenu(self):
57 """ 60 """
58 Private method to initialize the tags menu. 61 Private method to initialize the tags menu.
59 62
100 lambda: self.__applyTemplate("for")) 103 lambda: self.__applyTemplate("for"))
101 menu.addAction( 104 menu.addAction(
102 self.tr("for...empty - For Loop with fallback for empty loop"), 105 self.tr("for...empty - For Loop with fallback for empty loop"),
103 lambda: self.__applyTemplate("for...empty")) 106 lambda: self.__applyTemplate("for...empty"))
104 menu.addSeparator() 107 menu.addSeparator()
105 # TODO: add 'if...' templates here 108 menu.addAction(
109 self.tr("if - if...elif...else... clauses"),
110 lambda: self.__applyTemplate("if"))
111 menu.addAction(
112 self.tr("ifchanged - Check if a value has changed"),
113 lambda: self.__applyTemplate("ifchanged"))
114 menu.addAction(
115 self.tr("ifequal - Output block if variables are equal"),
116 lambda: self.__applyTemplate("ifequal"))
117 menu.addAction(
118 self.tr("ifnotequal - Output block if variables are not equal"),
119 lambda: self.__applyTemplate("ifnotequal"))
106 menu.addAction( 120 menu.addAction(
107 self.tr("include - Render template given by variable"), 121 self.tr("include - Render template given by variable"),
108 lambda: self.__applyTemplate("includevariable")) 122 lambda: self.__applyTemplate("includevariable"))
109 menu.addAction( 123 menu.addAction(
110 self.tr("include - Render template given by file name"), 124 self.tr("include - Render template given by file name"),
224 lambda: self.__applyTemplate("floatformat")) 238 lambda: self.__applyTemplate("floatformat"))
225 menu.addAction( 239 menu.addAction(
226 self.tr("force_escape - Escape as HTML immediately"), 240 self.tr("force_escape - Escape as HTML immediately"),
227 lambda: self.__applyTemplate("force_escape")) 241 lambda: self.__applyTemplate("force_escape"))
228 menu.addSeparator() 242 menu.addSeparator()
243 menu.addAction(
244 self.tr("get_digit - Extract rightmost digit"),
245 lambda: self.__applyTemplate("get_digit"))
246 menu.addSeparator()
247 menu.addAction(
248 self.tr("iriencode - Convert IRI to string"),
249 lambda: self.__applyTemplate("iriencode"))
250 menu.addSeparator()
251 menu.addAction(
252 self.tr("join - Join list"),
253 lambda: self.__applyTemplate("join"))
254 menu.addSeparator()
255 menu.addAction(
256 self.tr("last - Return last item in list"),
257 lambda: self.__applyTemplate("last"))
258 menu.addAction(
259 self.tr("length - Return length of the value"),
260 lambda: self.__applyTemplate("length"))
261 menu.addAction(
262 self.tr("length_is - Check length"),
263 lambda: self.__applyTemplate("length_is"))
264 menu.addAction(
265 self.tr("linebreaks - Replace line breaks with HTML"),
266 lambda: self.__applyTemplate("linebreaks"))
267 menu.addAction(
268 self.tr("linebreaksbr - Replace line breaks with <br/>"),
269 lambda: self.__applyTemplate("linebreaksbr"))
270 menu.addAction(
271 self.tr("linenumbers - Display text with line numbers"),
272 lambda: self.__applyTemplate("linenumbers"))
273 menu.addAction(
274 self.tr("ljust - Left-align value"),
275 lambda: self.__applyTemplate("ljust"))
276 menu.addAction(
277 self.tr("lower - Convert to lowercase"),
278 lambda: self.__applyTemplate("lower"))
279 menu.addSeparator()
280 menu.addAction(
281 self.tr("make_list - Turn value into a list"),
282 lambda: self.__applyTemplate("make_list"))
283 menu.addSeparator()
284 menu.addAction(
285 self.tr("phone2numeric - Convert phone number into numeric"),
286 lambda: self.__applyTemplate("phone2numeric"))
287 menu.addAction(
288 self.tr("pluralize - Return plural suffix"),
289 lambda: self.__applyTemplate("pluralize"))
290 menu.addAction(
291 self.tr("pprint - Pretty Print"),
292 lambda: self.__applyTemplate("pprint"))
293 menu.addSeparator()
294 menu.addAction(
295 self.tr("random - Return random item from list"),
296 lambda: self.__applyTemplate("random"))
297 menu.addAction(
298 self.tr("removetags - Remove HTML tags"),
299 lambda: self.__applyTemplate("removetags"))
300 menu.addAction(
301 self.tr("rjust - Right-align value"),
302 lambda: self.__applyTemplate("rjust"))
303 menu.addSeparator()
304 menu.addAction(
305 self.tr("safe - Mark as not requiring HTML escaping "),
306 lambda: self.__applyTemplate("safe"))
307 menu.addAction(
308 self.tr("safeseq - Mark as a safe sequence"),
309 lambda: self.__applyTemplate("safeseq"))
310 menu.addAction(
311 self.tr("slice - Return a slice of a list"),
312 lambda: self.__applyTemplate("slice"))
313 menu.addAction(
314 self.tr("slugify - Return value slugified"),
315 lambda: self.__applyTemplate("slugify"))
316 menu.addAction(
317 self.tr("stringformat - Format variable"),
318 lambda: self.__applyTemplate("stringformat"))
319 menu.addAction(
320 self.tr("striptags - Strip all HTML tags"),
321 lambda: self.__applyTemplate("striptags"))
322 menu.addSeparator()
323 menu.addAction(
324 self.tr("time - Format time"),
325 lambda: self.__applyTemplate("time"))
326 menu.addAction(
327 self.tr("timesince - Format as time since"),
328 lambda: self.__applyTemplate("timesince"))
329 menu.addAction(
330 self.tr("timeuntil - Format as time until"),
331 lambda: self.__applyTemplate("timeuntil"))
332 menu.addAction(
333 self.tr("title - Convert to titlecase"),
334 lambda: self.__applyTemplate("title"))
335 menu.addAction(
336 self.tr("truncatechars - Truncate after a number of characters"),
337 lambda: self.__applyTemplate("truncatechars"))
338 menu.addAction(
339 self.tr("truncatewords - Truncate after a number of words"),
340 lambda: self.__applyTemplate("truncatewords"))
341 menu.addAction(
342 self.tr("truncatewords_html - Truncate after a number of words"
343 " (HTML aware)"),
344 lambda: self.__applyTemplate("truncatewords_html"))
345 menu.addSeparator()
346 menu.addAction(
347 self.tr("unordered_list - Return HTML unordered list"),
348 lambda: self.__applyTemplate("unordered_list"))
349 menu.addAction(
350 self.tr("upper - Convert to uppercase"),
351 lambda: self.__applyTemplate("upper"))
352 menu.addAction(
353 self.tr("urlencode - Escape as URL"),
354 lambda: self.__applyTemplate("urlencode"))
355 menu.addAction(
356 self.tr("urlize - Convert URLs as clickable links"),
357 lambda: self.__applyTemplate("urlize"))
358 menu.addAction(
359 self.tr("urlizetrunc - Convert URLs as clickable links and"
360 " truncate"),
361 lambda: self.__applyTemplate("urlizetrunc"))
362 menu.addSeparator()
363 menu.addAction(
364 self.tr("wordcount - Return number of word"),
365 lambda: self.__applyTemplate("wordcount"))
366 menu.addAction(
367 self.tr("wordwrap - Wrap words"),
368 lambda: self.__applyTemplate("wordwrap"))
369 menu.addSeparator()
370 menu.addAction(
371 self.tr("yesno - Map True, False and None"),
372 lambda: self.__applyTemplate("yesno"))
229 373
230 self.__filtersMenu = menu 374 self.__filtersMenu = menu
375 return menu
376
377 def __initHumanizeMenu(self):
378 """
379 Private method to initialize the humanize menu.
380
381 @return generated menu (QMenu)
382 """
383 menu = QMenu(self.tr("Humanize"))
384 menu.addAction(
385 self.tr("Load Package"),
386 lambda: self.__applyTemplate("loadhumanize"))
387 menu.addSeparator()
388 menu.addAction(
389 self.tr("apnumber - Format integer the Associated Press style"),
390 lambda: self.__applyTemplate("apnumber"))
391 menu.addSeparator()
392 menu.addAction(
393 self.tr("intcomma - Format integer with commas"),
394 lambda: self.__applyTemplate("intcomma"))
395 menu.addAction(
396 self.tr("intword - Convert integer to word"),
397 lambda: self.__applyTemplate("intword"))
398 menu.addSeparator()
399 menu.addAction(
400 self.tr("naturalday - Format date naturally"),
401 lambda: self.__applyTemplate("naturalday"))
402 menu.addAction(
403 self.tr("naturaltime - Format time naturally"),
404 lambda: self.__applyTemplate("naturaltime"))
405 menu.addSeparator()
406 menu.addAction(
407 self.tr("ordinal - Convert integer to ordinal"),
408 lambda: self.__applyTemplate("ordinal"))
409
410 self.__humanizeMenu = menu
411 return menu
412
413 def __initWebDesignMenu(self):
414 """
415 Private method to initialize the web design menu.
416
417 @return generated menu (QMenu)
418 """
419 menu = QMenu(self.tr("Web Design"))
420 menu.addAction(
421 self.tr("Load Package"),
422 lambda: self.__applyTemplate("loadweb"))
423 menu.addSeparator()
424 menu.addAction(
425 self.tr("lorem - Builtin Lorem Impsum Generator"),
426 lambda: self.__applyTemplate("lorem"))
427
428 self.__webMenu = menu
429 return menu
430
431 def __initStaticMenu(self):
432 """
433 Private method to initialize the static menu.
434
435 @return generated menu (QMenu)
436 """
437 menu = QMenu(self.tr("Static"))
438 menu.addAction(
439 self.tr("Load Package"),
440 lambda: self.__applyTemplate("loadstatic"))
441 menu.addSeparator()
442 menu.addAction(
443 self.tr("static - Link to static file"),
444 lambda: self.__applyTemplate("staticfile"))
445 menu.addAction(
446 self.tr("static - Link to static file (via variable)"),
447 lambda: self.__applyTemplate("staticvariable"))
448 menu.addAction(
449 self.tr("get_static_prefix - Insert static URL"),
450 lambda: self.__applyTemplate("get_static_prefix"))
451 menu.addAction(
452 self.tr("get_media_prefix - Insert media URL"),
453 lambda: self.__applyTemplate("get_media_prefix"))
454
455 self.__staticMenu = menu
231 return menu 456 return menu
232 457
233 def __findTemplateTag(self): 458 def __findTemplateTag(self):
234 """ 459 """
235 Private slot to find a template tag and insert its text. 460 Private slot to find a template tag and insert its text.
270 @param tag name of the tag to insert (string) 495 @param tag name of the tag to insert (string)
271 @param selectedText selected text of the current editor (string) 496 @param selectedText selected text of the current editor (string)
272 @return tuple of generated template text (string), a flag indicating 497 @return tuple of generated template text (string), a flag indicating
273 to perform a replace operation (boolean) 498 to perform a replace operation (boolean)
274 """ 499 """
275 # TODO: complete the tag generation logic
276 replace = False # safe value 500 replace = False # safe value
277 ok = True 501 ok = True
278 templateText = "" 502 templateText = ""
279 503
280 #################################################### 504 ####################################################
376 templateText = ( 600 templateText = (
377 "{{% for {0} in {1} %}} {2} {{% empty %}} {3}" 601 "{{% for {0} in {1} %}} {2} {{% empty %}} {3}"
378 " {{% endfor %}}".format( 602 " {{% endfor %}}".format(
379 data[0], data[1], selectedText, data[2])) 603 data[0], data[1], selectedText, data[2]))
380 replace = True 604 replace = True
605 elif tag == "if":
606 from .IfTagInputDialog import IfTagInputDialog
607 dlg = IfTagInputDialog()
608 if dlg.exec_() == QDialog.Accepted:
609 templateText = dlg.getTag()
610 elif tag == "ifchanged":
611 data, ok = DjangoTagInputDialog.getText(
612 None,
613 self.tr("Check Variables for Changes"),
614 [self.tr("Enter variables to check (space separated):")],
615 ["variable1 variable2"])
616 if ok:
617 templateText = (
618 '{{% ifchanged {0} %}}{1}{{% endifchanged %}}'
619 .format(data[0], selectedText))
620 replace = True
621 elif tag == "ifequal":
622 data, ok = DjangoTagInputDialog.getText(
623 None,
624 self.tr("Check If Equal"),
625 [self.tr("Enter first variable or string to check:"),
626 self.tr("Enter second variable or string to check:")],
627 ["user.username", '"adrian"'])
628 if ok:
629 templateText = (
630 '{{% ifequal {0} {1} %}}{2}{{% endifequal %}}'
631 .format(data[0], data[1], selectedText))
632 replace = True
633 elif tag == "ifnotequal":
634 data, ok = DjangoTagInputDialog.getText(
635 None,
636 self.tr("Check If Not Equal"),
637 [self.tr("Enter first variable or string to check:"),
638 self.tr("Enter second variable or string to check:")],
639 ["user.username", '"adrian"'])
640 if ok:
641 templateText = (
642 '{{% ifnotequal {0} {1} %}}{2}{{% endifnotequal %}}'
643 .format(data[0], data[1], selectedText))
644 replace = True
381 elif tag == "includevariable": 645 elif tag == "includevariable":
382 data, ok = DjangoTagInputDialog.getText( 646 data, ok = DjangoTagInputDialog.getText(
383 None, 647 None,
384 self.tr("Include"), 648 self.tr("Include"),
385 [self.tr("Enter variable name:")], 649 [self.tr("Enter variable name:")],
504 if ok: 768 if ok:
505 templateText = '{{% with {0} %}} {1} {{% endwith %}}'.format( 769 templateText = '{{% with {0} %}} {1} {{% endwith %}}'.format(
506 data[0], selectedText) 770 data[0], selectedText)
507 771
508 #################################################### 772 ####################################################
509 ## Template Filters ## 773 ## Template Filters ##
510 #################################################### 774 ####################################################
511 775
512 elif tag == "add": 776 elif tag == "add":
513 data, ok = DjangoTagInputDialog.getText( 777 data, ok = DjangoTagInputDialog.getText(
514 None, 778 None,
515 self.tr("Add Variable or String"), 779 self.tr("Add Variable or String"),
516 [self.tr("Variables or String to add:") 780 [self.tr("Variables or String to add:")],
517 ],
518 ["variable"]) 781 ["variable"])
519 if ok: 782 if ok:
520 templateText = "|add:{0}".format(data[0]) 783 templateText = "|add:{0}".format(data[0])
521 elif tag == "addslashes": 784 elif tag == "addslashes":
522 templateText = "|addslashes " 785 templateText = "|addslashes"
523 elif tag == "capfirst": 786 elif tag == "capfirst":
524 templateText = "|capfirst " 787 templateText = "|capfirst"
525 elif tag == "center": 788 elif tag == "center":
526 width, ok = QInputDialog.getInt( 789 width, ok = QInputDialog.getInt(
527 None, 790 None,
528 self.tr("Center Value"), 791 self.tr("Center Value"),
529 self.tr("Enter width of the output:"), 792 self.tr("Enter width of the output:"),
532 templateText = '|center:"{0}"'.format(width) 795 templateText = '|center:"{0}"'.format(width)
533 elif tag == "cut": 796 elif tag == "cut":
534 data, ok = DjangoTagInputDialog.getText( 797 data, ok = DjangoTagInputDialog.getText(
535 None, 798 None,
536 self.tr("Cut Characters"), 799 self.tr("Cut Characters"),
537 [self.tr("Characters to cut:") 800 [self.tr("Characters to cut:")],
538 ],
539 [" "]) 801 [" "])
540 if ok: 802 if ok:
541 templateText = '|cut:"{0}"'.format(data[0]) 803 templateText = '|cut:"{0}"'.format(data[0])
542 elif tag == "date": 804 elif tag == "date":
543 date, ok = QInputDialog.getItem( 805 date, ok = QInputDialog.getItem(
546 self.tr("Enter date format:"), 808 self.tr("Enter date format:"),
547 ["DATETIME_FORMAT", "SHORT_DATETIME_FORMAT", 809 ["DATETIME_FORMAT", "SHORT_DATETIME_FORMAT",
548 "SHORT_DATE_FORMAT", "DATE_FORMAT"], 810 "SHORT_DATE_FORMAT", "DATE_FORMAT"],
549 0, True) 811 0, True)
550 if ok: 812 if ok:
551 templateText = '|date:"{0}" '.format(date) 813 if date:
814 templateText = '|date:"{0}"'.format(date)
815 else:
816 templateText = '|date'
552 elif tag == "default": 817 elif tag == "default":
553 data, ok = DjangoTagInputDialog.getText( 818 data, ok = DjangoTagInputDialog.getText(
554 None, 819 None,
555 self.tr("Default Value if False"), 820 self.tr("Default Value if False"),
556 [self.tr("Enter default value if result is False:") 821 [self.tr("Enter default value if result is False:")],
557 ],
558 ["nothing"]) 822 ["nothing"])
559 if ok: 823 if ok:
560 templateText = '|default:"{0}" '.format(data[0]) 824 templateText = '|default:"{0}"'.format(data[0])
561 elif tag == "default_if_none": 825 elif tag == "default_if_none":
562 data, ok = DjangoTagInputDialog.getText( 826 data, ok = DjangoTagInputDialog.getText(
563 None, 827 None,
564 self.tr("Default Value if None"), 828 self.tr("Default Value if None"),
565 [self.tr("Enter default value if result is None:") 829 [self.tr("Enter default value if result is None:")],
566 ],
567 ["nothing"]) 830 ["nothing"])
568 if ok: 831 if ok:
569 templateText = '|default:"{0}" '.format(data[0]) 832 templateText = '|default:"{0}"'.format(data[0])
570 elif tag == "dictsort": 833 elif tag == "dictsort":
571 data, ok = DjangoTagInputDialog.getText( 834 data, ok = DjangoTagInputDialog.getText(
572 None, 835 None,
573 self.tr("Sort Dictionaries"), 836 self.tr("Sort Dictionaries"),
574 [self.tr("Enter key to sort on:") 837 [self.tr("Enter key to sort on:")],
575 ],
576 ["key"]) 838 ["key"])
577 if ok: 839 if ok:
578 templateText = '|dictsort:"{0}" '.format(data[0]) 840 templateText = '|dictsort:"{0}"'.format(data[0])
579 elif tag == "dictsortreversed": 841 elif tag == "dictsortreversed":
580 data, ok = DjangoTagInputDialog.getText( 842 data, ok = DjangoTagInputDialog.getText(
581 None, 843 None,
582 self.tr("Sort Dictionaries reversed"), 844 self.tr("Sort Dictionaries reversed"),
583 [self.tr("Enter key to sort on:") 845 [self.tr("Enter key to sort on:")],
584 ],
585 ["key"]) 846 ["key"])
586 if ok: 847 if ok:
587 templateText = '|dictsortreversed:"{0}" '.format(data[0]) 848 templateText = '|dictsortreversed:"{0}"'.format(data[0])
588 elif tag == "divisibleby": 849 elif tag == "divisibleby":
589 divisor, ok = QInputDialog.getInt( 850 divisor, ok = QInputDialog.getInt(
590 None, 851 None,
591 self.tr("Check Divisibility"), 852 self.tr("Check Divisibility"),
592 self.tr("Enter divisor value:"), 853 self.tr("Enter divisor value:"),
593 2, 1, 99, 1) 854 2, 1, 99, 1)
594 if ok: 855 if ok:
595 templateText = '|divisibleby:"{0}" '.format(divisor) 856 templateText = '|divisibleby:"{0}"'.format(divisor)
596 elif tag == "escape": 857 elif tag == "escape":
597 templateText = '|escape ' 858 templateText = '|escape'
598 elif tag == "escapejs": 859 elif tag == "escapejs":
599 templateText = '|escapejs ' 860 templateText = '|escapejs'
600 elif tag == "filesizeformat": 861 elif tag == "filesizeformat":
601 templateText = '|filesizeformat ' 862 templateText = '|filesizeformat'
602 elif tag == "first": 863 elif tag == "first":
603 templateText = '|first ' 864 templateText = '|first'
604 elif tag == "fix_ampersands": 865 elif tag == "fix_ampersands":
605 templateText = '|fix_ampersands ' 866 templateText = '|fix_ampersands'
606 elif tag == "floatformat": 867 elif tag == "floatformat":
607 decimals, ok = QInputDialog.getInt( 868 decimals, ok = QInputDialog.getInt(
608 None, 869 None,
609 self.tr("Format Floating Number"), 870 self.tr("Format Floating Number"),
610 self.tr("Enter number of decimal places:"), 871 self.tr("Enter number of decimal places:"),
611 2, -20, 20, 1) 872 2, -20, 20, 1)
612 if ok: 873 if ok:
613 templateText = '|floatformat:"{0}" '.format(decimals) 874 templateText = '|floatformat:"{0}"'.format(decimals)
614 elif tag == "force_escape": 875 elif tag == "force_escape":
615 templateText = '|force_escape ' 876 templateText = '|force_escape'
616 877 elif tag == "get_digit":
878 digit, ok = QInputDialog.getInt(
879 None,
880 self.tr("Get Rightmost Digit"),
881 self.tr("Enter index of digit:"),
882 1, 1, 99, 1)
883 if ok:
884 templateText = '|get_digit:"{0}"'.format(digit)
885 elif tag == "iriencode":
886 templateText = '|iriencode'
887 elif tag == "join":
888 data, ok = DjangoTagInputDialog.getText(
889 None,
890 self.tr("Join List"),
891 [self.tr("Enter string to join by:")],
892 ["//"])
893 if ok:
894 templateText = '|join:"{0}"'.format(data[0])
895 elif tag == "last":
896 templateText = '|last'
897 elif tag == "length":
898 templateText = '|length'
899 elif tag == "length_is":
900 length, ok = QInputDialog.getInt(
901 None,
902 self.tr("Check Length"),
903 self.tr("Enter desired length:"),
904 10, 1, 99, 1)
905 if ok:
906 templateText = '|length_is:"{0}"'.format(length)
907 elif tag == "linebreaks":
908 templateText = '|linebreaks'
909 elif tag == "linebreaksbr":
910 templateText = '|linebreaksbr'
911 elif tag == "linenumbers":
912 templateText = '|linenumbers'
913 elif tag == "ljust":
914 width, ok = QInputDialog.getInt(
915 None,
916 self.tr("Left-align Value"),
917 self.tr("Enter width of the output:"),
918 10, 1, 99, 1)
919 if ok:
920 templateText = '|ljust:"{0}"'.format(width)
921 elif tag == "lower":
922 templateText = '|lower'
923 elif tag == "make_list":
924 templateText = '|make_list'
925 elif tag == "phone2numeric":
926 templateText = '|phone2numeric'
927 elif tag == "pluralize":
928 data, ok = DjangoTagInputDialog.getText(
929 None,
930 self.tr("Plural Suffix"),
931 [self.tr("Enter plural suffix (nothing for default):")],
932 [""])
933 if ok:
934 if data[0]:
935 templateText = '|pluralize:"{0}"'.format(data[0])
936 else:
937 templateText = '|pluralize'
938 elif tag == "pprint":
939 templateText = '|pprint'
940 elif tag == "random":
941 templateText = '|random'
942 elif tag == "removetags":
943 data, ok = DjangoTagInputDialog.getText(
944 None,
945 self.tr("Remove HTML Tags"),
946 [self.tr("Enter HTML tags to remove (space separated):")],
947 ["b span"])
948 if ok:
949 templateText = '|removetags:"{0}"'.format(data[0])
950 elif tag == "rjust":
951 width, ok = QInputDialog.getInt(
952 None,
953 self.tr("Right-align Value"),
954 self.tr("Enter width of the output:"),
955 10, 1, 99, 1)
956 if ok:
957 templateText = '|rjust:"{0}"'.format(width)
958 elif tag == "safe":
959 templateText = '|safe'
960 elif tag == "safeseq":
961 templateText = '|safeseq'
962 elif tag == "slice":
963 data, ok = DjangoTagInputDialog.getText(
964 None,
965 self.tr("Extract slice of a list"),
966 [self.tr("Enter Python like slice expression:")],
967 ["1:5"])
968 if ok:
969 templateText = '|slice:"{0}"'.format(data[0])
970 elif tag == "slugify":
971 templateText = '|slugify'
972 elif tag == "stringformat":
973 data, ok = DjangoTagInputDialog.getText(
974 None,
975 self.tr("Perform String Formatting"),
976 [self.tr("Enter Python like string format:")],
977 [""])
978 if ok:
979 templateText = '|stringformat:"{0}"'.format(data[0])
980 elif tag == "striptags":
981 templateText = '|striptags'
982 elif tag == "time":
983 time, ok = QInputDialog.getItem(
984 None,
985 self.tr("Format Date"),
986 self.tr("Enter date format:"),
987 ["TIME_FORMAT"],
988 0, True)
989 if ok:
990 if time:
991 templateText = '|time:"{0}"'.format(time)
992 else:
993 templateText = '|time'
994 elif tag == "timesince":
995 data[0], ok = DjangoTagInputDialog.getText(
996 None,
997 self.tr("Time Since"),
998 [self.tr("Enter variable containing time reference:")],
999 ["comment_date"])
1000 if ok:
1001 if data[0]:
1002 templateText = '|timesince:{0}'.format(data[0])
1003 else:
1004 templateText = '|timesince'
1005 elif tag == "timeuntil":
1006 data[0], ok = DjangoTagInputDialog.getText(
1007 None,
1008 self.tr("Time Until"),
1009 [self.tr("Enter variable containing time reference:")],
1010 ["from_date"])
1011 if ok:
1012 if data[0]:
1013 templateText = '|timeuntil:{0}'.format(data[0])
1014 else:
1015 templateText = '|timeuntil'
1016 elif tag == "title":
1017 templateText = '|title'
1018 elif tag == "truncatechars":
1019 characters, ok = QInputDialog.getInt(
1020 None,
1021 self.tr("Truncate String"),
1022 self.tr("Enter number of characters:"),
1023 10, 1, 99, 1)
1024 if ok:
1025 templateText = '|truncatechars:{0}'.format(characters)
1026 elif tag == "truncatewords":
1027 words, ok = QInputDialog.getInt(
1028 None,
1029 self.tr("Truncate String"),
1030 self.tr("Enter number of words:"),
1031 10, 1, 99, 1)
1032 if ok:
1033 templateText = '|truncatewords:{0}'.format(words)
1034 elif tag == "truncatewords_html":
1035 words, ok = QInputDialog.getInt(
1036 None,
1037 self.tr("Truncate String"),
1038 self.tr("Enter number of words:"),
1039 10, 1, 99, 1)
1040 if ok:
1041 templateText = '|truncatewords_html:{0}'.format(words)
1042 elif tag == "unordered_list":
1043 templateText = '|unordered_list'
1044 elif tag == "upper":
1045 templateText = '|upper'
1046 elif tag == "urlencode":
1047 templateText = '|urlencode'
1048 elif tag == "urlize":
1049 templateText = '|urlize'
1050 elif tag == "urlizetrunc":
1051 characters, ok = QInputDialog.getInt(
1052 None,
1053 self.tr("Convert URLs as clickable links and truncate"),
1054 self.tr("Enter number of characters:"),
1055 10, 1, 199, 1)
1056 if ok:
1057 templateText = '|urlizetrunc:{0}'.format(characters)
1058 elif tag == "wordcount":
1059 templateText = '|wordcount'
1060 elif tag == "wordwrap":
1061 characters, ok = QInputDialog.getInt(
1062 None,
1063 self.tr("Wrap words"),
1064 self.tr("Enter number of characters:"),
1065 10, 1, 99, 1)
1066 if ok:
1067 templateText = '|wordwrap:{0}'.format(characters)
1068 elif tag == "yesno":
1069 data, ok = DjangoTagInputDialog.getText(
1070 None,
1071 self.tr("Map True, False and None"),
1072 [self.tr("Enter mapping (comma separated):")],
1073 ["yeah,no,maybe"])
1074 if ok:
1075 if data[0]:
1076 templateText = '|yesno:"{0}"'.format(data[0])
1077 else:
1078 templateText = '|yesno'
1079
1080 ####################################################
1081 ## Humanize Template Tags ##
1082 ####################################################
1083
1084 elif tag == "loadhumanize":
1085 templateText = '{% load humanize %}'
1086 elif tag == "apnumber":
1087 templateText = '|apnumber'
1088 elif tag == "intcomma":
1089 templateText = '|intcomma'
1090 elif tag == "intword":
1091 templateText = '|intword'
1092 elif tag == "naturalday":
1093 templateText = '|naturalday'
1094 elif tag == "naturaltime":
1095 templateText = '|naturaltime'
1096 elif tag == "ordinal":
1097 templateText = '|ordinal'
1098
1099 ####################################################
1100 ## Web Design Template Tags ##
1101 ####################################################
1102
1103 elif tag == "loadweb":
1104 templateText = '{% load webdesign %}'
1105 elif tag == "lorem":
1106 from .LoremTagInputDialog import LoremTagInputDialog
1107 dlg = LoremTagInputDialog()
1108 if dlg.exec_() == QDialog.Accepted:
1109 templateText = "{{% {0} %}}".format(dlg.getTag())
1110
1111 ####################################################
1112 ## Static Template Tags ##
1113 ####################################################
1114
1115 elif tag == "loadstatic":
1116 templateText = '{% load static %}'
1117 elif tag == "staticfile":
1118 data, ok = DjangoTagInputDialog.getText(
1119 None,
1120 self.tr("Link to static file"),
1121 [self.tr("Enter relative path of static file:")],
1122 ["images/hi.jpg"])
1123 if ok:
1124 templateText = '{{% static "{0}" %}}'.format(data[0])
1125 elif tag == "staticvariable":
1126 data, ok = DjangoTagInputDialog.getText(
1127 None,
1128 self.tr("Link to static file"),
1129 [self.tr("Enter variable containing relative path of"
1130 " static file:")],
1131 ["user_stylesheet"])
1132 if ok:
1133 templateText = '{{% static {0} %}}'.format(data[0])
1134 elif tag == "get_static_prefix":
1135 templateText = '{% get_static_prefix %}'
1136 elif tag == "get_media_prefix":
1137 templateText = '{% get_media_prefix %}'
1138
1139 # TODO: add comment functions
1140 # TODO: add internationalization tags
617 #################################################### 1141 ####################################################
618 ## Fallback: return just the tag name ## 1142 ## Fallback: return just the tag name ##
619 #################################################### 1143 ####################################################
620 1144
621 else: 1145 else:

eric ide

mercurial