A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

123 lines
3.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # pylint: disable-msg=W0104,E0602,W0613,R0201
  3. """
  4. Abstract classes and utilities for template engines
  5. """
  6. from hyde.exceptions import HydeException
  7. from hyde.util import getLoggerWithNullHandler
  8. class Template(object):
  9. """
  10. Interface for hyde template engines. To use a different template engine,
  11. the following interface must be implemented.
  12. """
  13. def __init__(self, sitepath):
  14. self.sitepath = sitepath
  15. self.logger = getLoggerWithNullHandler(self.__class__.__name__)
  16. def configure(self, config, preprocessor=None, postprocessor=None):
  17. """
  18. The config object is a simple YAML object with required settings. The
  19. template implementations are responsible for transforming this object
  20. to match the `settings` required for the template engines.
  21. The preprocessor and postprocessor contain the fucntions that
  22. trigger the hyde plugins to preprocess the template after load
  23. and postprocess it after it is processed and code is generated.
  24. Note that the processor must only be used when referencing templates,
  25. for example, using the include tag. The regular preprocessing and
  26. post processing logic is handled by hyde.
  27. """
  28. abstract
  29. def get_dependencies(self, text):
  30. """
  31. Finds the dependencies based on the included
  32. files.
  33. """
  34. return None
  35. def render(self, text, context):
  36. """
  37. Given the text, and the context, this function must return the
  38. rendered string.
  39. """
  40. abstract
  41. @property
  42. def exception_class(self):
  43. return HydeException
  44. @property
  45. def include_pattern(self):
  46. """
  47. The pattern for matching include statements
  48. """
  49. return '\s*\{\%\s*include\s*(?:\'|\")(.+?\.[^.]*)(?:\'|\")\s*\%\}'
  50. def get_include_statement(self, path_to_include):
  51. """
  52. Returns an include statement for the current template,
  53. given the path to include.
  54. """
  55. return "{%% include '%s' %%}" % path_to_include
  56. @property
  57. def block_open_pattern(self):
  58. """
  59. The pattern for matching include statements
  60. """
  61. return '\s*\{\%\s*block\s*([^\s]+)\s*\%\}'
  62. @property
  63. def block_close_pattern(self):
  64. """
  65. The pattern for matching include statements
  66. """
  67. return '\s*\{\%\s*endblock\s*([^\s]*)\s*\%\}'
  68. def get_block_open_statement(self, block_name):
  69. """
  70. Returns a open block statement for the current template,
  71. given the block name.
  72. """
  73. return "{%% block %s %%}" % block_name
  74. def get_block_close_statement(self, block_name):
  75. """
  76. Returns a close block statement for the current template,
  77. given the block name.
  78. """
  79. return "{%% endblock %s %%}" % block_name
  80. @property
  81. def extends_pattern(self):
  82. """
  83. The pattern for matching include statements
  84. """
  85. return '\s*\{\%\s*extends\s*(?:\'|\")(.+?\.[^.]*)(?:\'|\")\s*\%\}'
  86. def get_extends_statement(self, path_to_extend):
  87. """
  88. Returns an extends statement for the current template,
  89. given the path to extend.
  90. """
  91. return "{%% extends '%s' %%}" % path_to_extend
  92. @staticmethod
  93. def find_template(site):
  94. """
  95. Reads the configuration to find the appropriate template.
  96. """
  97. # TODO: Find the appropriate template environment
  98. from hyde.ext.templates.jinja import Jinja2Template
  99. template = Jinja2Template(site.sitepath)
  100. return template