Commit e00b1050 authored by Jim Fulton's avatar Jim Fulton

Various updates, including new indentation utilities.

parent 895fb103
...@@ -46,7 +46,7 @@ Special symbology is used to indicate special constructs: ...@@ -46,7 +46,7 @@ Special symbology is used to indicate special constructs:
first '**' and whitespace or puctuation to the right of the second '**') first '**' and whitespace or puctuation to the right of the second '**')
is made strong. is made strong.
$Id: StructuredText.py,v 1.7 1997/12/12 15:39:54 jim Exp $''' $Id: StructuredText.py,v 1.8 1998/02/27 18:45:22 jim Exp $'''
# Copyright # Copyright
# #
# Copyright 1996 Digital Creations, L.C., 910 Princess Anne # Copyright 1996 Digital Creations, L.C., 910 Princess Anne
...@@ -98,6 +98,9 @@ $Id: StructuredText.py,v 1.7 1997/12/12 15:39:54 jim Exp $''' ...@@ -98,6 +98,9 @@ $Id: StructuredText.py,v 1.7 1997/12/12 15:39:54 jim Exp $'''
# (540) 371-6909 # (540) 371-6909
# #
# $Log: StructuredText.py,v $ # $Log: StructuredText.py,v $
# Revision 1.8 1998/02/27 18:45:22 jim
# Various updates, including new indentation utilities.
#
# Revision 1.7 1997/12/12 15:39:54 jim # Revision 1.7 1997/12/12 15:39:54 jim
# Added level as argument for html_with_references. # Added level as argument for html_with_references.
# #
...@@ -133,6 +136,7 @@ $Id: StructuredText.py,v 1.7 1997/12/12 15:39:54 jim Exp $''' ...@@ -133,6 +136,7 @@ $Id: StructuredText.py,v 1.7 1997/12/12 15:39:54 jim Exp $'''
import regex, regsub import regex, regsub
from regsub import gsub from regsub import gsub
from string import split, join, strip
indent_tab =regex.compile('\(\n\|^\)\( *\)\t') indent_tab =regex.compile('\(\n\|^\)\( *\)\t')
indent_space=regex.compile('\n\( *\)') indent_space=regex.compile('\n\( *\)')
...@@ -155,6 +159,35 @@ def untabify(aString): ...@@ -155,6 +159,35 @@ def untabify(aString):
else: else:
return result+rest return result+rest
def indent(aString, indent=2):
"""Indent a string the given number of spaces"""
r=split(untabify(aString),'\n')
if not r: return ''
if not r[-1]: del r[-1]
tab=' '*level
return "%s%s\n" % (tab,join(r,'\n'+tab))
def reindent(aString, indent=2, already_untabified=0):
"reindent a block of text, so that the minimum indent is as given"
if not already_untabified: aString=untabify(aString)
l=indent_level(aString)[0]
if indent==l: return aString
r=[]
append=r.append
if indent > l:
tab=' ' * (indent-l)
for s in split(aString,'\n'): append(tab+s)
else:
l=l-indent
for s in split(aString,'\n'): append(s[l:])
return join(r,'\n')
def indent_level(aString): def indent_level(aString):
'''\ '''\
Find the minimum indentation for a string, not counting blank lines. Find the minimum indentation for a string, not counting blank lines.
...@@ -286,12 +319,12 @@ class HTML(StructuredText): ...@@ -286,12 +319,12 @@ class HTML(StructuredText):
return s return s
def ul(self, before, p, after): def ul(self, before, p, after):
if p: p="<p>%s</p>" % ctag(p) if p: p="<p>%s</p>" % strip(ctag(p))
return ('%s<ul><li>%s\n%s\n</ul>\n' return ('%s<ul><li>%s\n%s\n</ul>\n'
% (before,p,after)) % (before,p,after))
def ol(self, before, p, after): def ol(self, before, p, after):
if p: p="<p>%s</p>" % ctag(p) if p: p="<p>%s</p>" % strip(ctag(p))
return ('%s<ol><li>%s\n%s\n</ol>\n' return ('%s<ol><li>%s\n%s\n</ol>\n'
% (before,p,after)) % (before,p,after))
...@@ -302,15 +335,26 @@ class HTML(StructuredText): ...@@ -302,15 +335,26 @@ class HTML(StructuredText):
def head(self, before, t, level, d): def head(self, before, t, level, d):
if level > 0 and level < 6: if level > 0 and level < 6:
return ('%s<h%d>%s</h%d>\n%s\n' return ('%s<h%d>%s</h%d>\n%s\n'
% (before,level,ctag(t),level,d)) % (before,level,strip(ctag(t)),level,d))
t="<p><strong>%s</strong><p>" % t t="<p><strong>%s</strong><p>" % strip(ctag(t))
return ('%s<dl><dt>%s\n<dd>%s\n</dl>\n' return ('%s<dl><dt>%s\n<dd>%s\n</dl>\n'
% (before,ctag(t),d)) % (before,t,d))
def normal(self,before,p,after): def normal(self,before,p,after):
return '%s<p>%s</p>\n%s\n' % (before,ctag(p),after) return '%s<p>%s</p>\n%s\n' % (before,ctag(p),after)
def pre(self,structure,tagged=0):
if not structure: return ''
if tagged:
r=''
else:
r='<PRE>\n'
for s in structure:
r="%s%s\n\n%s" % (r,html_quote(s[0]),self.pre(s[1],1))
if not tagged: r=r+'</PRE>\n'
return r
def _str(self,structure,level): def _str(self,structure,level):
r='' r=''
for s in structure: for s in structure:
...@@ -342,17 +386,6 @@ class HTML(StructuredText): ...@@ -342,17 +386,6 @@ class HTML(StructuredText):
r=self.normal(r,s[0],self._str(s[1],level)) r=self.normal(r,s[0],self._str(s[1],level))
return r return r
def pre(self,structure,tagged=0):
if not structure: return ''
if tagged:
r=''
else:
r='<PRE>\n'
for s in structure:
r="%s%s\n\n%s" % (r,html_quote(s[0]),self.pre(s[1],1))
if not tagged: r=r+'</PRE>\n'
return r
def html_quote(v, def html_quote(v,
character_entities=( character_entities=(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment