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.
 
 
 

105 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Implements the hyde entry point commands
  4. """
  5. from commando import *
  6. from hyde.exceptions import HydeException
  7. from hyde.fs import File, Folder
  8. from hyde.layout import Layout, HYDE_DATA
  9. from hyde.model import Config
  10. from hyde.site import Site
  11. from hyde.version import __version__
  12. import logging
  13. import os
  14. import yaml
  15. HYDE_LAYOUTS = "HYDE_LAYOUTS"
  16. logger = logging.getLogger('hyde.engine')
  17. logger.setLevel(logging.DEBUG)
  18. import sys
  19. logger.addHandler(logging.StreamHandler(sys.stdout))
  20. class Engine(Application):
  21. """
  22. The Hyde Application
  23. """
  24. @command(description='hyde - a python static website generator',
  25. epilog='Use %(prog)s {command} -h to get help on individual commands')
  26. @version('-v', '--version', version='%(prog)s ' + __version__)
  27. @store('-s', '--sitepath', default='.', help="Location of the hyde site")
  28. def main(self, args):
  29. """
  30. Will not be executed. A sub command is required. This function exists to provide
  31. common parameters for the subcommands and some generic stuff like version and
  32. metadata
  33. """
  34. pass
  35. @subcommand('create', help='Create a new hyde site')
  36. @store('-l', '--layout', default='basic', help='Layout for the new site')
  37. @true('-f', '--force', default=False, dest='overwrite',
  38. help='Overwrite the current site if it exists')
  39. def create(self, args):
  40. """
  41. The create command. Creates a new site from the template at the given
  42. sitepath.
  43. """
  44. sitepath = Folder(args.sitepath)
  45. if sitepath.exists and not args.overwrite:
  46. raise HydeException("The given site path[%s] is not empty" % sitepath)
  47. layout = Layout.find_layout(args.layout)
  48. logger.info("Creating site at [%s] with layout [%s]" % (sitepath, layout))
  49. if not layout or not layout.exists:
  50. raise HydeException(
  51. "The given layout is invalid. Please check if you have the `layout` "
  52. "in the right place and the environment variable(%s) has been setup "
  53. "properly if you are using custom path for layouts" % HYDE_DATA)
  54. layout.copy_contents_to(args.sitepath)
  55. logger.info("Site creation complete")
  56. @subcommand('gen', help='Generate the site')
  57. @store('-c', '--config-path', default='site.yaml', dest='config',
  58. help='The configuration used to generate the site')
  59. @store('-d', '--deploy-path', default='deploy', help='Where should the site be generated?')
  60. def gen(self, args):
  61. """
  62. The generate command. Generates the site at the given deployment directory.
  63. """
  64. sitepath = Folder(args.sitepath)
  65. logger.info("Generating site at [%s]" % sitepath)
  66. # Read the configuration
  67. config_file = sitepath.child(args.config)
  68. logger.info("Reading site configuration from [%s]", config_file)
  69. conf = {}
  70. with open(config_file) as stream:
  71. conf = yaml.load(stream)
  72. site = Site(sitepath, Config(sitepath, conf))
  73. # TODO: Find the appropriate template environment
  74. from hyde.ext.templates.jinja import Jinja2Template
  75. template = Jinja2Template(sitepath)
  76. logger.info("Using [%s] as the template", template)
  77. # Configure the environment
  78. logger.info("Configuring Template environment")
  79. template.configure(site.config)
  80. # Prepare site info
  81. logger.info("Analyzing site contents")
  82. site.build()
  83. context = dict(site=site)
  84. # Generate site one file at a time
  85. logger.info("Generating site to [%s]" % site.config.deploy_root_path)
  86. for page in site.content.walk_resources():
  87. logger.info("Processing [%s]", page)
  88. target = File(page.source_file.get_mirror(site.config.deploy_root_path, site.content.source_folder))
  89. target.parent.make()
  90. if page.source_file.is_text:
  91. logger.info("Rendering [%s]", page)
  92. context.update(page=page)
  93. text = template.render(page.source_file.read_all(), context)
  94. target.write(text)
  95. else:
  96. logger.info("Copying binary file [%s]", page)
  97. page.source_file.copy_to(target)