def _render_pages(self): public_pages = [page for page in self.pages if page.is_public] templating_engine = \ get_templating_cls(self.settings['templating_engine']) template_cls = templating_engine(self.settings) for page in self.pages: if 'rendered' in page: logging.warning('Skipping already rendered page %s', page) continue page['output_path'] = self._get_output_path(page['url']) # render template if page['template'] == 'self': render_func = template_cls.render_string template = page['content'] else: render_func = template_cls.render_file template = page['template'] try: logging.debug('About to render "%s".', page['output_path']) params = page['params'] if 'params' in page else {} page['rendered'] = render_func(template, page=page, pages=public_pages, settings=self.settings, thot_version=thot_version, **params) assert isinstance(page['rendered'], str) except TemplateException as error: logging.error(error) logging.error('skipping article "%s"', page['path']) continue for proc in self.processors_for('after_rendering'): proc.after_rendering(page)
def _render_pages(self): public_pages = [page for page in self.pages if page.is_public] templating_engine = get_templating_cls(self.settings['templating_engine']) template_cls = templating_engine(self.settings) for page in self.pages: if 'rendered' in page: logging.warning('Page %s has already been rendered, skipping', page) continue page['output_path'] = self._get_output_path(page['url']) # render template if page['template'] == 'self': render_func = template_cls.render_string template = page['content'] else: render_func = template_cls.render_file template = page['template'] try: logging.debug('About to render "%s".', page['output_path'].decode('utf-8')) params = page['params'] if 'params' in page else {} page['rendered'] = render_func(template, page=page, pages=public_pages, settings=self.settings, thot_version=thot_version, **params) assert type(page['rendered']) == types.UnicodeType except TemplateException as error: logging.error(error) logging.error('skipping article "%s"', page['path']) continue for proc in self.processors_for('after_rendering'): proc.after_rendering(page)
def main(): parser = OptionParser(version="%prog " + version) parser.add_option('--quickstart', help="quickstart a thot site", action="store_true", dest="quickstart") parser.add_option('--logging', help="sets the logging level. 'info' (default) or 'debug'") parser.add_option('--hardlinks', action="store_true", help="instead of copying static files, creates hardlinks" \ + " - which is faster and saves space") parser.add_option('-z', '--gzip', action="store_true", help="make a gzip-compressed copy of rendered files") parser.add_option('-t', '--templating', default='mako', dest='templating_engine', help="templating engine (e.g. jinja2, mako) for output") parser.add_option('-s', '--source', default='filesystem', help="data source, e.g. 'filesystem' for files") options, args = parser.parse_args() try: project_dir = abspath(args[0]) except IndexError: project_dir = abspath(getcwd()) settings = {'project_dir': project_dir, 'output_dir': join(project_dir, '_output'), 'template_dir': join(project_dir, '_templates'), 'lib_dir': join(project_dir, '_lib'), 'url_path': join(project_dir, '_lib', 'urls.py'), 'settings_path': join(project_dir, '_config.yml'), 'hardlinks': options.hardlinks, 'make_compressed_copy': options.gzip, 'compress_if_ending': GZIP_ENDINGS, 'templating_engine': options.templating_engine, 'source': options.source, 'build_tz': pytz.timezone(time.strftime("%Z", time.gmtime())), 'build_time': pytz.utc.localize(datetime.utcnow())} # configure logging logging_level = LOGGING_LEVELS.get(options.logging, logging.INFO) if has_logformatter: color = False if sys.stderr.isatty(): try: curses.setupterm() if curses.tigetnum('colors') > 0: color = True except: pass root_logger = logging.getLogger() root_logger.setLevel(logging_level) channel = logging.StreamHandler() channel.setFormatter(_LogFormatter(color=color)) root_logger.addHandler(channel) else: logging.basicConfig(level=logging_level, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') # quickstart if options.quickstart: quickstart(settings) settings['build_time'] = pytz.utc.localize(datetime.utcnow()) print '\nYour website will be available at %s' % settings['output_dir'] # read settings file if exists(settings['settings_path']): with open(settings['settings_path'], 'rb', encoding='utf-8') as configfile: config = yaml.safe_load(configfile.read()) settings.update(config['pyll'] if 'pyll' in config else config['thot']) logging.debug('settings %s', settings) # check and find the user's timezone if not 'timezone' in settings: settings['timezone'] = 'UTC' logging.warn('No timezone has been set. Assuming all dates are in "%s".', settings['timezone']) elif settings['timezone'] not in pytz.all_timezones_set: logging.error('Timezone "%s" is unknown (not in pytz.all_timezones_set).' + ' Try "Europe/Berlin", "UTC" or "US/Pacific" for example.', settings['timezone']) sys.exit(1) settings['timezone'] = pytz.timezone(settings['timezone']) # find the data source for entrypoint in pkg_resources.iter_entry_points('thot.sources'): if entrypoint.name == settings['source']: source_cls = entrypoint.load() break else: logging.error('Data source "%s" could not be found.', settings['source']) sys.exit(1) # initialize site source = source_cls(settings['project_dir'], settings['build_time'], settings['timezone'], settings['default_template'] if 'default_template' in settings \ else get_templating_cls(settings['templating_engine']).default_template, settings['page_defaults'] if 'page_defaults' in settings else dict()) site = Site(settings, source) if True: site.run() if options.hardlinks: print "Keep in mind: Output directory contains hardlinks."
def main(): parser = OptionParser(version='%prog ' + version) parser.add_option('--quickstart', help='quickstart a thot site', action='store_true', dest='quickstart') parser.add_option( '--logging', help='sets the logging level. "info" (default) or "debug"') parser.add_option( '--hardlinks', action='store_true', help='instead of copying static files, creates hardlinks' \ + ' - which is faster and saves space') parser.add_option('-z', '--gzip', action='store_true', help='make a gzip-compressed copy of rendered files') parser.add_option('-t', '--templating', default='mako', dest='templating_engine', help='templating engine (e.g. jinja2, mako) for output') parser.add_option('-s', '--source', default='filesystem', help='data source, e.g. "filesystem" for files') options, args = parser.parse_args() try: project_dir = abspath(args[0]) except IndexError: project_dir = abspath(getcwd()) settings = { 'project_dir': project_dir, 'output_dir': join(project_dir, '_output'), 'template_dir': join(project_dir, '_templates'), 'lib_dir': join(project_dir, '_lib'), 'url_path': join(project_dir, '_lib', 'urls.py'), 'settings_path': join(project_dir, '_config.yml'), 'hardlinks': options.hardlinks, 'make_compressed_copy': options.gzip, 'compress_if_ending': GZIP_ENDINGS, 'templating_engine': options.templating_engine, 'source': options.source, 'build_tz': pytz.timezone(time.strftime('%Z', time.gmtime())), 'build_time': pytz.utc.localize(datetime.utcnow()), } # configure logging logging_level = LOGGING_LEVELS.get(options.logging, logging.INFO) if has_logformatter: color = False if sys.stderr.isatty(): try: curses.setupterm() if curses.tigetnum('colors') > 0: color = True except: # pylint: disable=bare-except pass root_logger = logging.getLogger() root_logger.setLevel(logging_level) channel = logging.StreamHandler() channel.setFormatter(_LogFormatter(color=color)) root_logger.addHandler(channel) else: logging.basicConfig(level=logging_level, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S') # quickstart if options.quickstart: u = quickstart(settings) settings.update(u) settings['build_time'] = pytz.utc.localize(datetime.utcnow()) print('\nYour website will written to %s' % settings['output_dir']) sys.stdout.flush() # read settings file if exists(settings['settings_path']): with codecs.open(settings['settings_path'], 'rb', encoding='utf-8') as configfile: config = yaml.safe_load(configfile.read()) settings.update(config['pyll'] if 'pyll' in config else config['thot']) else: logging.error('Not found: %s', settings['settings_path']) sys.exit(1) logging.debug('settings %s', settings) # check and find the user's timezone if 'timezone' not in settings: settings['timezone'] = 'UTC' logging.warning( 'No timezone has been set. Assuming all dates are in "%s".', settings['timezone']) elif settings['timezone'] not in pytz.all_timezones_set: logging.error( # pylint: disable=logging-not-lazy 'Timezone "%s" is absent from pytz.all_timezones_set.' + ' Try "Europe/Berlin", "UTC" or "US/Pacific".', settings['timezone']) sys.exit(1) settings['timezone'] = pytz.timezone(settings['timezone']) # find the data source for entrypoint in pkg_resources.iter_entry_points('thot.sources'): if entrypoint.name == settings['source']: source_cls = entrypoint.load() break else: logging.error('Data source "%s" could not be found.', settings['source']) sys.exit(1) # initialize site source = source_cls( settings['project_dir'], settings['build_time'], settings['timezone'], settings['default_template'] \ if 'default_template' in settings \ else get_templating_cls(settings['templating_engine'])\ .default_template, settings['page_defaults'] if 'page_defaults' in settings else dict(), ) site = Site(settings, source) site.run() if options.hardlinks: print('Keep in mind: Output directory contains hardlinks.')