Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
e14073a1
Commit
e14073a1
authored
Apr 27, 2014
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clean up new annotated code writer
parent
cda1e86f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
51 deletions
+46
-51
Cython/Compiler/Annotate.py
Cython/Compiler/Annotate.py
+46
-51
No files found.
Cython/Compiler/Annotate.py
View file @
e14073a1
...
...
@@ -92,12 +92,12 @@ class AnnotationCCodeWriter(CCodeWriter):
def
save_annotation
(
self
,
source_filename
,
target_filename
):
with
Utils
.
open_source_file
(
source_filename
)
as
f
:
lines
=
f
.
readlines
()
code_source_fil
e
=
self
.
code
.
get
(
source_filename
,
{})
code
=
f
.
read
()
generated_cod
e
=
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_fil
e
,
c_file
))
out_buffer
.
write
(
self
.
_save_annotation
(
code
,
generated_cod
e
,
c_file
))
def
_save_annotation_header
(
self
,
c_file
):
outlist
=
[
...
...
@@ -125,44 +125,35 @@ class AnnotationCCodeWriter(CCodeWriter):
def
_save_annotation_footer
(
self
):
return
(
u'</body></html>
\
n
'
,)
def
_save_annotation
(
self
,
lines
,
code_source_fil
e
,
c_file
=
None
):
def
_save_annotation
(
self
,
code
,
generated_cod
e
,
c_file
=
None
):
"""
lines : original cython source code split by lines
code_source_fil
e : generated c code keyed by line number in original file
generated_cod
e : 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_fil
e
))
outlist
.
extend
(
self
.
_save_annotation_body
(
code
,
generated_cod
e
))
outlist
.
extend
(
self
.
_save_annotation_footer
())
return
''
.
join
(
outlist
)
def
_
save_annotation_body
(
self
,
lines
,
code_source_fil
e
):
try
:
def
_
htmlify_code
(
self
,
cod
e
):
try
:
from
pygments
import
highlight
from
pygments.lexers
import
CythonLexer
from
pygments.formatters
import
HtmlFormatter
# pygments strip whitespace a t begining of file,
# need to compensate
n_empty_lines
=
0
for
l
in
lines
:
if
l
==
'
\
n
'
:
n_empty_lines
=
n_empty_lines
+
1
else
:
break
code
=
''
.
join
(
lines
)
hllines
=
highlight
(
code
,
CythonLexer
(),
HtmlFormatter
(
nowrap
=
True
))
# re-prepend the empty lines
hllines
=
'
\
n
'
*
n_empty_lines
+
hllines
lines
=
hllines
.
split
(
'
\
n
'
)
except
ImportError
:
lines
=
map
(
html_escape
,
lines
)
# no Pygments, just escape the code
return
html_escape
(
code
)
html_code
=
highlight
(
code
,
CythonLexer
(
stripnl
=
False
,
stripall
=
False
),
HtmlFormatter
(
nowrap
=
True
))
# re-prepend the empty lines
return
html_code
def
_save_annotation_body
(
self
,
cython_code
,
generated_code
):
outlist
=
[
u'<div class="cython">'
]
pos_comment_marker
=
u'/*
\
N{HORIZONTAL ELLIPSIS}
*/
\
n
'
new_calls_map
=
dict
(
...
...
@@ -178,42 +169,46 @@ class AnnotationCCodeWriter(CCodeWriter):
return
ur"<span class='%s'>%s</span>"
%
(
group_name
,
match
.
group
(
group_name
))
ln_width
=
len
(
str
(
len
(
lines
)))
lines
=
self
.
_htmlify_code
(
cython_code
).
splitlines
()
line_width
=
len
(
str
(
len
(
lines
)))
for
k
,
line
in
enumerate
(
lines
,
1
):
try
:
c
ode
=
code_source_fil
e
[
k
]
c
_code
=
generated_cod
e
[
k
]
except
KeyError
:
code
=
''
c
_c
ode
=
''
else
:
c
ode
=
_replace_pos_comment
(
pos_comment_marker
,
code
)
if
code
.
startswith
(
pos_comment_marker
):
c
ode
=
code
[
len
(
pos_comment_marker
):]
c
ode
=
html_escape
(
code
)
c
_code
=
_replace_pos_comment
(
pos_comment_marker
,
c_
code
)
if
c
_c
ode
.
startswith
(
pos_comment_marker
):
c
_code
=
c_
code
[
len
(
pos_comment_marker
):]
c
_code
=
html_escape
(
c_
code
)
calls
=
new_calls_map
()
c
ode
=
_parse_code
(
annotate
,
code
)
c
_code
=
_parse_code
(
annotate
,
c_
code
)
score
=
(
5
*
calls
[
'py_c_api'
]
+
2
*
calls
[
'pyx_c_api'
]
+
calls
[
'py_macro_api'
]
+
calls
[
'pyx_macro_api'
])
if
c_code
:
onclick
=
" onclick='toggleDiv(this)'"
expandsymbol
=
'+'
else
:
onclick
=
''
expandsymbol
=
' '
if
code
:
onclick
=
"onclick='toggleDiv(this)'"
expandsymbol
=
'+'
outlist
.
append
(
u"<pre class='cython line score-{score}'"
u"{onclick}>"
outlist
.
append
(
u"<pre class='cython line score-{score}'{onclick}>"
# generate line number with expand symbol in front,
# and the right number of digit
"{expandsymbol}{ln:0{ln_width}d}: {lin
e}</pre>
\
n
"
.
format
(
u"{expandsymbol}{line:0{line_width}d}: {cod
e}</pre>
\
n
"
.
format
(
score
=
score
,
expandsymbol
=
expandsymbol
,
ln_width
=
ln
_width
,
ln
=
k
,
lin
e
=
line
.
rstrip
(),
onclick
=
onclick
expandsymbol
=
expandsymbol
,
line_width
=
line
_width
,
line
=
k
,
cod
e
=
line
.
rstrip
(),
onclick
=
onclick
,
))
if
code
:
outlist
.
append
(
u"<pre class='cython c
ode score-%s'>%s</pre>"
%
(
score
,
code
))
if
c
_c
ode
:
outlist
.
append
(
u"<pre class='cython c
_code score-%s'>%s</pre>"
%
(
score
,
c_
code
))
outlist
.
append
(
u"</div>"
)
return
outlist
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment