#! /usr/bin/env python # This software carries the following license: # # Modified BSD license # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # Steinar Knutsen """htmltrans.py 0.1 Generates nested definition lists in HTML based on documents indenting. (Tabs only.) An empty line ends all lists, a line correctly indented with tabs, but containing only space will insert a

without affecting the list structure. The first line of the source document is taken to be a title, the second line is ignored. Lines starting with '*' (or with a number of tabs before) is assumed to an item in an unordered list. Words surrounded by '_' are assumed to be emphasized. The code has more than its share of sillinesses. """ import re, string class translator: tabcounter = re.compile(r"^(\t*)") def __init__(self, plaintext): self.plaintext = plaintext self.html = [] def indentone(self, stack, prevline): if len(stack) == 0: stack.append('

') self.html.append('
\n') self.html.append(prevline) self.html.append('
\n') stack.append('
') else: i = 1 state = stack[-i] while state == '
  • ': i = i+1 state = stack[-i] if state == '
    ': self.html.append('
    \n') self.html.append(prevline) self.html.append('
    \n') stack.append('
    ') elif state == '
    ': self.html.append('
    \n') stack.append('
    ') self.html.append(prevline) self.html.append('
    \n') stack.append('
    ') def outdentone(self, stack, prevline): state = stack.pop() if state == '
    ': self.html.append(prevline) self.html.append('
    \n') try: if stack[-1] == '
    ': stack.pop() except: pass elif state == '
    ': self.html.append(prevline) elif state == '
  • ': self.html.append(prevline) self.html.append('') self.outdentone(stack, '') def emptystack(self, stack): for element in stack: if element == '
    ': self.html.append('
  • \n') if element == '
  • ': self.html.append('\n') return [] def translate(self): emphasis = previndentlevel = currindentlevel = 0 prevline = '' stack = [] self.html.append('') self.html.append(self.plaintext[0][:-1]) self.html.append('\n') for line in self.plaintext[2:]: currindentlevel = self.tabcounter.match(line).end(1) indentdiff = currindentlevel - previndentlevel if indentdiff > 0: for n in range(indentdiff): self.indentone(stack, prevline) prevline = '' elif indentdiff < 0: for n in range(-indentdiff): self.outdentone(stack, prevline) prevline = '' else: self.html.append(prevline) if line[currindentlevel] == '*': if len(stack) == 0 or stack[-1] != '
  • ': self.html.append('