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