[py-svn] r33483 - in py/dist/py: rst rst/testing test/tracer
guido at codespeak.net
guido at codespeak.net
Thu Oct 19 22:51:29 CEST 2006
Author: guido
Date: Thu Oct 19 22:51:27 2006
New Revision: 33483
Modified:
py/dist/py/rst/rst.py
py/dist/py/rst/testing/test_rst.py
py/dist/py/test/tracer/genrest.py
Log:
Renamed 'BlockQuote' to 'LiteralBlock' (since blockquote in ReST has a
different meaning), added some basic inline markup items, added escaping of
markup characters.
Modified: py/dist/py/rst/rst.py
==============================================================================
--- py/dist/py/rst/rst.py (original)
+++ py/dist/py/rst/rst.py Thu Oct 19 22:51:27 2006
@@ -6,6 +6,12 @@
import py
+def escape(txt):
+ """escape ReST markup"""
+ for c in '*`[|':
+ txt = txt.replace(c, '\\%s' % (c,))
+ return txt
+
class RestError(Exception):
pass
@@ -51,7 +57,7 @@
def add(self, child):
self._add(child)
- return child
+ return child
def _add(self, child):
if child.__class__ not in self.allowed_child:
@@ -84,7 +90,7 @@
return ""
link_texts = []
for link, target in self.links.iteritems():
- link_texts.append(".. _`%s`: %s" % (link, target))
+ link_texts.append(".. _`%s`: %s" % (escape(link), escape(target)))
return "\n" + "\n".join(link_texts) + "\n\n"
def text(self):
@@ -141,14 +147,15 @@
class SubParagraph(Paragraph):
indent = " "
-class BlockQuote(Paragraph):
+class LiteralBlock(Paragraph):
indent = " "
sep = ""
def text(self):
all_txt = AbstractNode.text(self)
all_txts = all_txt.split("\n")
- return '::\n\n%s' % ("\n".join([self.indent + i for i in all_txts]),)
+ return '::\n\n%s' % ("\n".join([self.indent + i for i in
+ all_txts]),)
class Title(Paragraph):
parentclass = Rest
@@ -173,27 +180,51 @@
self._text = _text
def text(self):
- return self.start + self._text + self.end
+ return self.start + escape(self._text) + self.end
class Text(AbstractText):
_reg_whitespace = py.std.re.compile('\s+')
def wordlist(self):
- return self._reg_whitespace.split(self._text)
+ return self._reg_whitespace.split(escape(self._text))
-class Emph(AbstractText):
+class Em(AbstractText):
start = "*"
end = "*"
+class Strong(AbstractText):
+ start = "**"
+ end = "**"
+
+class Quote(AbstractText):
+ start = '``'
+ end = '``'
+
+class Anchor(AbstractText):
+ start = '_`'
+ end = '`'
+
+class Footnote(AbstractText):
+ def __init__(self, note, symbol=False):
+ raise NotImplemented('XXX')
+
+class Citation(AbstractText):
+ def __init__(self, text, cite):
+ raise NotImplemented('XXX')
+
class ListItem(Paragraph):
item_char = "*"
def text(self):
- self.indent = self.indent + " "
+ oldindent = self.indent
+ self.indent = oldindent + ' '
txt = Paragraph.text(self)
txt = self.item_char + txt[1:]
- del self.indent # XXX ?
+ self.indent = oldindent
return txt
+class OrderedListItem(ListItem):
+ item_char = "#"
+
class Link(AbstractText):
start = '`'
end = '`_'
@@ -216,6 +247,7 @@
next = next.parent
return next
-class Quote(AbstractText):
- start = '``'
- end = '``'
+class Substitution(AbstractText):
+ def __init__(self, text, **kwargs):
+ raise NotImplemented('XXX')
+
Modified: py/dist/py/rst/testing/test_rst.py
==============================================================================
--- py/dist/py/rst/testing/test_rst.py (original)
+++ py/dist/py/rst/testing/test_rst.py Thu Oct 19 22:51:27 2006
@@ -72,7 +72,7 @@
Paragraph
"""
- txt = Rest(Paragraph("Text"), BlockQuote("def fun():\n some"), \
+ txt = Rest(Paragraph("Text"), LiteralBlock("def fun():\n some"), \
Paragraph("Paragraph")).text()
print repr(txt)
assert txt == expected
@@ -174,3 +174,18 @@
"""
txt = Rest(Paragraph("Foo, bar and ", Link("baz", "http://www.baz.com")),
Title('Spam'), Paragraph('Spam, eggs and spam.'))
+
+def test_basic_inline():
+ txt = Em('foo').text()
+ assert txt == '*foo*'
+ txt = Strong('bar').text()
+ assert txt == '**bar**'
+
+def test_escape():
+ txt = Paragraph('*escape* ``test``').text()
+ assert txt == '\\*escape\\* \\`\\`test\\`\\`'
+ txt = Strong('*strong*').text()
+ assert txt == '**\\*strong\\***'
+ txt = Rest(Paragraph(Link('foo[bar]', 'foo[bar]'))).text()
+ assert txt == "`foo\\[bar]`_\n\n.. _`foo\\[bar]`: foo\\[bar]\n\n"
+
Modified: py/dist/py/test/tracer/genrest.py
==============================================================================
--- py/dist/py/test/tracer/genrest.py (original)
+++ py/dist/py/test/tracer/genrest.py Thu Oct 19 22:51:27 2006
@@ -158,7 +158,7 @@
def write_class(self, section_name, class_name):
classlst = [Title("Class: %s" % class_name, belowchar='-'),
- BlockQuote(self.dsa.get_function_doc(class_name))]
+ LiteralBlock(self.dsa.get_function_doc(class_name))]
# write down exported methods
classlst.append(Title("Exported methods:", belowchar="^"))
@@ -181,8 +181,8 @@
# from indentation, or treated as ReST too (although this is obviously
# dangerous for non-ReST docstrings)...
lst = [Title("Function: %s" % fun_name, belowchar=belowchar),
- BlockQuote(self.dsa.get_function_doc(fun_name)),
- BlockQuote(self.dsa.get_function_definition(fun_name))]
+ LiteralBlock(self.dsa.get_function_doc(fun_name)),
+ LiteralBlock(self.dsa.get_function_definition(fun_name))]
# XXX missing implementation of dsa.get_function_location()
#filename, lineno = self.dsa.get_function_location(fun_name)
@@ -217,7 +217,7 @@
call_sites.append(Paragraph(Link(link_str, link_target)))
else:
call_sites.append(Paragraph(link_str))
- #call_sites.append(BlockQuote(call_site.source))
+ #call_sites.append(LiteralBlock(call_site.source))
# XXX: For now, we just paste here the filename of that
#call_sites.append(Paragraph(link_str))
source = frame.code.source()
@@ -228,7 +228,7 @@
lines.append(">%s%s" % ("-" * len(m.group(1)), m.group(2)))
else:
lines.append(" " + line)
- call_sites.append(BlockQuote("\n".join(lines)))
+ call_sites.append(LiteralBlock("\n".join(lines)))
return lst
More information about the py-svn
mailing list