00001 """
00002 MoinMoin - GetCode Macro
00003
00004 <<GetTaggedCode(uri,spec,tagname)>>
00005
00006 """
00007
00008 import re
00009 from urllib import urlopen
00010 import StringIO
00011
00012 from MoinMoin.parser import text_moin_wiki as wiki
00013
00014 Dependencies = ["namespace"]
00015
00016
00017 def execute(macro, args):
00018 args = args.split(',')
00019 if len(args) < 3:
00020 return "invalid arguments: GetTaggedCode(uri,spec,tag,[{unindent,global_lines,show_uri}])"
00021
00022 uri = args[0]
00023 if 'ros.org/wiki' in uri or 'ros.org/doc' in uri:
00024 return "GetTaggedCode cannot be used with www.ros.org URLs"
00025
00026 specline = args[1]
00027
00028 if specline[:2] != '#!':
00029 specline = '#!' + specline
00030
00031 tag = args[2]
00032
00033 shift = False
00034 if 'unindent' in args:
00035 shift = True
00036
00037 global_lines = False
00038 if 'global_lines' in args:
00039 global_lines = True
00040
00041 show_uri = False
00042 if 'show_uri' in args:
00043 show_uri = True
00044
00045 no_tag_newlines = False
00046 if 'no_tag_newlines' in args:
00047 no_tag_newlines = True
00048
00049
00050 if not uri:
00051 return "invalid arguments: no code uri specified"
00052 cache = getattr(macro.request.cfg, 'get_tag_cache', {})
00053 if uri not in cache:
00054 cache[uri] = urlopen(uri).readlines()
00055 lines = cache[uri]
00056 macro.request.cfg.get_tag_cache = dict(cache)
00057
00058 tagged_lines = []
00059
00060 skip = True
00061 indent = 0
00062 count = 1
00063 start_line = 1
00064 last_line = None
00065 for l in lines:
00066 m = re.search('(\s*).*%(End)?Tag\((.*)\)%', l)
00067
00068 if not m:
00069 count += 1
00070 if not skip:
00071 if len(l) > indent:
00072 tagged_lines.append(l[indent:])
00073 else:
00074 tagged_lines.append('\n')
00075 elif (m.groups()[1] is None and m.groups()[2].split(',')[0] == tag):
00076 minus_one = False
00077 if len(m.groups()[2].split(',')) == 2 and \
00078 m.groups()[2].split(',')[1] == '-1' and \
00079 last_line is not None:
00080 if len(tagged_lines) >= 1:
00081 del tagged_lines[-1]
00082 tagged_lines.append(last_line)
00083 minus_one = True
00084 skip = False
00085 if shift:
00086 indent = len(m.groups()[0])
00087 if global_lines:
00088 start_line = count
00089 if minus_one:
00090 start_line -= 1
00091 elif (m.groups()[1] == 'End' and m.groups()[2] == tag):
00092 break
00093 else:
00094
00095 if not no_tag_newlines:
00096 count += 1
00097 if not skip:
00098 tagged_lines.append('\n')
00099 last_line = l
00100
00101 if len(tagged_lines) == 0:
00102 return "No tagged region"
00103
00104
00105 if len(''.join(tagged_lines[0].splitlines())) == 0:
00106 tagged_lines[0] += '\n'
00107 code_block = ''.join(tagged_lines)
00108
00109 uri_str = ""
00110 if show_uri:
00111 uri_str = "''" + uri + "''\n"
00112
00113 out = StringIO.StringIO()
00114 macro.request.redirect(out)
00115 wikiizer = wiki.Parser(uri_str + "{{{\n" + specline
00116 + " start=%d" % start_line + "\n"
00117 + str(code_block) + "\n}}}\n",
00118 macro.request)
00119 wikiizer.format(macro.formatter)
00120 result = out.getvalue()
00121 macro.request.redirect()
00122 del out
00123
00124 return result