def __render(self, template = None, context = None): """Renders a page given template and context""" if template is None: template = self.template if context is None: context = self.context context['html_content'] = util.htransform(self.content, context.get('htransform', 'markdown')) try: return template.render(**context) except UnicodeDecodeError, msg: digest = hashlib.sha1(self.content).hexdigest() print "Rendering failed: %s" % msg print "Renter input is at '%s'." % os.path.join('cache', digest) sys.exit(1)
def test_asciidoc_lib(self): assert ( util.htransform("`strong` `{hammer}`", "asciidoc") == """<div class="paragraph"><p><tt>strong</tt> <tt>{hammer}</tt></p></div>\r\n""" )
def test_markdown_lib(self): assert util.htransform("*strong* hammer", "markdown") == "<p><em>strong</em> hammer</p>"
def __update_context(self, target_list = None): """Builds context from target_list""" entries = [] context = {} for target in target_list: with open(target + '.txt', 'r') as infh: raw_header, content = infh.read().split('\n' + self.config.get('separator', '---') + '\n', 1) header = util.parse_header(raw_header) title = header.get('title', 'No Title') extensions = ['codehilite', 'html_tidy'] snip = header.get('snip', '') if not len(snip) and (not 'snips' in self.config or self.config['snips'] == True): snip = util.htransform(content, self.config.get('htransform', None))[:50] + ' ...' if not self.rss: html_content = util.htransform(content, self.config.get('htransform', None)) else: rss_content = saxutils.escape(util.htransform(content, self.config.get('htransform', None))) pubdate = header.get('pubdate', None) pubdate_h = util.build_timestamp_h(pubdate, rss=self.rss) # Has a date it was published, isn't a draft and isn't a static page if pubdate and not (header.get('draft', None) or header.get('static', None)): entries.append({'title': title, 'permalink': header.get('permalink', util.build_slug(self.config, title, infh.name)), 'snip': snip, 'pubdate': pubdate, 'pubdate_h': pubdate_h}) if not self.rss: entries[-1]['html_content'] = html_content else: entries[-1]['rss_content'] = rss_content entries.sort(cmp = (lambda x, y: -1 if x['pubdate'] > y['pubdate'] else 1)) indexlen = int(self.config.get('indexlen', 10)) rsslen = int(self.config.get('rsslen', 10)) taglen = int(self.config.get('taglen', 0)) if self.name == 'index' and indexlen > 0: context['entries'] = entries[:indexlen] if os.path.exists('tags'): with open('tags') as tagsfh: context['tags'] = tagsfh.read() else: context['tags'] = '' elif self.name.endswith('.rss') and rsslen > 0: context['entries'] = entries[:rsslen] elif (not (self.name.endswith('.rss') or self.name == 'archive')) and taglen > 0: context['entries'] = entries[:taglen] else: context['entries'] = entries context['permalink'] = self.name if self.name.startswith('index') or self.name.startswith('archive'): context['title'] = self.config['title'] self.tagprefix = '' else: if self.name.endswith('.rss'): context['title'] = saxutils.escape(self.config['title'] + ' - Tags: ' + self.name.replace('.rss', '')) self.tagprefix = '' else: context['title'] = self.config['title'] + ' - Tags: ' + self.name context['baseurl'] = self.config['baseurl'] return context