Skip to content

Commit 56dbc42

Browse files
author
Ralf Schlatterbeck
committed
Add a rst method to StringHTMLProperty...
...similar to stext (which is for StructuredText) rst is for ReStructuredText. The new method supports hyperlinking which is on by default.
1 parent 49b1da7 commit 56dbc42

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

roundup/cgi/templating.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
import StructuredText
4343
except ImportError:
4444
StructuredText = None
45+
try:
46+
from docutils.core import publish_parts as ReStructuredText
47+
except ImportError:
48+
ReStructuredText = None
4549

4650
# bring in the templating support
4751
from roundup.cgi.PageTemplates import PageTemplate, GlobalTranslationService
@@ -1241,6 +1245,19 @@ class StringHTMLProperty(HTMLProperty):
12411245
hyper_re = re.compile(r'((?P<url>\w{3,6}://\S+)|'
12421246
r'(?P<email>[-+=%/\w\.]+@[\w\.\-]+)|'
12431247
r'(?P<item>(?P<class>[A-Za-z_]+)(\s*)(?P<id>\d+)))')
1248+
def _hyper_repl_item(self,match,replacement):
1249+
item = match.group('item')
1250+
cls = match.group('class').lower()
1251+
id = match.group('id')
1252+
try:
1253+
# make sure cls is a valid tracker classname
1254+
cl = self._db.getclass(cls)
1255+
if not cl.hasnode(id):
1256+
return item
1257+
return replacement % locals()
1258+
except KeyError:
1259+
return item
1260+
12441261
def _hyper_repl(self, match):
12451262
if match.group('url'):
12461263
s = match.group('url')
@@ -1249,17 +1266,18 @@ def _hyper_repl(self, match):
12491266
s = match.group('email')
12501267
return '<a href="mailto:%s">%s</a>'%(s, s)
12511268
else:
1252-
s = match.group('item')
1253-
s1 = match.group('class').lower()
1254-
s2 = match.group('id')
1255-
try:
1256-
# make sure s1 is a valid tracker classname
1257-
cl = self._db.getclass(s1)
1258-
if not cl.hasnode(s2):
1259-
return s
1260-
return '<a href="%s%s">%s</a>'%(s1, s2, s)
1261-
except KeyError:
1262-
return s
1269+
return self._hyper_repl_item(match,
1270+
'<a href="%(cls)s%(id)s">%(item)s</a>')
1271+
1272+
def _hyper_repl_rst(self, match):
1273+
if match.group('url'):
1274+
s = match.group('url')
1275+
return '`%s <%s>`_'%(s, s)
1276+
elif match.group('email'):
1277+
s = match.group('email')
1278+
return '`%s <mailto:%s>`_'%(s, s)
1279+
else:
1280+
return self._hyper_repl_item(match,'`%(item)s <%(cls)s%(id)s>`_')
12631281

12641282
def hyperlinked(self):
12651283
""" Render a "hyperlinked" version of the text """
@@ -1329,6 +1347,22 @@ def stext(self, escape=0, hyperlink=1):
13291347
return s
13301348
return StructuredText(s,level=1,header=0)
13311349

1350+
def rst(self, hyperlink=1):
1351+
""" Render the value of the property as ReStructuredText.
1352+
1353+
This requires docutils to be installed separately.
1354+
"""
1355+
if not self.is_view_ok():
1356+
return self._('[hidden]')
1357+
1358+
if not ReStructuredText:
1359+
return self.plain(escape=0, hyperlink=hyperlink)
1360+
s = self.plain(escape=0, hyperlink=0)
1361+
if hyperlink:
1362+
s = self.hyper_re.sub(self._hyper_repl_rst, s)
1363+
return ReStructuredText(s, writer_name="html")["body"].encode("utf-8",
1364+
"replace")
1365+
13321366
def field(self, **kwargs):
13331367
""" Render the property as a field in HTML.
13341368

0 commit comments

Comments
 (0)