diff --git a/lib/twparser.py b/lib/twparser.py index 25319a4..206ca28 100644 --- a/lib/twparser.py +++ b/lib/twparser.py @@ -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('<> without <>') + 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('<> without <>') @@ -202,7 +206,7 @@ def __repr__(self): return ''.format(self.kind, ident_list([self.text])) def _parse(self, token): - self.text = token[1] + self.text = token[1].replace(' ', '\x16') class ImageCmd(AbstractCmd): @@ -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""" diff --git a/twee.py b/twee.py index fc0d519..920ba2e 100644 --- a/twee.py +++ b/twee.py @@ -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'])) @@ -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): @@ -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 @@ -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 @@ -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 '' +# print('') if __name__ == '__main__': diff --git a/twee2sam.py b/twee2sam.py index 713b324..015bb9a 100644 --- a/twee2sam.py +++ b/twee2sam.py @@ -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) @@ -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) @@ -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': @@ -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') @@ -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):