aboutsummaryrefslogtreecommitdiffstats
path: root/mako/pygen.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-03-14 18:34:36 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-03-14 18:34:36 -0400
commite3b34e3d059e34ffd86e1d9c34d171d25b8499ad (patch)
tree1e12923e09d66037c6064bbf22fe2a989a492d6e /mako/pygen.py
parentda4737ce98bd037e86d005cf35f3f08ec0f1cbe2 (diff)
downloadexternal_python_mako-e3b34e3d059e34ffd86e1d9c34d171d25b8499ad.tar.gz
external_python_mako-e3b34e3d059e34ffd86e1d9c34d171d25b8499ad.tar.bz2
external_python_mako-e3b34e3d059e34ffd86e1d9c34d171d25b8499ad.zip
- improve the line counting to take place in the printer; the state
is also tracked for the template as a whole whereas before it was only occurring for the top level node and was therefore incomplete
Diffstat (limited to 'mako/pygen.py')
-rw-r--r--mako/pygen.py36
1 files changed, 31 insertions, 5 deletions
diff --git a/mako/pygen.py b/mako/pygen.py
index d6559e5..62967e3 100644
--- a/mako/pygen.py
+++ b/mako/pygen.py
@@ -37,10 +37,36 @@ class PythonPrinter(object):
self._reset_multi_line_flags()
- def write_blanks(self, num=1):
- self.stream.write("\n" * num)
+ # marker for template source lines; this
+ # is part of source/template line mapping
+ self.last_source_line = -1
+
+ self.last_boilerplate_line = -1
+
+ # mapping of generated python lines to template
+ # source lines
+ self.source_map = {}
+
+ # list of "boilerplate" lines, these are lines
+ # that precede/follow a set of template source-mapped lines
+ self.boilerplate_map = []
+
+
+ def _update_lineno(self, num):
+ if self.last_boilerplate_line <= self.last_source_line:
+ self.boilerplate_map.append(self.lineno)
+ self.last_boilerplate_line = self.lineno
self.lineno += num
+ def start_source(self, lineno):
+ if self.last_source_line != lineno:
+ self.source_map[self.lineno] = lineno
+ self.last_source_line = lineno
+
+ def write_blanks(self, num):
+ self.stream.write("\n" * num)
+ self._update_lineno(num)
+
def write_indented_block(self, block):
"""print a line or lines of python which already contain indentation.
@@ -98,7 +124,7 @@ class PythonPrinter(object):
# write the line
self.stream.write(self._indent_line(line) + "\n")
- self.lineno += 1
+ self._update_lineno(1)
# see if this line should increase the indentation level.
# note that a line can both decrase (before printing) and
@@ -218,13 +244,13 @@ class PythonPrinter(object):
for entry in self.line_buffer:
if self._in_multi_line(entry):
self.stream.write(entry + "\n")
- self.lineno += 1
+ self._update_lineno(1)
else:
entry = entry.expandtabs()
if stripspace is None and re.search(r"^[ \t]*[^# \t]", entry):
stripspace = re.match(r"^([ \t]*)", entry).group(1)
self.stream.write(self._indent_line(entry, stripspace) + "\n")
- self.lineno += 1
+ self._update_lineno(1)
self.line_buffer = []
self._reset_multi_line_flags()