Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add else to if clauses #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/twparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,12 @@ def _parse_macro(self, token, tokens):
macro = CallMacro(token)
elif kind == 'return':
macro = ReturnMacro(token)
elif kind == 'else':
if not (self._block_stack and self._block_stack[-1].kind == 'if'):
self._warning('<<else>> without <<if>>')
macro = ElseMacro(token)
elif kind == 'endif':
if self._block_stack and self._block_stack[-1].kind == 'if':
if self._block_stack and self._block_stack[-1].kind in ('if', 'else'):
self._block_stack.pop()
else:
self._warning('<<endif>> without <<if>>')
Expand Down Expand Up @@ -202,7 +206,7 @@ def __repr__(self):
return '<cmd {0}{1}>'.format(self.kind, ident_list([self.text]))

def _parse(self, token):
self.text = token[1]
self.text = token[1].replace('&nbsp;', '\x16')


class ImageCmd(AbstractCmd):
Expand Down Expand Up @@ -341,6 +345,9 @@ def _parse(self, token):
self.expr = self.target
return

class ElseMacro(AbstractMacro):
"""Class for else branch of the current macro"""

class ReturnMacro(AbstractMacro):
"""Class for a return-from-subroutine macro"""

Expand Down
57 changes: 28 additions & 29 deletions twee.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python

from __future__ import print_function
import sys, os, getopt, glob
scriptPath = os.path.realpath(os.path.dirname(sys.argv[0]))
sys.path.append(os.sep.join([scriptPath, 'tw', 'lib']))
Expand All @@ -9,7 +10,8 @@


def usage():
print 'usage: twee [-a author] [-t target] [-m mergefile] [-r rss] source1 [source2..]'
print('usage: twee [-a author] [-t target] [-m mergefile]'+
' [-r rss] source1 [source2..]')


def main (argv):
Expand All @@ -24,21 +26,22 @@ def main (argv):
# read command line switches

try:
opts, args = getopt.getopt(argv, 'a:m:p:r:t:', ['author=', 'merge=', 'plugins=', 'rss=', 'target='])
opts, args = getopt.getopt(argv, 'a:m:p:r:t:',
['author=', 'merge=', 'plugins=', 'rss=', 'target='])
except getopt.GetoptError:
usage()
sys.exit(2)

for opt, arg in opts:
if (opt in ('-a', '--author')):
if opt in ('-a', '--author'):
author = arg
elif (opt in ('-m', '--merge')):
elif opt in ('-m', '--merge'):
merge = arg
elif (opt in ('-p', '--plugins')):
elif opt in ('-p', '--plugins'):
plugins = arg.split(',')
elif (opt in ('-r', '--rss')):
elif opt in ('-r', '--rss'):
rss_output = arg
elif (opt in ('-t', '--target')):
elif opt in ('-t', '--target'):
target = arg

# construct a TW object
Expand All @@ -47,10 +50,9 @@ def main (argv):

# read in a file to be merged

if merge != '':
file = open(merge)
tw.addHtml(file.read())
file.close()
if not merge:
with open(merge) as reader:
tw.addHtml(reader.read())

# read source files

Expand All @@ -60,47 +62,44 @@ def main (argv):
for file in glob.glob(arg):
sources.append(file)

if len(sources) == 0:
print 'twee: no source files specified\n'
if not sources:
print('twee: no source files specified\n')
sys.exit(2)

for source in sources:
file = open(source)
tw.addTwee(file.read())
file.close()
with open(source) as reader:
tw.addTwee(reader.read())

# generate RSS if requested

if rss_output != '':
rss_file = open(rss_output, 'w')
tw.toRss().write_xml(rss_file)
rss_file.close()
with open(rss_output, 'w') as rss_file:
tw.toRss().write_xml(rss_file)

# output the target header

# if (target != 'none') and (target != 'plugin'):
# file = open(scriptPath + os.sep + 'targets' + os.sep + target \
# + os.sep + 'header.html')
# with open(scriptPath + os.sep + 'targets' + os.sep + target \
# + os.sep + 'header.html') as reader:
# print(file.read())
# file.close()

# the tiddlers

print TwParser(tw)
print(TwParser(tw))

# print tw.toHtml()
# print(tw.toHtml())

# plugins

# for plugin in plugins:
# file = open(scriptPath + os.sep + 'targets' + os.sep + target \
# + os.sep + 'plugins' + os.sep + plugin + os.sep + 'compiled.html')
# print(file.read())
# file.close()
# with open(scriptPath + os.sep + 'targets' + os.sep + target +
# os.sep + 'plugins' + os.sep + plugin +
# os.sep + 'compiled.html') as reader:
# print(reader.read())

# and close it up

# print '</div></html>'
# print('</div></html>')


if __name__ == '__main__':
Expand Down
16 changes: 10 additions & 6 deletions twee2sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ def out_if(cmd):
process_command_list(cmd.children, True)
script.write(u' 0]\n')

def out_else(cmd):
script.write(u'|')

def out_print(cmd):
# print a numeric qvariable
out_expr(cmd.expr)
Expand Down Expand Up @@ -190,9 +193,8 @@ def register_link(cmd, is_conditional):
def process_command_list(commands, is_conditional=False):
for cmd in commands:
if cmd.kind == 'text':
text = cmd.text.strip()
if text:
out_string(text)
if cmd.text.strip():
out_string(cmd.text)
check_print.pending = True
elif cmd.kind == 'print':
out_print(cmd)
Expand All @@ -215,6 +217,8 @@ def process_command_list(commands, is_conditional=False):
out_set(cmd)
elif cmd.kind == 'if':
out_if(cmd)
elif cmd.kind == 'else':
out_else(cmd)
elif cmd.kind == 'call':
out_call(cmd)
elif cmd.kind == 'return':
Expand All @@ -239,11 +243,12 @@ def process_command_list(commands, is_conditional=False):

if links:
# Outputs the options separated by line breaks, max 28 chars per line
for link, temp_var in links:
for idx, (link, temp_var) in enumerate(links, 1):
if temp_var:
script.write(u'{0}['.format(variables.get_var(temp_var)))

out_string(link.actual_label()[:28] + '\n')
ending = "\n" if idx != len(links) else ""
out_string(link.actual_label()[:28] + ending)

if temp_var:
script.write(u'0]\n')
Expand Down Expand Up @@ -336,7 +341,6 @@ def get_var(self, name):

if name in self.never_used:
self.never_used.remove(name)

return '{0}:'.format(self.vars[name])

def new_temp_var(self):
Expand Down