Skip to content

Commit

Permalink
Allow template processing instructions to be split
Browse files Browse the repository at this point in the history
Readability of some templates can be improved by splitting very long
instructions across lines.

Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Aug 30, 2018
1 parent 69df650 commit b8443f5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/pywws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '18.8.0'
_release = '1622'
_commit = 'ae1811f'
_release = '1623'
_commit = '69df650'
35 changes: 21 additions & 14 deletions src/pywws/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
breaks. If you want to output a line break after a processing
instruction, put a blank line immediately after it.
Processing instructions can be split across lines to improve
readability. Split lines are joined together before processing, after
removing any trailing newline characters.
``##``
^^^^^^
Expand Down Expand Up @@ -414,26 +418,29 @@ def jump(idx, count):
else:
tmplt = open(template_file, 'rb')
# do the text processing
while True:
line = tmplt.readline()
if not line:
break
if isinstance(line, bytes) or sys.version_info[0] < 3:
line = line.decode(file_encoding)
line = ''
for new_line in tmplt:
if isinstance(new_line, bytes) or sys.version_info[0] < 3:
new_line = new_line.decode(file_encoding)
line += new_line
parts = line.split('#')
for i in range(len(parts)):
if len(parts) % 2 == 0:
# odd number of '#'
line = line.rstrip('\r\n')
continue
for i, part in enumerate(parts):
if i % 2 == 0:
# not a processing directive
if i == 0 or parts[i] != '\n':
yield parts[i]
if i == 0 or part != '\n':
yield part
continue
if parts[i] and parts[i][0] == '!':
if part and part[0] == '!':
# comment
continue
# Python 2 shlex can't handle unicode
if sys.version_info[0] < 3:
parts[i] = parts[i].encode(file_encoding)
command = shlex.split(parts[i])
part = part.encode(file_encoding)
command = shlex.split(part)
if sys.version_info[0] < 3:
command = map(lambda x: x.decode(file_encoding), command)
if command == []:
Expand Down Expand Up @@ -558,9 +565,9 @@ def jump(idx, count):
if valid_data and loop_count > 0:
tmplt.seek(loop_start, 0)
else:
logger.error(
"Unknown processing directive: #%s#", parts[i])
logger.error("Unknown processing directive: #%s#", part)
return
line = ''

def make_text(self, template_file, live_data=None):
result = u''
Expand Down

0 comments on commit b8443f5

Please sign in to comment.