#!/usr/bin/env python """ avocet2.py - Revised Avocet Processor Copyright 2008, Sean B. Palmer, inamidst.com Licensed under the Eiffel Forum License 2. http://inamidst.com/proj/avocet/ """ import sys, re, fileinput class Avocet(object): def __init__(self, input=None): if input is None: self.input = fileinput.input() else: self.input = input self.block = None self.done = False def test_unordered_list(self): if len(self.block) < 2: return False items = 0 for line in self.block: if line.startswith('* '): items += 1 if items >= 2: return True return False def test_ordered_list(self): if len(self.block) < 2: return False first = self.block[0] if first.startswith('0) '): for line in self.block[1:]: if line.startswith('0) '): return True elif first.startswith('1) '): for line in self.block[1:]: if line.startswith('2) '): return True return False def test_header(self): if len(self.block) != 1: return False first = self.block[0] if len(first) <= 3: return False if first[0] == 'h' and first[1] in '12345' and first[2:4] == '. ': return True return False def test_preformatted(self): if not self.block: return False first = self.block[0] if first == '--': return True return False def test_auto_preformatted(self): if not self.block: return False r_spaces = re.compile(r'(?' for item in items: sys.stdout.write('
  • ') self.inline(item) print '
  • ' print '' print def ordered_list(self): items = [] first = self.block[0] if first.startswith('0) '): for line in self.block: if line.startswith('0) '): line = line[3:] items.append([]) items[-1].append(line) else: counter = 1 for line in self.block: if ')' in line: i = line.index(')') prefix = line[:i] if prefix.isdigit(): n = int(prefix) if n == counter: counter += 1 line = line[3:] items.append([]) items[-1].append(line) print '
      ' for item in items: sys.stdout.write('
    1. ') self.inline(item) print '
    2. ' print '
    ' print def header(self): line = self.block[0] level = line[1] line = line[4:] sys.stdout.write('') self.inline([line]) print '' print def preformatted(self): assert self.block[0] == '--' block = self.block[1:] print '
    '
          while block[-1] != '--': 
             self.inline(block)
             print 
             self.read_block()
             block = self.block
             print 
    
          self.inline(block[:-1])
          print 
          print '
    ' print def auto_preformatted(self): print '
    '
          while self.test_auto_preformatted(): 
             self.inline(self.block)
             print 
             self.read_block()
          print '
    ' print def paragraph(self): sys.stdout.write('

    ') self.inline(self.block) print '

    ' print def inline(self, lines): for line in lines[:-1]: print line sys.stdout.write(lines[-1]) def main(): a = Avocet() a.process() if __name__ == '__main__': main()