def _write_items(self, items, template_path): """Writes Page objects using the given template file (PRIVATE). items -- List of Page objects to be written. template_path -- Template file name, must exist in the defined template directory. """ template_env = CONFIG.SITE.TEMPLATE_ENV template_file = os.path.basename(template_path) # get template from cache if it's already loaded if template_file not in self._templates: template = template_env.get_template(template_file) self._templates[template_file] = template else: template = self._templates[template_file] for item in items: # warn if files are overwritten # this indicates a duplicate post, which could result in # unexpected results if os.path.exists(item.path): message = "File %s already exists. Make sure there are no "\ "other entries leading to this file path." % item.path self.logger.error(message) raise IOError(message) else: rendered = template.render(page=item, CONFIG=CONFIG, \ widgets=self.widgets) if sys.version_info[0] < 3: rendered = rendered.encode('utf-8') write_file(item.path, rendered)
def write_site_pages(self): """Write site pages, such as a separate index.html or 404.html.""" for filename in self.config.SITE.PAGES: message = "Writing site page: '%s'" % filename console(message) self.logger.debug(message) template = self.config.SITE.TEMPLATE_ENV.get_template(filename) path = os.path.join(self.config.VOLT.SITE_DIR, filename) if os.path.exists(path): message = "File %s already exists. Make sure there are no "\ "other entries leading to this file path." % path console("Error: %s" % message, is_bright=True, color='red') self.logger.error(message) sys.exit(1) rendered = template.render(page={}, CONFIG=self.config, \ widgets=self.widgets) if sys.version_info[0] < 3: rendered = rendered.encode('utf-8') write_file(path, rendered)