def main(): import sys, getopt opts,args=getopt.getopt(sys.argv[1:],'tw') if args: [infile]=args s=open(infile,'r').read() else: s=sys.stdin.read() if opts: if filter(lambda o: o[0]=='-w', opts): print 'Content-Type: text/html\n' if s[:2]=='#!': s=ts_regex.sub('^#![^\n]+','',s) r=ts_regex.compile('\([\0-\n]*\n\)') ts_results = r.match_group(s, (1,)) if ts_results: s=s[len(ts_results[1]):] s=str(html_with_references(s)) if s[:4]=='<h1>': t=s[4:find(s,'</h1>')] s='''<html><head><title>%s</title> </head><body> %s </body></html> ''' % (t,s) print s else: print html_with_references(s)
def create(self,aPar,td=ts_regex.compile( '[ \t\n]*||\([^\0|]*\)').match_group): '''parses a table and returns nested list representing the table''' self.table=[] text=filter(None,split(aPar,'\n')) for line in text: row=[] while 1: pos=td(line,(1,)) if not pos:return 0 row.append(pos[1]) if pos[0]==len(line):break line=line[pos[0]:] self.table.append(row) return 1
def untabify(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_level(aString, indent_space=ts_regex.compile('\n\( *\)').search_group, ): '''\ Find the minimum indentation for a string, not counting blank lines. ''' start=0 text='\n'+aString indent=l=len(text) while 1: ts_results = indent_space(text, (1,2), start) if ts_results: start, grps = ts_results i=len(grps[0]) start=start+i+1 if start < l and text[start] != '\n': # Skip blank lines if not i: return (0,aString) if i < indent: indent = i else: return (indent,aString)
def __init__(self, aStructuredString, level=0, paragraph_divider=ts_regex.compile('\(\r?\n *\)+\r?\n'), ): '''Convert a structured text string into a structured text object. Aguments: aStructuredString -- The string to be parsed. level -- The level of top level headings to be created. ''' pat = ' \"([%s0-9-_,./?=@~&]*)\":' % string.letters+ \ '([-:%s0-9_,./?=@#~&]*?)' % string.letters + \ '([.:?;] )' p_reg = re.compile(pat,re.M) aStructuredString = p_reg.sub(r'<a href="\2">\1</a>\3 ' , aStructuredString) pat = ' \"([%s0-9-_,./?=@~&]*)\", ' % string.letters+ \ '([-:%s0-9_,./?=@#~&]*?)' % string.letters + \ '([.:?;] )' p_reg = re.compile(pat,re.M) aStructuredString = p_reg.sub(r'<a href="\2">\1</a>\3 ' , aStructuredString) protoless = aStructuredString.find('<a href=":') if protoless != -1: aStructuredString = re.sub('<a href=":', '<a href="', aStructuredString) self.level=level paragraphs=ts_regex.split(untabify(aStructuredString), paragraph_divider) paragraphs=map(indent_level,paragraphs) self.structure=structure(paragraphs)
def __init__(self, blocks): tname, args, section = blocks[0] args=parse_params(args, name='', start='1',end='-1',size='10', orphan='3',overlap='1',mapping=1, skip_unauthorized=1, previous=1, next=1, expr='', sort='', reverse=1, sort_expr='', reverse_expr='') self.args=args has_key=args.has_key if has_key('sort'): self.sort=sort=args['sort'] if sort=='sequence-item': self.sort='' if has_key('sort_expr'): self.sort_expr=VSEval.Eval(args['sort_expr'], expr_globals) if has_key('reverse_expr'): self.reverse_expr=VSEval.Eval(args['reverse_expr'], expr_globals) if has_key('reverse'): self.reverse=args['reverse'] if has_key('mapping'): self.mapping=args['mapping'] for n in 'start', 'size', 'end': if has_key(n): self.batch=1 for n in 'orphan','overlap','previous','next': if has_key(n) and not self.batch: raise ParseError, ( """ The %s attribute was used but neither of the <code>start</code>, <code>end</code>, or <code>size</code> attributes were used. """ % n, 'in') if has_key('start'): v=args['start'] if type(v)==type(''): try: atoi(v) except: self.start_name_re=ts_regex.compile( '&+'+ join(map(lambda c: "[%s]" % c, v),'')+ '=[0-9]+&+') name,expr=name_param(args,'in',1) if expr is not None: expr=expr.eval self.__name__, self.expr = name, expr self.section=section.blocks if len(blocks) > 1: if len(blocks) != 2: raise ParseError, ( 'too many else blocks', 'in') tname, args, section = blocks[1] args=parse_params(args, name='') if args: ename=name_param(args) if ename != name: raise ParseError, ( 'name in else does not match in', 'in') self.elses=section.blocks
def _str(self,structure,level, # Static bullet=ts_regex.compile('[ \t\n]*[o*-][ \t\n]+\([^\0]*\)' ).match_group, example=ts_regex.compile('[\0- ]examples?:[\0- ]*$' ).search, dl=ts_regex.compile('\([^\n]+\)[ \t]+--[ \t\n]+\([^\0]*\)' ).match_group, nl=ts_regex.compile('\n').search, ol=ts_regex.compile( '[ \t]*\(\([0-9]+\|[%s]+\)[.)]\)+[ \t\n]+\([^\0]*\|$\)' % string.letters ).match_group, olp=ts_regex.compile('[ \t]*([0-9]+)[ \t\n]+\([^\0]*\|$\)' ).match_group, ): r='' for s in structure: ts_results = bullet(s[0], (1,)) if ts_results: p = ts_results[1] if s[0][-2:]=='::' and s[1]: ps=self.pre(s[1]) else: ps=self._str(s[1],level) r=self.ul(r,p,ps) continue ts_results = ol(s[0], (3,)) if ts_results: p = ts_results[1] if s[0][-2:]=='::' and s[1]: ps=self.pre(s[1]) else: ps=self._str(s[1],level) r=self.ol(r,p,ps) continue ts_results = olp(s[0], (1,)) if ts_results: p = ts_results[1] if s[0][-2:]=='::' and s[1]: ps=self.pre(s[1]) else: ps=self._str(s[1],level) r=self.ol(r,p,ps) continue ts_results = dl(s[0], (1,2)) if ts_results: t,d = ts_results[1] r=self.dl(r,t,d,self._str(s[1],level)) continue if example(s[0]) >= 0 and s[1]: # Introduce an example, using pre tags: r=self.normal(r,s[0],self.pre(s[1])) continue if s[0][-2:]=='::' and s[1]: # Introduce an example, using pre tags: r=self.normal(r,s[0][:-1],self.pre(s[1])) continue if table.create(s[0]): ## table support. r=self.table(r,table.html(),self._str(s[1],level)) continue else: if nl(s[0]) < 0 and s[1] and s[0][-1:] != ':': # Treat as a heading t=s[0] r=self.head(r,t,level, self._str(s[1],level and level+1)) else: r=self.normal(r,s[0],self._str(s[1],level)) return r