def __init__(self, input_path, **kw): # setting up paths self.paths = OMD() self._paths = OMD() # for the raw input paths self.fal = ChertFAL(chlog) set_path = self._set_path set_path('input_path', input_path) set_path('config_path', kw.pop('config_path', None), DEFAULT_CONFIG_FILENAME) set_path('entries_path', kw.pop('entries_path', None), 'entries') set_path('themes_path', kw.pop('themes_path', None), 'themes') set_path('uploads_path', kw.pop('uploads_path', None), 'uploads', required=False) set_path('output_path', kw.pop('output_path', None), 'site', required=False) self.reload_config() self.reset() self.dev_mode = kw.pop('dev_mode', False) if kw: raise TypeError('unexpected keyword arguments: %r' % kw) chlog.debug('init site').success() return
def _set_path(self, name, path, default_suffix=None, required=True): """Set a path. Args: name: name of attribute (e.g., input_path) path: the path or None default_suffix: if path is None, self.input_path + default_suffix is used. The input_path should already be set. required: raise an error if path does not exist """ with chlog.debug('set {path_name} path to {path_val}', path_name=name, path_val=path) as rec: self._paths[name] = path if path: self.paths[name] = abspath(path) elif default_suffix: self.paths[name] = pjoin(self.input_path, default_suffix) else: raise ValueError('no path or default set for %r' % name) if required: if not os.path.exists(self.paths[name]): raise RuntimeError('expected existent %s path, not %r' % (name, self.paths[name])) rec.success('set {path_name} path to {path_val}', path_val=self.paths[name]) return
def reload_config(self, **kw): # TODO: take optional kwarg self.config = yaml.load(self.fal.read(self.paths['config_path'])) # set theme with chlog.debug('setting theme'): theme_name = self.get_config('theme', 'name') theme_path = pjoin(self.themes_path, theme_name) self._set_path('theme_path', theme_path)
def _load_custom_mod(self): input_path = self.paths['input_path'] custom_mod_path = pjoin(input_path, 'custom.py') if not os.path.exists(custom_mod_path): self.custom_mod = None return # site_name = os.path.split(input_path)[1] with chlog.debug('import site custom module'): mod_name = 'custom' self.custom_mod = imp.load_source(mod_name, custom_mod_path)
def render_parts(entry): for part in entry.loaded_parts: part['content_html'] = markdown2html(part['content']) part['content_ihtml'] = markdown2ihtml(part['content'], entry.output_filename) if not entry.summary: with chlog.debug('autosummarizing', reraise=False): entry.summary = entry._autosummarize() tmpl_name = entry.entry_layout + MD_LAYOUT_EXT render_ctx = { 'entry': entry.to_dict(with_links=False), 'site': site_info } entry.content_md = self.md_renderer.render(tmpl_name, render_ctx) tmpl_name = entry.content_layout + HTML_LAYOUT_EXT content_html = self.html_renderer.render(tmpl_name, render_ctx) with chlog.debug('parse_content_html'): content_html_tree = hypertext.html_text_to_tree(content_html) with chlog.debug('add_toc_content_html'): hypertext.add_toc(content_html_tree, make_anchor_id=self._make_anchor_id) with chlog.debug('retarget_links_content_html'): _mode = self.get_config('site', 'retarget_links', 'external') hypertext.retarget_links(content_html_tree, mode=_mode) with chlog.debug('reserialize_content_html'): content_html = hypertext.html_tree_to_text(content_html_tree) entry.content_html = content_html render_ctx['inline'] = True content_ihtml = self.html_renderer.render(tmpl_name, render_ctx) with chlog.debug('canonicalize_ihtml_links'): # TODO: use tree (and move slightly down) content_ihtml = hypertext.canonicalize_links( content_ihtml, canonical_domain, entry.output_filename) with chlog.debug('parse_content_ihtml'): content_ihtml_tree = hypertext.html_text_to_tree(content_ihtml) with chlog.debug('add_toc_content_ihtml'): hypertext.add_toc(content_ihtml_tree) with chlog.debug('reserialize_content_ihtml'): content_ihtml = hypertext.html_tree_to_text(content_ihtml_tree) entry.content_ihtml = content_ihtml return
def render_parts(entry): for part in entry.loaded_parts: part['content_html'] = markdown2html(part['content']) part['content_ihtml'] = markdown2ihtml(part['content'], entry.output_filename) if not entry.summary: with chlog.debug('autosummarizing', reraise=False): entry.summary = entry._autosummarize() tmpl_name = entry.entry_layout + MD_LAYOUT_EXT render_ctx = {'entry': entry.to_dict(with_links=False), 'site': site_info} entry.content_md = self.md_renderer.render(tmpl_name, render_ctx) tmpl_name = entry.content_layout + HTML_LAYOUT_EXT content_html = self.html_renderer.render(tmpl_name, render_ctx) with chlog.debug('parse_content_html'): content_html_tree = hypertext.html_text_to_tree(content_html) with chlog.debug('add_toc_content_html'): hypertext.add_toc(content_html_tree, make_anchor_id=self._make_anchor_id) with chlog.debug('retarget_links_content_html'): _mode = self.get_config('site', 'retarget_links', 'external') hypertext.retarget_links(content_html_tree, mode=_mode) with chlog.debug('reserialize_content_html'): content_html = hypertext.html_tree_to_text(content_html_tree) entry.content_html = content_html render_ctx['inline'] = True content_ihtml = self.html_renderer.render(tmpl_name, render_ctx) with chlog.debug('canonicalize_ihtml_links'): # TODO: use tree (and move slightly down) content_ihtml = hypertext.canonicalize_links(content_ihtml, canonical_domain, entry.output_filename) with chlog.debug('parse_content_ihtml'): content_ihtml_tree = hypertext.html_text_to_tree(content_ihtml) with chlog.debug('add_toc_content_ihtml'): hypertext.add_toc(content_ihtml_tree) with chlog.debug('reserialize_content_ihtml'): content_ihtml = hypertext.html_tree_to_text(content_ihtml_tree) entry.content_ihtml = content_ihtml return
def _call_custom_hook(self, hook_name): with chlog.debug('call custom {hook_name} hook', hook_name=hook_name, reraise=False) as rec: if not self.custom_mod: # TODO: success or failure? rec.failure('no custom module loaded') try: hook_func = getattr(self.custom_mod, 'chert_' + hook_name) except AttributeError: rec.failure('no {} hook defined', hook_name) return hook_func(self) return
def _get_analytics_code(self): with chlog.debug('set analytics code') as rec: code = self.get_config('site', 'analytics_code', None) if code is None: rec.failure('site.analytics_code not set in config.yaml') return '' match = _analytics_re.search(unicode(code)) if not match: rec.failure('analytics code blank or invalid: {!r}', code) return '' code = match.group('code') if len(code) < 6: rec.failure('analytics code too short: {!r}', code) return '' rec.success('analytics code set to {!r}', code) return code