Commit d71cc134 authored by Matthias BUSSONNIER's avatar Matthias BUSSONNIER

split _save_annotation on multiple function

will allow refactor of the cython magic not to parse temporary files
and not to leak css into IPython notebooks
parent ef8674b3
......@@ -56,37 +56,20 @@ class AnnotationCCodeWriter(CCodeWriter):
def annotate(self, pos, item):
self.annotations.append((pos, item))
def save_annotation(self, source_filename, target_filename):
with closing(Utils.open_source_file(source_filename)) as f:
lines = f.readlines()
code_source_file = self.code.get(source_filename, {})
c_file = Utils.decode_filename(os.path.basename(target_filename))
html_filename = os.path.splitext(target_filename)[0] + ".html"
with codecs.open(html_filename, "w", encoding="UTF-8") as out_buffer:
out_buffer.write(self._save_annotation(lines, code_source_file , target_filename, c_file))
def _css(self):
"""css template will later allow to choose a colormap"""
return _css_template
def _save_annotation(self, lines, code_source_file , c_file=None):
"""
lines : original cython source code split by lines
code_source_file : generated c code keyed by line number in original file
target filename : name of the file in which to store the generated html
c_file : filename in which the c_code has been written
_js = """
function toggleDiv(id) {
theDiv = document.getElementById(id);
if (theDiv.style.display != 'block') theDiv.style.display = 'block';
else theDiv.style.display = 'none';
}
"""
"""
self.mark_pos(None)
for k, line in enumerate(lines):
for c, cc, html in special_chars:
line = line.replace(c, cc)
lines[k] = line
outlist = []
outlist.append(u'<!DOCTYPE html>\n')
outlist.append(u'<!-- Generated by Cython %s -->\n' % Version.watermark)
outlist.append(u'<html>\n')
outlist.append(u"""
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
_css_template = """
body { font-family: courier; font-size: 12; }
......@@ -111,19 +94,69 @@ body { font-family: courier; font-size: 12; }
.line { margin: 0em }
</style>
<script>
function toggleDiv(id) {
theDiv = document.getElementById(id);
if (theDiv.style.display != 'block') theDiv.style.display = 'block';
else theDiv.style.display = 'none';
}
</script>
"""
def save_annotation(self, source_filename, target_filename):
with closing(Utils.open_source_file(source_filename)) as f:
lines = f.readlines()
code_source_file = self.code.get(source_filename, {})
c_file = Utils.decode_filename(os.path.basename(target_filename))
html_filename = os.path.splitext(target_filename)[0] + ".html"
with codecs.open(html_filename, "w", encoding="UTF-8") as out_buffer:
out_buffer.write(self._save_annotation(lines, code_source_file , c_file))
def _save_annotation_header(self, c_file):
outlist = []
outlist.append(u'<!DOCTYPE html>\n')
outlist.append(u'<!-- Generated by Cython %s -->\n' % Version.watermark)
outlist.append(u'<html>\n')
outlist.append(u"""
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
{css}
</style>
<script>
{js}
</script>
</head>
""")
""".format(css=self._css(), js=self._js))
outlist.append(u'<body>\n')
outlist.append(u'<p>Generated by Cython %s</p>\n' % Version.watermark)
outlist.append(u'<p>Raw output: <a href="%s">%s</a></p>\n' % (c_file, c_file))
if c_file :
outlist.append(u'<p>Raw output: <a href="%s">%s</a></p>\n' % (c_file, c_file))
return outlist
def _save_annotation_footer(self):
return (u'</body></html>\n',)
def _save_annotation(self, lines, code_source_file , c_file=None):
"""
lines : original cython source code split by lines
code_source_file : generated c code keyed by line number in original file
target filename : name of the file in which to store the generated html
c_file : filename in which the c_code has been written
"""
outlist = []
outlist.extend(self._save_annotation_header(c_file))
outlist.extend(self._save_annotation_body(lines, code_source_file , c_file=None))
outlist.extend(self._save_annotation_footer())
return ''.join(outlist)
def _save_annotation_body(self, lines, code_source_file , c_file=None):
outlist=[]
self.mark_pos(None)
for k, line in enumerate(lines):
for c, cc, html in special_chars:
line = line.replace(c, cc)
lines[k] = line
zero_calls = dict((name, 0) for name in
'refnanny py_macro_api py_c_api pyx_macro_api pyx_c_api error_goto'.split())
......@@ -163,8 +196,7 @@ function toggleDiv(id) {
outlist.append(u'</pre>\n')
outlist.append(u"<pre id='line%s' class='code' style='background-color: #%s'>%s</pre>" % (k, color, code))
outlist.append(u'</body></html>\n')
return ''.join(outlist)
return outlist
_parse_code = re.compile(
ur'(?P<refnanny>__Pyx_X?(?:GOT|GIVE)REF|__Pyx_RefNanny[A-Za-z]+)|'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment