def test_humane_labels(self): self.assertEqual(u'Politician', schema.get_schema('Politician')['label']) self.assertEqual( u'Politicians', schema.humane_property('Politician', 'politicalParty', True)) self.assertEqual( u'Political Party', schema.humane_property('Politician', 'politicalParty'))
def rendered_data(self): try: data = self.data except ValueError: data = {} data = [ (n, v, schema.humane_property(self.itemtype, n)) for n, v in data.items() if (n not in ["schema", "name", "datePageModified"]) and (not isinstance(v, schema.Property) or v.ptype != "LongText") ] if len(data) == 0: return "" html = [u'<div class="structured-data">', u"<h1>Structured data</h1>", u"<dl>"] data = sorted(data, key=operator.itemgetter(2)) render_data_item = ( lambda itemname, itemvalue: u'<dd class="value value-%s"><span itemprop="%s">%s</span></dd>' % (itemname, itemname, itemvalue.render()) ) for name, value, humane_name in data: html.append(u'<dt class="key key-%s">%s</dt>' % (name, humane_name)) if type(value) == list: html += [render_data_item(name, v) for v in value] else: html.append(render_data_item(name, value)) html.append(u"</dl></div>") return "\n".join(html)
def rendered_data(self): try: data = self.data except ValueError: data = {} data = [ (n, v, schema.humane_property(self.itemtype, n)) for n, v in data.items() if (n not in ['schema', 'name', 'datePageModified']) and (not isinstance(v, schema.Property) or v.ptype != 'LongText') ] if len(data) == 0: return '' html = [ u'<div class="structured-data">', u'<h1>Structured data</h1>', u'<dl>', ] data = sorted(data, key=operator.itemgetter(2)) render_data_item = lambda itemname, itemvalue: u'<dd class="value value-%s"><span itemprop="%s">%s</span></dd>' % (itemname, itemname, itemvalue.render()) for name, value, humane_name in data: html.append(u'<dt class="key key-%s">%s</dt>' % (name, humane_name)) if type(value) == list: html += [render_data_item(name, v) for v in value] else: html.append(render_data_item(name, value)) html.append(u'</dl></div>') return '\n'.join(html)
def render_body( cls, title, body, rendered_data="", inlinks={}, related_links_by_score={}, older_title=None, newer_title=None ): # body body_parts = [cls.remove_metadata(body)] # incoming links if len(inlinks) > 0: lines = [u"# Incoming Links"] for i, (rel, links) in enumerate(inlinks.items()): itemtype, rel = rel.split("/") lines.append( u'## %s <span class="hidden">(%s %d)</span>' % (schema.humane_property(itemtype, rel, True), itemtype, i) ) # remove dups and sort links = list(set(links)) links.sort() lines += [u"* [[%s]]" % t for t in links] body_parts.append(u"\n".join(lines)) # related links related_links = related_links_by_score if len(related_links) > 0: lines = [u"# Suggested Pages"] lines += [u"* {{.score::%.3f}} [[%s]]\n{.noli}" % (score, t) for t, score in related_links.items()[:10]] body_parts.append(u"\n".join(lines)) body_parts.append(u"* [More suggestions...](/+%s)\n{.more-suggestions}" % (cls.title_to_path(title))) # other posts if older_title or newer_title: lines = [u"# Other Posts"] if newer_title: lines.append(u"* {{.newer::newer}} [[%s]]\n{.noli}" % newer_title) if older_title: lines.append(u"* {{.older::older}} [[%s]]\n{.noli}" % older_title) body_parts.append(u"\n".join(lines)) # remove yaml/schema block joined = u"\n".join(body_parts) joined = re.sub(PageOperationMixin.re_yaml_schema, u"\n", joined) # render to html rendered = md.convert(joined) # add table of contents rendered = TocGenerator(rendered).add_toc() # add class for embedded image rendered = PageOperationMixin.re_img.sub(ur'<\1 class="img-container"><img\2/></\3>', rendered) # add structured data block rendered = rendered_data + rendered return cls.sanitize_html(rendered)
def rendered_body(self): final = cache.get_rendered_body(self.title) if final is not None: return final # body body_parts = [WikiPage.remove_metadata(self.body)] # incoming links if len(self.inlinks) > 0: lines = [u'# Incoming Links'] for rel, links in self.inlinks.items(): itemtype, rel = rel.split('/') humane_rel = schema.humane_property(itemtype, rel, True) lines.append(u'## %s' % humane_rel) lines += [u'* [[%s]]' % title for title in set(links)] body_parts.append(u'\n'.join(lines)) # related links related_links = self.related_links_by_score if len(related_links) > 0: lines = [u'# Suggested Pages'] lines += [u'* {{.score::%.2f}} [[%s]]' % (score, title) for title, score in related_links.items()[:10]] body_parts.append(u'\n'.join(lines)) # other posts if self.older_title or self.newer_title: lines = [u'# Other Posts'] if self.newer_title: lines.append(u'* {{.newer::newer}} [[%s]]' % self.newer_title) if self.older_title: lines.append(u'* {{.older::older}} [[%s]]' % self.older_title) body_parts.append(u'\n'.join(lines)) # render to html rendered = md.convert(u'\n'.join(body_parts)) # add table of contents final = TocGenerator(rendered).add_toc() # add class for embedded image final = WikiPage.re_img.sub(u'<p class="img-container"><img \\1/></p>', final) # sanitize if final: cleaner = Cleaner(safe_attrs_only=False) final = cleaner.clean_html(final) cache.set_rendered_body(self.title, final) return final
def render_body(cls, title, body, rendered_data='', inlinks={}, related_links_by_score={}, older_title=None, newer_title=None): # body body_parts = [cls.remove_metadata(body)] # incoming links if len(inlinks) > 0: lines = [u'# Incoming Links'] for i, (rel, links) in enumerate(inlinks.items()): itemtype, rel = rel.split('/') lines.append(u'## %s <span class="hidden">(%s %d)</span>' % (schema.humane_property(itemtype, rel, True), itemtype, i)) # remove dups and sort links = list(set(links)) links.sort() lines += [u'* [[%s]]' % t for t in links] body_parts.append(u'\n'.join(lines)) # related links related_links = related_links_by_score if len(related_links) > 0: lines = [u'# Suggested Pages'] lines += [u'* {{.score::%.3f}} [[%s]]\n{.noli}' % (score, t) for t, score in related_links.items()[:10]] body_parts.append(u'\n'.join(lines)) body_parts.append(u'* [More suggestions...](/+%s)\n{.more-suggestions}' % (cls.title_to_path(title))) # other posts if older_title or newer_title: lines = [u'# Other Posts'] if newer_title: lines.append(u'* {{.newer::newer}} [[%s]]\n{.noli}' % newer_title) if older_title: lines.append(u'* {{.older::older}} [[%s]]\n{.noli}' % older_title) body_parts.append(u'\n'.join(lines)) # remove yaml/schema block joined = u'\n'.join(body_parts) joined = re.sub(PageOperationMixin.re_yaml_schema, u'\n', joined) # render to html rendered = md.convert(joined) # add table of contents rendered = TocGenerator(rendered).add_toc() # add class for embedded image rendered = PageOperationMixin.re_img.sub(ur'<\1 class="img-container"><img\2/></\3>', rendered) # add structured data block rendered = rendered_data + rendered return cls.sanitize_html(rendered)
def render_body(cls, body, rendered_data='', inlinks={}, related_links_by_score={}, older_title=None, newer_title=None): # body body_parts = [cls.remove_metadata(body)] # incoming links if len(inlinks) > 0: lines = [u'# Incoming Links'] for rel, links in inlinks.items(): itemtype, rel = rel.split('/') lines.append(u'## %s' % schema.humane_property(itemtype, rel, True)) # remove dups and sort links = list(set(links)) links.sort() lines += [u'* [[%s]]' % title for title in links] body_parts.append(u'\n'.join(lines)) # related links related_links = related_links_by_score if len(related_links) > 0: lines = [u'# Suggested Pages'] lines += [u'* {{.score::%.3f}} [[%s]]\n{.noli}' % (score, title) for title, score in related_links.items()[:10]] body_parts.append(u'\n'.join(lines)) # other posts if older_title or newer_title: lines = [u'# Other Posts'] if newer_title: lines.append(u'* {{.newer::newer}} [[%s]]\n{.noli}' % newer_title) if older_title: lines.append(u'* {{.older::older}} [[%s]]\n{.noli}' % older_title) body_parts.append(u'\n'.join(lines)) # remove yaml/schema block joined = u'\n'.join(body_parts) joined = re.sub(PageOperationMixin.re_yaml_schema, u'\n', joined) # render to html rendered = md.convert(joined) # add table of contents rendered = TocGenerator(rendered).add_toc() # add class for embedded image rendered = PageOperationMixin.re_img.sub(ur'<\1 class="img-container"><img\2/></\3>', rendered) # add structured data block rendered = rendered_data + rendered return cls.sanitize_html(rendered)
def rendered_data(self): data = [(n, v, schema.humane_property(self.itemtype, n)) for n, v in self.data.items() if n != 'schema'] if len(data) == 1: # only name and schema? return '' html = [ u'<div class="structured-data">', u'<h1>Structured data</h1>', u'<dl>', ] data = sorted(data, key=operator.itemgetter(2)) render_data_item = lambda itemname, itemvalue: u'<dd class="value value-%s"><span itemprop="%s">%s</span></dd>' % (itemname, itemname, itemvalue.render()) for name, value, humane_name in data: html.append(u'<dt class="key key-%s">%s</dt>' % (name, humane_name)) if type(value) == list: html += [render_data_item(name, v) for v in value] else: html.append(render_data_item(name, value)) html.append(u'</dl></div>') return '\n'.join(html)
def rendered_data(self): data = [ (n, v, schema.humane_property(self.itemtype, n, False)) for n, v in self.data.items() if n not in ["schema", "inlinks", "outlinks"] ] if len(data) == 1: # only name and schema? return "" html = [u'<div class="structured-data">', u"<h1>Structured data</h1>", u"<dl>"] data = sorted(data, key=operator.itemgetter(2)) for name, value, humane_name in data: html.append(u'<dt class="key key-%s">%s</dt>' % (name, humane_name)) if type(value) == list: for v in value: html.append(u'<dd class="value value-%s">%s</dd>' % (name, self._render_data_item(name, v))) else: html.append(u'<dd class="value value-%s">%s</dd>' % (name, self._render_data_item(name, value))) html.append(u"</dl></div>") return "\n".join(html)
def test_humane_property(self): self.assertEqual('Publications', schema.humane_property('Book', 'datePublished', True)) self.assertEqual( 'Published date', schema.humane_property('Book', 'datePublished', False))
def rendered_body(self): # body body_parts = [PageOperationMixin.remove_metadata(self.body)] # incoming links if len(self.inlinks) > 0: lines = [u"# Incoming Links"] for rel, links in self.inlinks.items(): itemtype, rel = rel.split("/") humane_rel = schema.humane_property(itemtype, rel, True) lines.append(u"## %s" % humane_rel) # remove dups and sort links = list(set(links)) links.sort() lines += [u"* [[%s]]" % title for title in links] body_parts.append(u"\n".join(lines)) # related links related_links = self.related_links_by_score if len(related_links) > 0: lines = [u"# Suggested Pages"] lines += [ u"* {{.score::%.3f}} [[%s]]\n{.noli}" % (score, title) for title, score in related_links.items()[:10] ] body_parts.append(u"\n".join(lines)) # other posts if self.older_title or self.newer_title: lines = [u"# Other Posts"] if self.newer_title: lines.append(u"* {{.newer::newer}} [[%s]]\n{.noli}" % self.newer_title) if self.older_title: lines.append(u"* {{.older::older}} [[%s]]\n{.noli}" % self.older_title) body_parts.append(u"\n".join(lines)) # remove yaml/schema block joined = u"\n".join(body_parts) joined = re.sub(PageOperationMixin.re_yaml_schema, u"\n", joined) # render to html rendered = md.convert(joined) # add table of contents rendered = TocGenerator(rendered).add_toc() # add class for embedded image rendered = PageOperationMixin.re_img.sub(u'<p class="img-container"><img \\1/></p>', rendered) # add structured data block rendered = self.rendered_data + rendered # sanitize if rendered: cleaner = Cleaner(safe_attrs_only=False) cleaner.host_whitelist = ("www.youtube.com", "player.vimeo.com") rendered = cleaner.clean_html(rendered) # remove div wrapper if there is one if rendered.startswith("<div>"): rendered = rendered[5:-6] return rendered
def test_incoming_links(self): self.assertEqual(u'Related People', schema.humane_property('Person', 'relatedTo', True)) self.assertEqual(u'Children (People)', schema.humane_property('Person', 'parent', True))
def test_humane_labels(self): self.assertEqual(u'Politician', schema.get_schema('Politician')['label']) self.assertEqual(u'Politicians', schema.humane_property('Politician', 'politicalParty', True)) self.assertEqual(u'Political Party', schema.humane_property('Politician', 'politicalParty'))
def test_humane_property(self): self.assertEqual('Published Books', schema.humane_property('Book', 'datePublished', True)) self.assertEqual('Date Published', schema.humane_property('Book', 'datePublished'))
def test_humane_property(self): self.assertEqual('Publications', schema.humane_property('Book', 'datePublished', True)) self.assertEqual('Published date', schema.humane_property('Book', 'datePublished', False))
def test_humane_property(self): self.assertEqual("Publications", schema.humane_property("Book", "datePublished", True)) self.assertEqual("Published date", schema.humane_property("Book", "datePublished", False))