ink.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. #------------------------------------------------------------------------------
  4. # The Ink Templating System
  5. # A lightweight, fast, flexible text templating system
  6. # Copyright 2014 Christopher Simpkins
  7. # MIT License
  8. #------------------------------------------------------------------------------
  9. import re
  10. #------------------------------------------------------------------------------
  11. # Template class
  12. # A template string class that is inherited from Python str
  13. # Includes metadata about the template:
  14. # odel = opening delimiter
  15. # cdel = closing delimiter
  16. # varlist = inclusive list of all variables in the template text (parsed in constructor)
  17. # Delimiters:
  18. # default = {{variable}}
  19. # assign new opening and closing delimiters as parameters when you make a new Template instance
  20. # `escape_regex` boolean is a speedup, avoids Python escape of special regex chars if you do not need it
  21. #------------------------------------------------------------------------------
  22. class Template(str):
  23. def __new__(cls, template_text, open_delimiter="{{", close_delimiter="}}", escape_regex=False):
  24. obj = str.__new__(cls, template_text)
  25. obj.odel = open_delimiter
  26. obj.cdel = close_delimiter
  27. obj.varlist = obj._make_var_list(template_text, escape_regex) #contains all unique parsed variables from the template in a list
  28. return obj
  29. #------------------------------------------------------------------------------
  30. # [ _make_var_list method ] (list of strings)
  31. # Private method that parses the template string for all variables that match the delimiter pattern
  32. # Returns a list of the variable names as strings
  33. #------------------------------------------------------------------------------
  34. def _make_var_list(self, template_text, escape_regex=False):
  35. if escape_regex:
  36. open_match_pat = self._escape_regex_special_chars(self.odel)
  37. close_match_pat = self._escape_regex_special_chars(self.cdel)
  38. match_pat = open_match_pat + r'(.*?)' + close_match_pat # capture group contains the variable name used between the opening and closing delimiters
  39. else:
  40. match_pat = self.odel + r'(.*?)' + self.cdel
  41. var_list = re.findall(match_pat, template_text) #generate a list that contains the capture group from the matches (i.e. the variables in the template)
  42. return set(var_list) # remove duplicate entries by converting to set (and lookup speed improvement from hashing)
  43. #------------------------------------------------------------------------------
  44. # [ _escape_regex_special_chars method ] (string)
  45. # Private method that escapes special regex metacharacters
  46. # Returns a string with the escaped character modifications
  47. #------------------------------------------------------------------------------
  48. def _escape_regex_special_chars(self, test_escape_string):
  49. return re.escape(test_escape_string)
  50. #------------------------------------------------------------------------------
  51. # Renderer class
  52. # Render the variable replacements in the ink template using a Python dictionary key argument
  53. # Construct the instace of the Renderer with the Ink template and the dictionary key
  54. # Run the renderer with the render method on the instance (e.g. r.render())
  55. # Parameters to constructor:
  56. # - template = an Ink Template instance
  57. # - key = a dictionary mapped key = variable name : value = variable replacement data
  58. # - html_entities = encode html entities with HTML escaped characters (default = False = do not encode)
  59. #------------------------------------------------------------------------------
  60. class Renderer:
  61. def __init__(self, template, key, html_entities=False):
  62. self.odel = template.odel
  63. self.cdel = template.cdel
  64. self.template = template
  65. self.html_entities = html_entities
  66. self.key_dict = key
  67. #------------------------------------------------------------------------------
  68. # [ render method ] (string)
  69. # renders the variable replacements in the Ink template
  70. # returns the rendered template as a string
  71. #------------------------------------------------------------------------------
  72. def render(self):
  73. # make local variables for the loop below (faster)
  74. local_dict = self.key_dict
  75. local_template = self.template
  76. local_varlist = self.template.varlist
  77. local_odel = self.odel
  78. local_cdel = self.cdel
  79. local_htmlent = self.html_entities
  80. if local_htmlent:
  81. from xml.sax.saxutils import escape #from Python std lib
  82. for key in local_dict:
  83. if key in local_varlist:
  84. value = local_dict[key]
  85. replace_string = local_odel + key + local_cdel
  86. if local_htmlent:
  87. value = escape(value) #xml.sax.saxutils function
  88. local_template = local_template.replace(replace_string, value)
  89. return local_template
  90. ##TODO : multiple file render method?
  91. if __name__ == '__main__':
  92. pass
  93. # template = Template("This is a of the {{test}} of the {{document}} {{type}} and more of the {{test}} {{document}} {{type}}")
  94. # renderer = Renderer(template, {'test': 'ব য', 'document':'testing document', 'type':'of mine', 'bogus': 'bogus test'})
  95. # print(renderer.render())