Python txt2html

From PeformIQ Upgrade
Revision as of 17:31, 24 May 2008 by PeterHarding (talk | contribs) (New page: <pre> #!/usr/bin/python """ txt2html.py, Version 4.5.00 (Update: 15.5.02) Python script by Tobias Polzin, polzin@gmx.de If you like the module, please give me feedback. All rights rese...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
#!/usr/bin/python

"""
txt2html.py, Version 4.5.00 (Update: 15.5.02)
Python script  by Tobias Polzin, polzin@gmx.de

If you like the module, please give me feedback. 
All rights reserved. Rapid creation of HTML and TEX files from txt files with 
the following syntax (indenting lines is used for scoping as in PYTHON): 
>Headlines

Normal text, _bold text_, *emphasised*
there are:
 - descriptions: this is a description text
   the identation is used as in python. 
     * itemizes
     * unsorted lists
Converted to html: 
Headlines

Normal text, bold text, emphasised 
there are: 
  descriptions:
  this is a description text the identation is used as in python. 
    itemizes 
    unsorted lists 
Converted to Latex: 
% Generated by txt2html, polzin@gmx.de
% tt (05.05.2000)
\documentclass[a4paper]{article}
\usepackage[latin1]{inputenc}
\begin{document}


\subsection*{Headlines}

\\
Normal text, \textbf{bold text},  \emph{emphasised}
\\
there are:
\begin{description}
\item[descriptions:]
    this is a description text
   the identation is used as in python. 
\begin{itemize}
\item
       itemizes
\item
       unsorted lists
\end{itemize}
\end{description}
\end{document}
"""

from sys import argv,exit
from string import split
import time
import re

 
linescan = re.compile(r"(?P *(?P[*>-])? *)"+ r"(?P(?P''', r'''%% Generated by txt2html, polzin@gmx.de %% %s \documentclass[a4paper]{article} \usepackage[latin1]{inputenc} \begin{document} '''][tex] % (filename + 
time.strftime(" (%d.%m.%Y)", time.localtime(time.time())))
 
EXIT=['', r'\end{document}'][tex]
lastlen    = 80
lastindent = 0
indents    = []
bfison     = 0
 
f = open(filename,"r")

print INTRO

for l in f.readlines():
   m      = linescan.match(l) 
   llen   = len(l)
   indent = len(m.group("indent"))
   # print "% "+`m.group("mode")`,indent,lastindent
   
   if lastindentindent:
      while indents and indents[-1][1]>indent:
         print MODE[indents[-1][0]][2]
         del indents[-1] 
         lastlen=maxlen
         lastindent=indent
         line=m.group("line")
         if m.group("mode"): 
            p=MODE[m.group("mode")][1]
            if "%" in p:
               if m.group("title"): 
                  p=p%m.group("title")
                  line=m.group("notitle")
               else:
                  p=p%line
                  line=""
                  if p:
                     print p
                     llen=maxlen
                  elif (len(split(line))>0 and lastlen+len(split(line)[0])<maxlen):
                   
                     print BR
                     line=emphscan.sub(r" %s\1%s" % (EM[0],EM[1]),line) 
                     line=bfscan.sub(bfinsert,line)
                     if len(line):
                        print " "*indent+line 
                     lastindent=indent
                     lastlen=llen
                     print EXIT