00001
00002 """
00003 MoinMoin - CodeRef Macro
00004
00005 <<CodeRef(blockname,start_line,end_line)>>
00006
00007 """
00008
00009 from MoinMoin.Page import Page
00010 from MoinMoin import wikiutil
00011 from MoinMoin.parser.text_moin_wiki import Parser as WikiParser
00012 from MoinMoin.parser import text_moin_wiki as wiki
00013
00014 import re, string, StringIO
00015
00016
00017
00018 Dependencies = ["namespace"]
00019
00020 def execute(macro, args):
00021 args = args.split(',')
00022 if len(args) != 3:
00023 return "invalid arguments: <<CodeRef(blockname,start_line,end_line)>>"
00024
00025 block = args[0]
00026 start_line = int(args[1])
00027 end_line = int(args[2])
00028
00029 if not block:
00030 return "invalid arguments: no code block specified"
00031 if start_line > end_line:
00032 return "invalid arguments: start_line cannot be greater than end_line."
00033
00034 request = macro.request
00035 content = []
00036 page_name = macro.formatter.page.page_name
00037
00038
00039 page = Page(request, page_name)
00040 body = page.get_raw_body()
00041
00042 start_pat = re.compile("{{{\n(#!.*)\n")
00043 block_pat = re.compile("block=([-a-z0-9_]*)")
00044 end_pat = re.compile("}}}")
00045 i = 0
00046 code_block = None
00047 specline = None
00048 while i<len(body):
00049 m = start_pat.search(body, i)
00050 if m is None: break
00051
00052 if m:
00053 specline = m.group(1)
00054 m2 = block_pat.search(specline)
00055 if m2:
00056 _block = m2.group(1)
00057 if block == _block:
00058 m3 = end_pat.search(body, m.end())
00059 if m3:
00060 code_block = body[m.end():m3.start()]
00061 else:
00062 code_block = "unknown"
00063 break
00064 i = m.end()
00065
00066 if not code_block:
00067 return "Error: No code_block found"
00068
00069 lines = code_block.split("\n")
00070 mylines = lines[start_line-1:end_line]
00071 code_block = string.join(mylines, "\n")
00072
00073 out=StringIO.StringIO()
00074 macro.request.redirect(out)
00075 wikiizer = wiki.Parser("{{{\n"+specline + " start=%d" % start_line + "\n" + code_block+"\n}}}\n", macro.request)
00076 wikiizer.format(macro.formatter)
00077 result=out.getvalue()
00078 macro.request.redirect()
00079 del out
00080
00081 return result
00082
00083