00001 """
00002 MoinMoin - GetCode Macro
00003
00004 <<GetCode(uri,start_line,end_line)>>
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) != 2 and len(args) !=4:
00021 return "invalid arguments: GetCode(uri,spec[,start_line,end_line])"
00022
00023 uri = args[0]
00024 specline = args[1]
00025 if specline[:2] != '#!':
00026 specline = '#!'+specline
00027
00028
00029
00030 if not uri:
00031 return "invalid arguments: no code uri specified"
00032 lines = urlopen(uri).readlines()
00033
00034 if len(args) == 4:
00035 start_line = int(args[2])
00036 end_line = int(args[3])
00037
00038 if start_line > end_line:
00039 return "invalid arguments: start_line cannot be greater than end_line."
00040
00041 lines = lines[start_line-1:end_line]
00042 else:
00043 start_line = 1
00044
00045
00046 if len(''.join(lines[0].splitlines())) == 0:
00047 lines[0]+='\n'
00048 code_block = ''.join(lines)
00049
00050 out=StringIO.StringIO()
00051 macro.request.redirect(out)
00052 if len(args) == 4:
00053 wikiizer = wiki.Parser("{{{\n" + specline
00054 +" start=%d" % start_line + "\n"
00055 + str(code_block)+"\n}}}\n",
00056 macro.request)
00057 else:
00058 wikiizer = wiki.Parser("''"+uri+"''\n"
00059 + "{{{\n" + specline
00060 +" start=%d" % start_line + "\n"
00061 + str(code_block)+"\n}}}\n",
00062 macro.request)
00063
00064 wikiizer.format(macro.formatter)
00065 result=out.getvalue()
00066 macro.request.redirect()
00067 del out
00068
00069 return result
00070
00071