[Zope-Checkins] CVS: Zope2 - StructuredText.py:1.34.8.1
Andreas Jung
andreas@dhcp165.digicool.com
Thu, 19 Apr 2001 08:18:29 -0400
Update of /cvs-repository/Zope2/lib/python/StructuredText
In directory yetix:/work/sandboxes/ajung-2_4-ts_regex-exterminiation-branch/lib/python/StructuredText
Modified Files:
Tag: ajung-2_4-ts_regex-exterminiation-branch
StructuredText.py
Log Message:
priv. com
--- Updated File StructuredText.py in package Zope2 --
--- StructuredText.py 2001/03/20 23:45:24 1.34
+++ StructuredText.py 2001/04/19 12:18:28 1.34.8.1
@@ -205,11 +205,12 @@
'''
-import ts_regex, regex
-from ts_regex import gsub
+import ts_regex_old as ts_regex
+import regex
from string import split, join, strip, find
+import re
-def untabify(aString,
+def untabify_new(aString,
indent_tab=ts_regex.compile('\(\n\|^\)\( *\)\t').search_group,
):
'''\
@@ -229,6 +230,26 @@
else:
return result+rest
+def untabify_old(aString,
+ indent_tab=ts_regex.compile('\(\n\|^\)\( *\)\t').search_group,
+ ):
+ '''\
+ Convert indentation tabs to spaces.
+ '''
+ result=''
+ rest=aString
+ while 1:
+ ts_results = indent_tab(rest, (1,2))
+ if ts_results:
+ start, grps = ts_results
+ lnl=len(grps[0])
+ indent=len(grps[1])
+ result=result+rest[:start]
+ rest="\n%s%s" % (' ' * ((indent/8+1)*8),
+ rest[start+indent+1+lnl:])
+ else:
+ return result+rest
+
def indent(aString, indent=2):
"""Indent a string the given number of spaces"""
r=split(untabify(aString),'\n')
@@ -364,18 +385,18 @@
level -- The level of top level headings to be created.
'''
- aStructuredString = gsub(
- '\"\([^\"\0]+\)\":' # title: <"text":>
- + ('\([-:a-zA-Z0-9_,./?=@#~&]+%s\)'
+ aStructuredString = re.sub(
+ '\"([^\"\0]+)\":' # title: <"text":>
+ + ('([-:a-zA-Z0-9_,./?=@#~&]+%s)'
% not_punctuation_or_whitespace)
+ optional_trailing_punctuation
+ trailing_space,
'<a href="\\2">\\1</a>\\4\\5\\6',
aStructuredString)
- aStructuredString = gsub(
- '\"\([^\"\0]+\)\",[\0- ]+' # title: <"text", >
- + ('\([a-zA-Z]*:[-:a-zA-Z0-9_,./?=@#~&]*%s\)'
+ aStructuredString = re.sub(
+ '\"([^\"\0]+)\",[\0- ]+' # title: <"text", >
+ + ('([a-zA-Z]*:[-:a-zA-Z0-9_,./?=@#~&]*%s)'
% not_punctuation_or_whitespace)
+ optional_trailing_punctuation
+ trailing_space,
@@ -384,7 +405,7 @@
protoless = find(aStructuredString, '<a href=":')
if protoless != -1:
- aStructuredString = gsub('<a href=":', '<a href="',
+ aStructuredString = re.sub('<a href=":', '<a href="',
aStructuredString)
self.level=level
@@ -399,26 +420,26 @@
return str(self.structure)
-ctag_prefix="\([\0- (]\|^\)"
-ctag_suffix="\([\0- ,.:;!?)]\|$\)"
-ctag_middle="[%s]\([^\0- %s][^%s]*[^\0- %s]\|[^%s]\)[%s]"
-ctag_middl2="[%s][%s]\([^\0- %s][^%s]*[^\0- %s]\|[^%s]\)[%s][%s]"
+ctag_prefix='([\x00- \\(]|^)'
+ctag_suffix='([\x00- ,.:;!?\\)]|$)'
+ctag_middle='[%s]([^\x00- %s][^%s]*[^\x00- %s]|[^%s])[%s]'
+ctag_middl2='[%s][%s]([^\x00- %s][^%s]*[^\x00- %s]|[^%s])[%s][%s]'
def ctag(s,
- em=regex.compile(
+ em=re.compile(
ctag_prefix+(ctag_middle % (("*",)*6) )+ctag_suffix),
- strong=regex.compile(
+ strong=re.compile(
ctag_prefix+(ctag_middl2 % (("*",)*8))+ctag_suffix),
- under=regex.compile(
+ under=re.compile(
ctag_prefix+(ctag_middle % (("_",)*6) )+ctag_suffix),
- code=regex.compile(
+ code=re.compile(
ctag_prefix+(ctag_middle % (("\'",)*6))+ctag_suffix),
):
if s is None: s=''
- s=gsub(strong,'\\1<strong>\\2</strong>\\3',s)
- s=gsub(under, '\\1<u>\\2</u>\\3',s)
- s=gsub(code, '\\1<code>\\2</code>\\3',s)
- s=gsub(em, '\\1<em>\\2</em>\\3',s)
+ s=strong.sub('\\1<strong>\\2</strong>\\3',s)
+ s=under.sub('\\1<u>\\2</u>\\3',s)
+ s=code.sub( '\\1<code>\\2</code>\\3',s)
+ s=em.sub( '\\1<em>\\2</em>\\3',s)
return s
class HTML(StructuredText):
@@ -428,18 +449,18 @@
'''\
def __str__(self,
- extra_dl=regex.compile("</dl>\n<dl>"),
- extra_ul=regex.compile("</ul>\n<ul>"),
- extra_ol=regex.compile("</ol>\n<ol>"),
+ extra_dl=re.compile("</dl>\n<dl>"),
+ extra_ul=re.compile("</ul>\n<ul>"),
+ extra_ol=re.compile("</ol>\n<ol>"),
):
'''\
Return an HTML string representation of the structured text data.
'''
s=self._str(self.structure,self.level)
- s=gsub(extra_dl,'\n',s)
- s=gsub(extra_ul,'\n',s)
- s=gsub(extra_ol,'\n',s)
+ s=re.sub(extra_dl,'\n',s)
+ s=re.sub(extra_ul,'\n',s)
+ s=re.sub(extra_ol,'\n',s)
return s
def ul(self, before, p, after):
@@ -552,29 +573,29 @@
def html_quote(v,
character_entities=(
- (regex.compile('&'), '&'),
- (regex.compile("<"), '<' ),
- (regex.compile(">"), '>' ),
- (regex.compile('"'), '"')
+ (re.compile('&'), '&'),
+ (re.compile("<"), '<' ),
+ (re.compile(">"), '>' ),
+ (re.compile('"'), '"')
)): #"
text=str(v)
for re,name in character_entities:
- text=gsub(re,name,text)
+ text=re.sub(re,name,text)
return text
def html_with_references(text, level=1):
- text = gsub(
- '[\0\n]\.\. \[\([0-9_a-zA-Z-]+\)\]',
+ text = re.sub(
+ '[\0\n]\.\. \[([0-9_a-zA-Z-]+)\]',
'\n <a name="\\1">[\\1]</a>',
text)
- text = gsub(
- '\([\0- ,]\)\[\([0-9_a-zA-Z-]+\)\]\([\0- ,.:]\)',
+ text = re.sub(
+ '([\0- ,])\[([0-9_a-zA-Z-]+)\]([\0- ,.:])',
'\\1<a href="#\\2">[\\2]</a>\\3',
text)
- text = gsub(
- '\([\0- ,]\)\[\([^]]+\)\.html\]\([\0- ,.:]\)',
+ text = re.sub(
+ '([\0- ,])\[([^]]+)\.html\]([\0- ,.:])',
'\\1<a href="\\2.html">[\\2]</a>\\3',
text)