def test_end_of_line(self): # we want to make sure we don't generate windows-style EOL article = Article() article.set_metadata("title", u"ok") article["body"] = u"hey\r\nyou" result = article.render() self.assertTrue(u"\r\n" not in result) self.assertTrue(u"hey\nyou" in result)
def create_article_or_page(): data = dict(request.POST.decode()) category = u'resultats' page = None for key, val in data.items(): if key.startswith(u'cat_add_'): category = key[len(u'cat_add_'):] break if key.startswith(u'page_add_'): page = key[len(u'page_add_'):] break title = data.get('title', u'').strip() if len(title) == 0: # nope app.add_alert(_('A title is required.')) if page is None: redirect('/category/%s' % category) else: redirect('/page/%s' % page) return article = Article() article['title'] = data['title'] article['body'] = data.get('content', DEFAULT_BODY) date = datetime.datetime.now() article.set_metadata('date', date) if page is None: # it's an article cat_info = dict(app.vars['categories'])[category] article.set_metadata('category', cat_info['title']) path = cat_info['path'] else: # it's a page path = dict(app.vars['pages'])[page]['path'] # XXX we might want to put it under the year directory i = 1 filename = slugify(article['title']) fullfilename = os.path.join(path, filename) while os.path.exists(fullfilename + '.rst'): fullfilename += str(i) i += 1 with open(fullfilename + '.rst', 'w') as f: f.write(article.render().encode('utf8')) emit(EVENT_CREATED_CONTENT, article_path=fullfilename) if page is None: redirect('/category/%s/%s' % (category, filename + '.rst')) else: redirect('/page/%s/%s' % (page, filename + '.rst'))
def __init__(self, *args, **kw): nodes.NodeVisitor.__init__(self, *args, **kw) self.result = [] self.article = Article() self._main_title_visited = False
class RSTTranslator(nodes.NodeVisitor): def __init__(self, *args, **kw): nodes.NodeVisitor.__init__(self, *args, **kw) self.result = [] self.article = Article() self._main_title_visited = False def visit_title(self, node): if not self._main_title_visited: self.article['title'] = node.astext() self.article['title_source'] = node.realsource self._main_title_visited = True self.result.append(node.realsource) self.result.append('') raise SkipNode() def visit_docinfo(self, node): for info in node.children: if info.tagname == 'field': name, body = info.children name = name.astext() value = body.astext() if name in ('date', 'eventdate'): value = get_date(value) self.article.set_metadata(name, value) else: if info.tagname.lower() in ('date', 'eventdate'): value = get_date(info.astext()) else: value = info.astext() self.article.set_metadata(info.tagname.lower(), value) self.result.append(node.realsource) self.result.append('') raise SkipNode() def visit_enumerated_list(self, node): self.result.append(node.rawsource) self.result.append('') raise SkipNode() def visit_block_quote(self, node): self.result.append(node.rawsource) self.result.append('') raise SkipNode() def depart_block_quote(self, node): pass def visit_line_block(self, node): self.result.append(node.rawsource) self.result.append('') raise SkipNode() def depart_line_block(self, node): pass def visit_system_message(self, node): raise SkipNode() def depart_system_message(self): pass def visit_line(self, node): raise NotImplementedError(node) def depart_line(self, node): pass def visit_Text(self, node): self.result.append(node.rawsource) def depart_Text(self, node): pass def visit_substitution_definition(self, node): self.result.append(node.rawsource) raise SkipNode() def depart_substitution_definition(self, node): pass def visit_target(self, node): self.result.append(node.rawsource) raise SkipNode() def depart_target(self, node): pass def visit_reference(self, node): self.result.append(node.rawsource) raise SkipNode() def visit_transition(self, node): self.result.append(node.rawsource) self.result.append('') raise SkipNode() def depart_transition(self, node): pass def visit_comment(self, node): self.result.append(node.rawsource) raise SkipNode() def depart_comment(self, node): pass def visit_paragraph(self, node): self.result.append(node.rawsource) self.result.append('') raise SkipNode() def visit_image(self, node): self.result.append(node.rawsource) raise SkipNode() def visit_bullet_list(self, node): self.result.append(node.rawsource) self.result.append('') raise SkipNode() def visit_definition_list(self, node): self.result.append(node.rawsource) self.result.append('') raise SkipNode() def visit_doctest_block(self, node): self.result.append(node.rawsource) self.result.append('') raise SkipNode() def visit_section(self, node): pass def visit_document(self, node): pass def depart_document(self, node): pass def depart_section(self, node): pass def astext(self): self.article['body'] = '\n'.join(self.result[4:]) res = '\n'.join(self.result) if res[-1] != '\n': res += '\n' return res def visit_raw(self, node): if node.rawsource != '': self.result.append(node.rawsource) self.result.append('') raise SkipNode() source = node.astext() self.result.append('..raw:: html') self.result.append('') for line in source.split('\n'): self.result.append(' %s' % line) self.result.append('') raise SkipNode() def depart_raw(self, node): pass