00001
00002
00003
00004
00005
00006
00007
00008 import cgi, string, sys, cStringIO
00009 from os.path import dirname,join
00010 import keyword, token, tokenize
00011
00012
00013
00014
00015
00016
00017 _KEYWORD = token.NT_OFFSET + 1
00018 _TEXT = token.NT_OFFSET + 2
00019
00020 _styles = {
00021 token.NUMBER: 'repro_number',
00022 token.OP: 'repro_op',
00023 token.STRING: 'repro_string',
00024 tokenize.COMMENT: 'repro_comment',
00025 token.NAME: 'repro_name',
00026 token.ERRORTOKEN: 'repro_error',
00027 _KEYWORD: 'repro_keyword',
00028 _TEXT: 'repro_text',
00029 }
00030
00031 HTML_CODE_TEMPLATE = string.Template(
00032 """
00033 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
00034
00035 <!--created by Repro -->
00036 <HTML>
00037
00038 <HEAD>
00039 <TITLE>${TITLE}</TITLE>
00040 <link type="text/css" rel="stylesheet" href="${stylesheet}">
00041 </HEAD>
00042
00043 <BODY >
00044 <table align="center" width="100%" cellpadding="0" cellspacing="0">
00045 <tr><td class="navigation" align="center" width="100%">${TITLE}</td>
00046 </tr></table>
00047 <BR>
00048 <B> Next:</B> <A NAME="tex2html20"
00049 HREF="node2.html">My subsection</A>
00050 <B>Up:</B> <A NAME="tex2html18"
00051 HREF="paper.html">Some title</A>
00052 <B> Previous:</B> <A NAME="tex2html12"
00053 HREF="paper.html">Some title</A>
00054 <BR><HR>
00055
00056 <!--End of Navigation Panel-->
00057
00058 <pre>
00059 <slab_source_body>
00060 <div class="slab_code_box1">
00061 <div class="slab_code_box2">
00062
00063
00064 """ )
00065
00066 HTML_CODE_TEMPLATE_END = """
00067
00068 </div>
00069 </div>
00070 </slab_source_body>
00071 </pre>
00072 </DIV></TD></TR>
00073 </TABLE>
00074 </DIV>
00075
00076 <P>
00077 <BR><HR>
00078 <ADDRESS>
00079 <I>Generated using repro, a Python package for automating reproducible research in scientific computing <BR>
00080 2008-09-09</I>
00081 </ADDRESS>
00082
00083 </BODY>
00084 </HTML>
00085
00086 """
00087
00088
00089
00090
00091 class Parser:
00092
00093
00094
00095
00096 def __init__(self, raw, stylesheet, title , out = sys.stdout ):
00097 self.raw = string.strip(string.expandtabs(raw))
00098 self.stylesheet =stylesheet
00099 self.title =title
00100
00101 self.out = out
00102
00103
00104
00105
00106 def format(self, formatter, form):
00107
00108 self.lines = [0, 0]
00109 pos = 0
00110 while 1:
00111 pos = string.find(self.raw, '\n', pos) + 1
00112 if not pos: break
00113 self.lines.append(pos)
00114 self.lines.append(len(self.raw))
00115
00116
00117 self.pos = 0
00118 text = cStringIO.StringIO(self.raw)
00119
00120
00121 print >> self.out, HTML_CODE_TEMPLATE.substitute( stylesheet=self.stylesheet, TITLE=self.title )
00122
00123 try:
00124 tokenize.tokenize( text.readline, self )
00125 except tokenize.TokenError, ex:
00126 msg = ex[0]
00127 line = ex[1][0]
00128 self.out.write("<h3>ERROR: %s</h3>%s\n" % (
00129 msg, self.raw[self.lines[line]:]))
00130
00131
00132 print >> self.out, HTML_CODE_TEMPLATE_END
00133
00134
00135
00136
00137 def __call__(self, toktype, toktext, (srow,scol), (erow,ecol), line):
00138
00139
00140
00141
00142
00143 if 0:
00144 print "type", toktype, token.tok_name[toktype], "text", toktext,
00145 print "start", srow,scol, "end", erow,ecol, "<br>"
00146
00147
00148 oldpos = self.pos
00149 newpos = self.lines[srow] + scol
00150 self.pos = newpos + len(toktext)
00151
00152
00153 if toktype in [token.NEWLINE, tokenize.NL]:
00154 self.out.write('\n')
00155 return
00156
00157
00158 if newpos > oldpos:
00159 self.out.write(self.raw[oldpos:newpos])
00160
00161
00162 if toktype in [token.INDENT, token.DEDENT]:
00163 self.pos = newpos
00164 return
00165
00166
00167 if token.LPAR <= toktype and toktype <= token.OP:
00168 toktype = token.OP
00169 elif toktype == token.NAME and keyword.iskeyword(toktext):
00170 toktype = _KEYWORD
00171
00172
00173
00174 slab_style = _styles.get(toktype, _styles[_TEXT] )
00175
00176
00177 self.out.write('<%s>' %(slab_style))
00178
00179 self.out.write(cgi.escape(toktext))
00180
00181 self.out.write('</%s>' %(slab_style) )
00182
00183
00184
00185 def PyHtmlAction( target, source, env ):
00186
00187 src = source[0].get_contents()
00188 tgt = open( str(target[0]), "w+" )
00189 Parser( src, "paper.css", str(source[0]), tgt ).format(None, None)
00190
00191
00192