#!/usr/bin/python # otl2html.py # convert a tab-formatted outline from VIM to HTML # # Copyright 2001 Noel Henson All rights reserved # # ALPHA VERSION!!! # $Revision: 1.52 $ # $Date: 2008/10/11 22:04:09 $ # $Author: noel $ # $Source: /home/noel/active/otl2html/RCS/otl2html.py,v $ # $Locker: $ ########################################################################### # Basic function # # This program accepts text outline files and converts them # to HTML. The outline levels are indicated by tabs. A line with no # tabs is assumed to be part of the highest outline level. # # 10 outline levels are supported. These loosely correspond to the # HTML H1 through H9 tags. Alphabetic, numeric and bullet formats # are also supported. # # CSS support has been added. # ########################################################################### # include whatever mdules we need import sys from string import * from re import * from time import * from os import system,popen ########################################################################### # global variables formatMode = "indent" copyright = "" level = 0 div = 0 silentdiv = 0 slides = 0 hideComments = 0 showTitle = 1 inputFile = "" outline = [] flatoutline = [] inBodyText = 0 # 0: no, 1: text, 2: preformatted text, 3: table styleSheet = "nnnnnn.css" inlineStyle = 0 ########################################################################### # function definitions # usage # print the simplest form of help # input: none # output: simple command usage is printed on the console def showUsage(): print print "Usage:" print "otl2html.py [options] inputfile > outputfile" print "Options" print " -p Presentation: slide show output for use with HtmlSlides." print " -D First-level is divisions (
# input: linein - a single line that may or may not have tabs at the beginning # output: through standard out def handleBodyText(linein,lineLevel): global inBodyText if (inBodyText == 2): print "" if (inBodyText == 3): print "" print "
" + colonStrip(rstrip(lstrip(linein))), # handlePreformattedText # print preformatted text lines with a class indicating level, if style sheets # are being used. otherwise print just
# input: linein - a single line that may or may not have tabs at the beginning # output: through standard out def handlePreformattedText(linein,lineLevel): global inBodyText if (inBodyText == 1): print "" if (inBodyText == 3): print "" print "" + semicolonStrip(rstrip(lstrip(linein))), # isAlignRight # return flag # input: coldata, a string def isAlignRight(coldata): l = len(coldata) if (coldata[0:2] == " ") and (coldata[l-2:l] != " "): return 1 else: return 0 # isAlignLeft # return flag # input: coldata, a string def isAlignLeft(coldata): l = len(coldata) if (coldata[0:2] != " ") and (coldata[l-2:l] == " "): return 1 else: return 0 # isAlignCenter # return flag # input: coldata, a string def isAlignCenter(coldata): l = len(coldata) if (coldata[0:2] == " ") and (coldata[l-2:l] == " "): return 1 else: return 0 # getColumnAlignment(string) # return string # input: coldata # output:or or or def getColumnAlignment(coldata): if isAlignCenter(coldata): return ' ' if isAlignRight(coldata): return ' ' if isAlignLeft(coldata): return ' ' return ' ' # handleTableColumns # return the souce for a row's columns # input: linein - a single line that may or may not have tabs at the beginning # output: string with the columns' source def handleTableColumns(linein,lineLevel): out = "" coldata = lstrip(rstrip(linein)) coldata = coldata.split("|") for i in range(1,len(coldata)-1): out += getColumnAlignment(coldata[i]) out += lstrip(rstrip(coldata[i]))+' ' return out # handleTableHeaders # return the souce for a row's headers # input: linein - a single line that may or may not have tabs at the beginning # output: string with the columns' source def handleTableHeaders(linein,lineLevel): out = "" coldata = lstrip(rstrip(linein)) coldata = coldata.split("|") for i in range(2,len(coldata)-1): out += getColumnAlignment(coldata[i]) out += lstrip(rstrip(coldata[i]))+'' out = replace(out,'" return out # handleTable # print a table, starting with a " if (slides == 0): if (lineLevel == find(linein," ") +1 ) or \ (lineLevel == find(linein,":") +1 ): if (inBodyText != 1): handleBodyText(linein,lineLevel) elif (colonStrip(rstrip(lstrip(linein))) == ""): print "" handleBodyText(linein,lineLevel) else: print colonStrip(rstrip(lstrip(linein))), elif (lineLevel == find(linein,";") +1 ): if (inBodyText != 2): handlePreformattedText(linein,lineLevel) elif (semicolonStrip(rstrip(lstrip(linein))) == ""): print "" handlePreformattedText(linein,lineLevel) else: print semicolonStrip(rstrip(lstrip(linein))), elif (lineLevel == find(linein,"|") +1 ): if (inBodyText != 3): handleTable(linein,lineLevel) elif (pipeStrip(rstrip(lstrip(linein))) == ""): print "" handleTtable(linein,lineLevel) else: print handleTableRow(linein,lineLevel), elif (lineLevel == find(linein,"!!!") +1 ): execProgram(linein) elif (lineLevel == find(linein,"!!") +1 ): includeOutline(linein,lineLevel) elif (lineLevel == find(linein,"!") +1 ): includeFile(linein,lineLevel) else: if (inBodyText == 1): print"" inBodyText = 0 elif (inBodyText == 2): print"" inBodyText = 0 elif (inBodyText == 3): print"" inBodyText = 0 if (silentdiv == 0): print "
- " + lstrip(rstrip(dashStrip(lstrip(linein)))), elif (lineLevel == find(linein,"+ ") +1 ): print " class=\"LN" + str(lineLevel) + "\"", print ">" + lstrip(rstrip(plusStrip(lstrip(linein)))), else: print " class=\"L" + str(lineLevel) + "\"", print ">" + rstrip(lstrip(linein)), else: silentdiv = 0 else: if (lineLevel == 1): if (linein[0] == " "): if (inBodyText == 0): handleBodyText(linein,lineLevel) else: print rstrip(lstrip(linein)), else: print "" print rstrip(lstrip(linein)), print "\n" else: if (lineLevel == find(linein," ") +1 ) or \ (lineLevel == find(linein,":") +1 ): if (inBodyText == 0): handleBodyText(linein,lineLevel) else: print rstrip(lstrip(linein)), else: if (inBodyText == 1): print"" inBodyText = 0 print "
- " + rstrip(lstrip(linein)), # flatten # Flatten a subsection of an outline. The index passed is the outline section # title. All sublevels that are only one level deeper are indcluded in the current # subsection. Then there is a recursion for those items listed in the subsection. # Exits when the next line to be processed is of the same or lower outline level. # (lower means shallower) # input: idx - the index into the outline. The indexed line is the title. # output: adds reformatted lines to flatoutline[] def flatten(idx): if (outline[idx] == ""): return if (len(outline) <= idx): return titleline = outline[idx] titlelevel = getLineLevel(titleline) if (getLineLevel(outline[idx+1]) > titlelevel): if (titleline[titlelevel-1] != " "): flatoutline.append(lstrip(titleline)) exitflag = 0 while (exitflag == 0): if (idx < len(outline)-1): idx = idx + 1 currlevel = getLineLevel(outline[idx]) if (currlevel == titlelevel + 1): if (currlevel == find(outline[idx]," ") +1): flatoutline.append("\t " + lstrip(outline[idx])) else: flatoutline.append("\t" + lstrip(outline[idx])) elif (currlevel <= titlelevel): exitflag = 1 else: exitflag = 1 level = titlelevel return def createCSS(): global styleSheet output = " /* copyright notice and filename */\n" output += "body { \n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 10pt;\n" output += "}\n" output += " /* title at the top of the page */\n" output += "H1 { \n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 14pt;\n" output += " font-weight: bold;\n" output += " text-align: center;\n" output += " color: black;\n" output += " background-color: #ddddee;\n" output += " padding-top: 20px;\n" output += " padding-bottom: 20px;\n" output += "}\n" output += "H2 { \n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 12pt;\n" output += " font-weight: bold;\n" output += " text-align: left;\n" output += " color: black;\n" output += "}\n" output += "H3 { \n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 12pt;\n" output += " text-align: left;\n" output += " color: black;\n" output += "}\n" output += "H4 { \n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 12pt;\n" output += " text-align: left;\n" output += " color: black;\n" output += "}\n" output += "H5 { \n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 10pt;\n" output += " text-align: left;\n" output += " color: black;\n" output += "}\n" output += " /* outline level spacing */\n" output += "OL { \n" output += " margin-left: 1.0em;\n" output += " padding-left: 0;\n" output += " padding-bottom: 8pt;\n" output += "}\n" output += " /* global heading settings */\n" output += "LI { \n" output += " font-family: helvetica,arial,sans-serif;\n" output += " color: black;\n" output += " font-weight: normal;\n" output += " list-style: lower-alpha;\n" output += " padding-top: 4px;\n" output += "}\n" output += " /* level 1 heading overrides */\n" output += "LI.L1 { \n" output += " font-size: 12pt;\n" output += " font-weight: bold;\n" output += " list-style: none;\n" output += "}\n" output += " /* level 2 heading overrides */\n" output += "LI.L2 { \n" output += " font-size: 10pt;\n" output += " font-weight: bold;\n" output += " list-style: none;\n" output += "}\n" output += " /* level 3 heading overrides */\n" output += "LI.L3 { \n" output += " font-size: 10pt;\n" output += " list-style: none;\n" output += "}\n" output += " /* level 4 heading overrides */\n" output += "LI.L4 { \n" output += " font-size: 10pt;\n" output += " list-style: none;\n" output += "}\n" output += " /* level 5 heading overrides */\n" output += "LI.L5 { \n" output += " font-size: 10pt;\n" output += " list-style: none;\n" output += "}\n" output += " /* level 6 heading overrides */\n" output += "LI.L6 { \n" output += " font-size: 10pt;\n" output += " list-style: none;\n" output += "}\n" output += " /* level 7 heading overrides */\n" output += "LI.L7 { \n" output += " font-size: 10pt;\n" output += " list-style: none;\n" output += "}\n" output += " /* level 1 bullet heading overrides */\n" output += "LI.LB1 { \n" output += " font-size: 12pt;\n" output += " font-weight: bold;\n" output += " list-style: disc;\n" output += "}\n" output += " /* level 2 bullet heading overrides */\n" output += "LI.LB2 { \n" output += " font-size: 10pt;\n" output += " font-weight: bold;\n" output += " list-style: disc;\n" output += "}\n" output += " /* level 3 bullet heading overrides */\n" output += "LI.LB3 { \n" output += " font-size: 10pt;\n" output += " list-style: disc;\n" output += "}\n" output += " /* level 4 bullet heading overrides */\n" output += "LI.LB4 { \n" output += " font-size: 10pt;\n" output += " list-style: disc;\n" output += "}\n" output += " /* level 5 bullet heading overrides */\n" output += "LI.LB5 { \n" output += " font-size: 10pt;\n" output += " list-style: disc;\n" output += "}\n" output += " /* level 6 bullet heading overrides */\n" output += "LI.LB6 { \n" output += " font-size: 10pt;\n" output += " list-style: disc;\n" output += "}\n" output += " /* level 7 bullet heading overrides */\n" output += "LI.LB7 { \n" output += " font-size: 10pt;\n" output += " list-style: disc;\n" output += "}\n" output += " /* level 1 numeric heading overrides */\n" output += "LI.LN1 { \n" output += " font-size: 12pt;\n" output += " font-weight: bold;\n" output += " list-style: decimal;\n" output += "}\n" output += " /* level 2 numeric heading overrides */\n" output += "LI.LN2 { \n" output += " font-size: 10pt;\n" output += " font-weight: bold;\n" output += " list-style: decimal;\n" output += "}\n" output += " /* level 3 numeric heading overrides */\n" output += "LI.LN3 { \n" output += " font-size: 10pt;\n" output += " list-style: decimal;\n" output += "}\n" output += " /* level 4 numeric heading overrides */\n" output += "LI.LN4 { \n" output += " font-size: 10pt;\n" output += " list-style: decimal;\n" output += "}\n" output += " /* level 5 numeric heading overrides */\n" output += "LI.LN5 { \n" output += " font-size: 10pt;\n" output += " list-style: decimal;\n" output += "}\n" output += " /* level 6 numeric heading overrides */\n" output += "LI.LN6 { \n" output += " font-size: 10pt;\n" output += " list-style: decimal;\n" output += "}\n" output += " /* level 7 numeric heading overrides */\n" output += "LI.LN7 { \n" output += " font-size: 10pt;\n" output += " list-style: decimal;\n" output += "}\n" output += " /* body text */\n" output += "P {\n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 9pt;\n" output += " font-weight: normal;\n" output += " color: darkgreen;\n" output += "}\n" output += " /* preformatted text */\n" output += "PRE { \n" output += " font-family: fixed,monospace;\n" output += " font-size: 9pt;\n" output += " font-weight: normal;\n" output += " color: darkblue;\n" output += "}\n" output += "\n" output += "TABLE {\n" output += " margin-top: 1em;\n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 12pt;\n" output += " font-weight: normal;\n" output += " border-collapse: collapse;\n" output += "}\n" output += "\n" output += "TH {\n" output += " border: 1px solid black;\n" output += " padding: 0.5em;\n" output += " background-color: #eeddee;\n" output += "}\n" output += "\n" output += "TD {\n" output += " border: 1px solid black;\n" output += " padding: 0.5em;\n" output += " background-color: #ddeeee;\n" output += "}\n" output += "\n" output += "CODE {\n" output += " background-color: yellow;\n" output += "}\n" output += "\n" output += "TABLE.TAB1 {\n" output += " margin-top: 1em;\n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 12pt;\n" output += " font-weight: normal;\n" output += " border-collapse: collapse;\n" output += "}\n" output += "TABLE.TAB2 {\n" output += " margin-top: 1em;\n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 11pt;\n" output += " font-weight: normal;\n" output += " border-collapse: collapse;\n" output += "}\n" output += "TABLE.TAB3 {\n" output += " margin-top: 1em;\n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 10pt;\n" output += " font-weight: normal;\n" output += " border-collapse: collapse;\n" output += "}\n" output += "TABLE.TAB4 {\n" output += " margin-top: 1em;\n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 10pt;\n" output += " font-weight: normal;\n" output += " border-collapse: collapse;\n" output += "}\n" output += "TABLE.TAB5 {\n" output += " margin-top: 1em;\n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 10pt;\n" output += " font-weight: normal;\n" output += " border-collapse: collapse;\n" output += "}\n" output += "TABLE.TAB6 {\n" output += " margin-top: 1em;\n" output += " font-family: helvetica,arial,sans-serif;\n" output += " font-size: 10pt;\n" output += " font-weight: normal;\n" output += " border-collapse: collapse;\n" file = open(styleSheet,"w") file.write(output) def printHeader(linein): global styleSheet, inlineStyle print "" print "
" + getTitleText(linein) + " " print"" print"" print"" try: file = open(styleSheet,"r") except IOError, e: createCSS() file = open(styleSheet,"r") if (styleSheet != "" and inlineStyle == 0): print "" if (styleSheet != "" and inlineStyle == 1): print "" print "" def printFirstLine(linein): print "" print "" print "" + stripTitleText(rstrip(lstrip(linein))) +"
" print "" def printFooter(): global slides, div print "" if (slides == 0 and div == 0): print " " print "