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.
 
 
 

164 lines
4.3 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. import abc
  9. class HtmlWrap(object):
  10. """
  11. A wrapper class for raw html.
  12. Provides pyquery interface if available.
  13. Otherwise raw html access.
  14. """
  15. def __init__(self, html):
  16. super(HtmlWrap, self).__init__()
  17. self.raw = html
  18. try:
  19. from pyquery import PyQuery
  20. except:
  21. PyQuery = False
  22. if PyQuery:
  23. self.q = PyQuery(html)
  24. def __unicode__(self):
  25. return self.raw
  26. def __call__(self, selector=None):
  27. if not self.q:
  28. return self.raw
  29. return self.q(selector).html()
  30. class Template(object):
  31. """
  32. Interface for hyde template engines. To use a different template engine,
  33. the following interface must be implemented.
  34. """
  35. __metaclass__ = abc.ABCMeta
  36. def __init__(self, sitepath):
  37. self.sitepath = sitepath
  38. self.logger = getLoggerWithNullHandler(self.__class__.__name__)
  39. @abc.abstractmethod
  40. def configure(self, site, engine):
  41. """
  42. The site object should contain a config attribute. The config object
  43. is a simple YAML object with required settings. The template
  44. implementations are responsible for transforming this object to match
  45. the `settings` required for the template engines.
  46. The engine is an informal protocol to provide access to some
  47. hyde internals.
  48. The preprocessor attribute must contain the function that trigger the
  49. hyde plugins to preprocess the template after load.
  50. A context_for_path attribute must contain the function that returns
  51. the context object that is populated with the appropriate variables
  52. for the given path.
  53. """
  54. return
  55. def clear_caches(self):
  56. """
  57. Clear all caches to prepare for regeneration
  58. """
  59. return
  60. def get_dependencies(self, text):
  61. """
  62. Finds the dependencies based on the included
  63. files.
  64. """
  65. return None
  66. @abc.abstractmethod
  67. def render_resource(self, resource, context):
  68. """
  69. This function must load the file represented by the resource
  70. object and return the rendered text.
  71. """
  72. return ''
  73. @abc.abstractmethod
  74. def render(self, text, context):
  75. """
  76. Given the text, and the context, this function must return the
  77. rendered string.
  78. """
  79. return ''
  80. @abc.abstractproperty
  81. def exception_class(self):
  82. return HydeException
  83. @abc.abstractproperty
  84. def patterns(self):
  85. """
  86. Patterns for matching selected template statements.
  87. """
  88. return {}
  89. @abc.abstractmethod
  90. def get_include_statement(self, path_to_include):
  91. """
  92. Returns an include statement for the current template,
  93. given the path to include.
  94. """
  95. return '{%% include \'%s\' %%}' % path_to_include
  96. @abc.abstractmethod
  97. def get_extends_statement(self, path_to_extend):
  98. """
  99. Returns an extends statement for the current template,
  100. given the path to extend.
  101. """
  102. return '{%% extends \'%s\' %%}' % path_to_extend
  103. @abc.abstractmethod
  104. def get_open_tag(self, tag, params):
  105. """
  106. Returns an open tag statement.
  107. """
  108. return '{%% %s %s %%}' % (tag, params)
  109. @abc.abstractmethod
  110. def get_close_tag(self, tag, params):
  111. """
  112. Returns an open tag statement.
  113. """
  114. return '{%% end%s %%}' % tag
  115. @abc.abstractmethod
  116. def get_content_url_statement(self, url):
  117. """
  118. Returns the content url statement.
  119. """
  120. return '/' + url
  121. @abc.abstractmethod
  122. def get_media_url_statement(self, url):
  123. """
  124. Returns the media url statement.
  125. """
  126. return '/media/' + url
  127. @staticmethod
  128. def find_template(site):
  129. """
  130. Reads the configuration to find the appropriate template.
  131. """
  132. # TODO: Find the appropriate template environment
  133. from hyde.ext.templates.jinja import Jinja2Template
  134. template = Jinja2Template(site.sitepath)
  135. return template