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.
 
 
 

399 lines
12 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Parses & holds information about the site to be generated.
  4. """
  5. import os
  6. from hyde.exceptions import HydeException
  7. from hyde.fs import FS, File, Folder
  8. from hyde.model import Config
  9. from hyde.util import getLoggerWithNullHandler
  10. logger = getLoggerWithNullHandler('hyde.engine')
  11. class Processable(object):
  12. """
  13. A node or resource.
  14. """
  15. def __init__(self, source):
  16. super(Processable, self).__init__()
  17. self.source = FS.file_or_folder(source)
  18. self.is_processable = True
  19. self.uses_template = True
  20. @property
  21. def name(self):
  22. """
  23. The resource name
  24. """
  25. return self.source.name
  26. def __repr__(self):
  27. return self.path
  28. @property
  29. def path(self):
  30. """
  31. Gets the source path of this node.
  32. """
  33. return self.source.path
  34. class Resource(Processable):
  35. """
  36. Represents any file that is processed by hyde
  37. """
  38. def __init__(self, source_file, node):
  39. super(Resource, self).__init__(source_file)
  40. self.source_file = source_file
  41. if not node:
  42. raise HydeException("Resource cannot exist without a node")
  43. if not source_file:
  44. raise HydeException("Source file is required"
  45. " to instantiate a resource")
  46. self.node = node
  47. self.site = node.site
  48. self._relative_deploy_path = None
  49. @property
  50. def relative_path(self):
  51. """
  52. Gets the path relative to the root folder (Content)
  53. """
  54. return self.source_file.get_relative_path(self.node.root.source_folder)
  55. @property
  56. def slug(self):
  57. #TODO: Add a more sophisticated slugify method
  58. return self.source.name_without_extension
  59. def get_relative_deploy_path(self):
  60. """
  61. Gets the path where the file will be created
  62. after its been processed.
  63. """
  64. return self._relative_deploy_path \
  65. if self._relative_deploy_path \
  66. else self.relative_path
  67. def set_relative_deploy_path(self, path):
  68. """
  69. Sets the path where the file ought to be created
  70. after its been processed.
  71. """
  72. self._relative_deploy_path = path
  73. self.site.content.resource_deploy_path_changed(self)
  74. relative_deploy_path = property(get_relative_deploy_path, set_relative_deploy_path)
  75. url = relative_deploy_path
  76. @property
  77. def full_url(self):
  78. """
  79. Returns the full url for the resource.
  80. """
  81. return self.site.full_url(self.relative_path)
  82. class Node(Processable):
  83. """
  84. Represents any folder that is processed by hyde
  85. """
  86. def __init__(self, source_folder, parent=None):
  87. super(Node, self).__init__(source_folder)
  88. if not source_folder:
  89. raise HydeException("Source folder is required"
  90. " to instantiate a node.")
  91. self.root = self
  92. self.module = None
  93. self.site = None
  94. self.source_folder = Folder(str(source_folder))
  95. self.parent = parent
  96. if parent:
  97. self.root = self.parent.root
  98. self.module = self.parent.module if self.parent.module else self
  99. self.site = parent.site
  100. self.child_nodes = []
  101. self.resources = []
  102. def contains_resource(self, resource_name):
  103. """
  104. Returns True if the given resource name exists as a file
  105. in this node's source folder.
  106. """
  107. return File(self.source_folder.child(resource_name)).exists
  108. def get_resource(self, resource_name):
  109. """
  110. Gets the resource if the given resource name exists as a file
  111. in this node's source folder.
  112. """
  113. if self.contains_resource(resource_name):
  114. return self.root.resource_from_path(
  115. self.source_folder.child(resource_name))
  116. return None
  117. def add_child_node(self, folder):
  118. """
  119. Creates a new child node and adds it to the list of child nodes.
  120. """
  121. if folder.parent != self.source_folder:
  122. raise HydeException("The given folder [%s] is not a"
  123. " direct descendant of [%s]" %
  124. (folder, self.source_folder))
  125. node = Node(folder, self)
  126. self.child_nodes.append(node)
  127. return node
  128. def add_child_resource(self, afile):
  129. """
  130. Creates a new resource and adds it to the list of child resources.
  131. """
  132. if afile.parent != self.source_folder:
  133. raise HydeException("The given file [%s] is not"
  134. " a direct descendant of [%s]" %
  135. (afile, self.source_folder))
  136. resource = Resource(afile, self)
  137. self.resources.append(resource)
  138. return resource
  139. def walk(self):
  140. """
  141. Walks the node, first yielding itself then
  142. yielding the child nodes depth-first.
  143. """
  144. yield self
  145. for child in self.child_nodes:
  146. for node in child.walk():
  147. yield node
  148. def walk_resources(self):
  149. """
  150. Walks the resources in this hierarchy.
  151. """
  152. for node in self.walk():
  153. for resource in node.resources:
  154. yield resource
  155. @property
  156. def relative_path(self):
  157. """
  158. Gets the path relative to the root folder (Content, Media, Layout)
  159. """
  160. return self.source_folder.get_relative_path(self.root.source_folder)
  161. @property
  162. def url(self):
  163. return '/' + self.relative_path
  164. @property
  165. def full_url(self):
  166. return self.site.full_url(self.relative_path)
  167. class RootNode(Node):
  168. """
  169. Represents one of the roots of site: Content, Media or Layout
  170. """
  171. def __init__(self, source_folder, site):
  172. super(RootNode, self).__init__(source_folder)
  173. self.site = site
  174. self.node_map = {}
  175. self.node_deploy_map = {}
  176. self.resource_map = {}
  177. self.resource_deploy_map = {}
  178. def node_from_path(self, path):
  179. """
  180. Gets the node that maps to the given path.
  181. If no match is found it returns None.
  182. """
  183. if Folder(path) == self.source_folder:
  184. return self
  185. return self.node_map.get(str(Folder(path)), None)
  186. def node_from_relative_path(self, relative_path):
  187. """
  188. Gets the content node that maps to the given relative path.
  189. If no match is found it returns None.
  190. """
  191. return self.node_from_path(
  192. self.source_folder.child(str(relative_path)))
  193. def resource_from_path(self, path):
  194. """
  195. Gets the resource that maps to the given path.
  196. If no match is found it returns None.
  197. """
  198. return self.resource_map.get(str(File(path)), None)
  199. def resource_from_relative_path(self, relative_path):
  200. """
  201. Gets the content resource that maps to the given relative path.
  202. If no match is found it returns None.
  203. """
  204. return self.resource_from_path(
  205. self.source_folder.child(str(relative_path)))
  206. def resource_deploy_path_changed(self, resource):
  207. """
  208. Handles the case where the relative deploy path of a
  209. resource has changed.
  210. """
  211. self.resource_deploy_map[str(resource.relative_deploy_path)] = resource
  212. def resource_from_relative_deploy_path(self, relative_deploy_path):
  213. """
  214. Gets the content resource whose deploy path maps to
  215. the given relative path. If no match is found it returns None.
  216. """
  217. if relative_deploy_path in self.resource_deploy_map:
  218. return self.resource_deploy_map[relative_deploy_path]
  219. return self.resource_from_relative_path(relative_deploy_path)
  220. def add_node(self, a_folder):
  221. """
  222. Adds a new node to this folder's hierarchy.
  223. Also adds to to the hashtable of path to node associations
  224. for quick lookup.
  225. """
  226. folder = Folder(a_folder)
  227. node = self.node_from_path(folder)
  228. if node:
  229. logger.debug("Node exists at [%s]" % node.relative_path)
  230. return node
  231. if not folder.is_descendant_of(self.source_folder):
  232. raise HydeException("The given folder [%s] does not"
  233. " belong to this hierarchy [%s]" %
  234. (folder, self.source_folder))
  235. p_folder = folder
  236. parent = None
  237. hierarchy = []
  238. while not parent:
  239. hierarchy.append(p_folder)
  240. p_folder = p_folder.parent
  241. parent = self.node_from_path(p_folder)
  242. hierarchy.reverse()
  243. node = parent if parent else self
  244. for h_folder in hierarchy:
  245. node = node.add_child_node(h_folder)
  246. self.node_map[str(h_folder)] = node
  247. logger.debug("Added node [%s] to [%s]" % (
  248. node.relative_path, self.source_folder))
  249. return node
  250. def add_resource(self, a_file):
  251. """
  252. Adds a file to the parent node. Also adds to to the
  253. hashtable of path to resource associations for quick lookup.
  254. """
  255. afile = File(a_file)
  256. resource = self.resource_from_path(afile)
  257. if resource:
  258. logger.debug("Resource exists at [%s]" % resource.relative_path)
  259. return resource
  260. if not afile.is_descendant_of(self.source_folder):
  261. raise HydeException("The given file [%s] does not reside"
  262. " in this hierarchy [%s]" %
  263. (afile, self.source_folder))
  264. node = self.node_from_path(afile.parent)
  265. if not node:
  266. node = self.add_node(afile.parent)
  267. resource = node.add_child_resource(afile)
  268. self.resource_map[str(afile)] = resource
  269. logger.debug("Added resource [%s] to [%s]" %
  270. (resource.relative_path, self.source_folder))
  271. return resource
  272. def load(self):
  273. """
  274. Walks the `source_folder` and loads the sitemap.
  275. Creates nodes and resources, reads metadata and injects attributes.
  276. This is the model for hyde.
  277. """
  278. if not self.source_folder.exists:
  279. raise HydeException("The given source folder [%s]"
  280. " does not exist" % self.source_folder)
  281. with self.source_folder.walker as walker:
  282. @walker.folder_visitor
  283. def visit_folder(folder):
  284. self.add_node(folder)
  285. @walker.file_visitor
  286. def visit_file(afile):
  287. self.add_resource(afile)
  288. class Site(object):
  289. """
  290. Represents the site to be generated.
  291. """
  292. def __init__(self, sitepath=None, config=None):
  293. super(Site, self).__init__()
  294. self.sitepath = Folder(Folder(sitepath).fully_expanded_path)
  295. self.config = config if config else Config(self.sitepath)
  296. self.content = RootNode(self.config.content_root_path, self)
  297. self.plugins = []
  298. self.context = {}
  299. def load(self):
  300. """
  301. Walks the content and media folders to load up the sitemap.
  302. """
  303. self.content.load()
  304. def content_url(self, path):
  305. """
  306. Returns the content url by appending the base url from the config
  307. with the given path.
  308. """
  309. return Folder(self.config.base_url).child(path).replace(os.sep, '/')
  310. def media_url(self, path):
  311. """
  312. Returns the media url by appending the media base url from the config
  313. with the given path.
  314. """
  315. return Folder(self.config.media_url).child(path).replace(os.sep, '/')
  316. def full_url(self, path):
  317. """
  318. Determines if the given path is media or content based on the
  319. configuration and returns the appropriate url.
  320. """
  321. if self.is_media(path):
  322. return self.media_url(
  323. FS(path).get_relative_path(
  324. self.config.media_root_path))
  325. else:
  326. return self.content_url(path)
  327. def is_media(self, path):
  328. """
  329. Given the relative path, determines if it is content or media.
  330. """
  331. folder = self.content.source.child_folder(path)
  332. return folder.is_descendant_of(self.config.media_root_path)