def execute(macro, args): args = args.split(',') if len(args) != 2 and len(args) != 4: return "invalid arguments: GetCode(uri,spec[,start_line,end_line])" uri = args[0] if 'ros.org/wiki' in uri or 'ros.org/doc' in uri: return "GetCode cannot be used with www.ros.org URLs" specline = args[1] if specline[:2] != '#!': specline = '#!' + specline # Grab uri if not uri: return "invalid arguments: no code uri specified" cache = getattr(macro.request.cfg, 'get_tag_cache', {}) if uri not in cache: cache[uri] = urlopen(uri).readlines() lines = cache[uri] macro.request.cfg.get_tag_cache = dict(cache) if len(args) == 4: start_line = int(args[2]) end_line = int(args[3]) if start_line > end_line: return "invalid arguments: start_line cannot be greater than end_line." lines = lines[start_line - 1:end_line] else: start_line = 1 # Join lines if len(''.join(lines[0].splitlines())) == 0: lines[0] += '\n' code_block = ''.join(lines) out = StringIO.StringIO() macro.request.redirect(out) if len(args) == 4: wikiizer = wiki.Parser( "{{{\n" + specline + " start=%d" % start_line + "\n" + str(code_block) + "\n}}}\n", macro.request) else: wikiizer = wiki.Parser( "''" + uri + "''\n" + "{{{\n" + specline + " start=%d" % start_line + "\n" + str(code_block) + "\n}}}\n", macro.request) wikiizer.format(macro.formatter) result = out.getvalue() macro.request.redirect() del out return result
def execute(macro, text): text = string.replace(string.join(text, ''), '\\n', '\n') out = StringIO.StringIO() macro.request.redirect(out) wikiizer = wiki.Parser(text, macro.request) wikiizer.format(macro.formatter) result = out.getvalue() macro.request.redirect() del out return (result)
def formattext(macro, text): # copied verbatim from MiniPage Macro by Reimar Bauer text = string.replace(string.join(text, ''), '\\n', '\n') out = StringIO.StringIO() macro.request.redirect(out) wikiizer = text_moin_wiki.Parser(text, macro.request, line_anchors=False) wikiizer.format(macro.formatter) result = out.getvalue() macro.request.redirect() del out return (result)
def parse_wiki_markup(self, formatter, text): """ Return result of parsing previously escaped text by MoinMoin wiki parser. Only allowed and therefore unescaped special command is '<<BR>>'. """ unescaped_text = text.replace('<<BR>>', '<<BR>>') request = ScriptContext() buf = StringIO.StringIO() request.redirect(buf) wiki_parser = text_moin_wiki.Parser(unescaped_text, request) wiki_parser.format(formatter) return buf.getvalue().replace('\n', ' ')
def formatContext(self, macro, page, context, maxlines): """ Format search context for each matched page Try to show first maxlines interesting matches context. """ f = self.formatter if not page.page: page.page = Page(self.request, page.page_name) body = page.page.get_raw_body() last = len(body) - 1 lineCount = 0 output = "" next_page = None title = "" titlepat = re.compile("== (.*) ==", re.I) m = titlepat.search(body) if m: title = m.group(1) pat = re.compile("Description:[']*(.*)", re.I) m = pat.search(body) if m: output = m.group(1) nextpat = re.compile("Next Tutorial:[']*(.*)", re.I) m = nextpat.search(body) next_pages = [] if m: next_page = m.group(1) i = 0 linkpat = re.compile("\[\[([^|]*)(\|([^]]*))?\]\]") while 1: m = linkpat.search(next_page, i) if not m: break if m: next_pages.append(m.group(1)) i = m.end() + 1 if output: out = StringIO.StringIO() macro.request.redirect(out) wikiizer = wiki.Parser(output, macro.request) wikiizer.format(macro.formatter) output = out.getvalue() macro.request.redirect() del out return title, output, next_pages
def formatContext(self, macro, page, context, maxlines): """ Format search context for each matched page Try to show first maxlines interesting matches context. """ f = self.formatter if not page.page: page.page = Page(self.request, page.page_name) body = page.page.get_raw_body() last = len(body) - 1 lineCount = 0 output = "" next_page = None pagedict = {} for line in body.split("\n"): if line.startswith("##"): line = line[2:].strip() parts = line.split("=", 1) if len(parts) == 2: pagedict[parts[0].strip()] = parts[1].strip() title = pagedict.get("title", "No Title") description = pagedict.get("description", "No Description") next_pages = [] linkpat = re.compile("\[\[([^|]*)(\|([^]]*))?\]\]") for key, val in pagedict.items(): if key.startswith("next.") and key.find(".link") != -1: m = linkpat.search(val) if m: next_pages.append(m.group(1)) if description: out = StringIO.StringIO() macro.request.redirect(out) wikiizer = wiki.Parser(description, macro.request) wikiizer.format(macro.formatter) description = out.getvalue() macro.request.redirect() del out return title, description, next_pages
def getMarkupText(lines): request = Globs.macro.request formatter = Globs.macro.formatter markup = Globs.markupforbidden for regex in markup.keys(): pattern = re.compile(regex, re.UNICODE + re.VERBOSE + re.MULTILINE) lines, nchanges = pattern.subn(markup[regex], lines) #if nchanges: # debug(regex) out = StringIO.StringIO() request.redirect(out) wikiizer = text_moin_wiki.Parser(lines, request) wikiizer.format(formatter) targettext = out.getvalue() request.redirect() del out return targettext
def execute(macro, args): args = args.split(',') if len(args) != 2 and len(args) != 4: return "invalid arguments: GetCode(uri,spec[,start_line,end_line])" uri = args[0] if 'ros.org/wiki' in uri or 'wiki.ros.org' in uri or 'ros.org/doc' in uri or 'docs.ros.org' in uri: return "GetCode cannot be used with ros.org URLs" specline = args[1] if specline[:2] != '#!': specline = '#!' + specline # Grab uri if not uri: return "invalid arguments: no code uri specified" cache = getattr(macro.request.cfg, 'get_tag_cache', {}) if uri not in cache: try: cache[uri] = urlopen(uri, timeout=macroutils.NETWORK_TIMEOUT).readlines() except (HTTPError, URLError) as e: return "<span style=\"color:red;font-weight:bold\">Could not fetch external code from '%s': %s</span>" % (uri, e) except socket.timeout as e: return "<span style=\"color:red;font-weight:bold\">Timed out while trying to access '%s'</span>" % uri lines = cache[uri] macro.request.cfg.get_tag_cache = dict(cache) if len(args) == 4: start_line = int(args[2]) end_line = int(args[3]) if start_line > end_line: return "invalid arguments: start_line cannot be greater than end_line." lines = lines[start_line - 1:end_line] else: start_line = 1 # Join lines if len(''.join(lines[0].splitlines())) == 0: lines[0] += '\n' code_block = ''.join(lines) out = StringIO.StringIO() macro.request.redirect(out) if len(args) == 4: wikiizer = wiki.Parser("{{{\n" + specline + " start=%d" % start_line + "\n" + str(code_block) + "\n}}}\n", macro.request) else: wikiizer = wiki.Parser("''" + uri + "''\n" + "{{{\n" + specline + " start=%d" % start_line + "\n" + str(code_block) + "\n}}}\n", macro.request) wikiizer.format(macro.formatter) result = out.getvalue() macro.request.redirect() del out return result
def execute(macro, args): args = args.split(',') if len(args) < 3: return "invalid arguments: GetTaggedCode(uri,spec,tag,[{unindent,global_lines,show_uri}])" uri = args[0] if 'ros.org/wiki' in uri or 'ros.org/doc' in uri: return "GetTaggedCode cannot be used with www.ros.org URLs" specline = args[1] if specline[:2] != '#!': specline = '#!' + specline tag = args[2] shift = False if 'unindent' in args: shift = True global_lines = False if 'global_lines' in args: global_lines = True show_uri = False if 'show_uri' in args: show_uri = True no_tag_newlines = False if 'no_tag_newlines' in args: no_tag_newlines = True # Grab uri if not uri: return "invalid arguments: no code uri specified" cache = getattr(macro.request.cfg, 'get_tag_cache', {}) if uri not in cache: cache[uri] = urlopen(uri).readlines() lines = cache[uri] macro.request.cfg.get_tag_cache = dict(cache) tagged_lines = [] skip = True indent = 0 count = 1 start_line = 1 last_line = None for l in lines: m = re.search('(\s*).*%(End)?Tag\((.*)\)%', l) # Only include lines containing <WikiCodeTag()> if not m: count += 1 if not skip: if len(l) > indent: tagged_lines.append(l[indent:]) else: tagged_lines.append('\n') elif (m.groups()[1] is None and m.groups()[2].split(',')[0] == tag): minus_one = False if len(m.groups()[2].split(',')) == 2 and \ m.groups()[2].split(',')[1] == '-1' and \ last_line is not None: if len(tagged_lines) >= 1: del tagged_lines[-1] tagged_lines.append(last_line) minus_one = True skip = False if shift: indent = len(m.groups()[0]) if global_lines: start_line = count if minus_one: start_line -= 1 elif (m.groups()[1] == 'End' and m.groups()[2] == tag): break else: # This is required for line-number constancy for nested blocks if not no_tag_newlines: count += 1 if not skip: tagged_lines.append('\n') last_line = l if len(tagged_lines) == 0: return "No tagged region" # Join tagged_lines if len(''.join(tagged_lines[0].splitlines())) == 0: tagged_lines[0] += '\n' code_block = ''.join(tagged_lines) uri_str = "" if show_uri: uri_str = "''" + uri + "''\n" out = StringIO.StringIO() macro.request.redirect(out) wikiizer = wiki.Parser(uri_str + "{{{\n" + specline + " start=%d" % start_line + "\n" + str(code_block) + "\n}}}\n", macro.request) wikiizer.format(macro.formatter) result = out.getvalue() macro.request.redirect() del out return result
def execute(macro, args): args = args.split(',') if len(args) != 3: return "invalid arguments: <<CodeRef(blockname,start_line,end_line)>>" block = args[0] start_line = int(args[1]) end_line = int(args[2]) if not block: return "invalid arguments: no code block specified" if start_line > end_line: return "invalid arguments: start_line cannot be greater than end_line." request = macro.request content = [] page_name = macro.formatter.page.page_name page = Page(request, page_name) body = page.get_raw_body() start_pat = re.compile("{{{\n(#!.*)\n") block_pat = re.compile("block=([-a-z0-9_]*)") end_pat = re.compile("}}}") i = 0 code_block = None specline = None while i < len(body): m = start_pat.search(body, i) if m is None: break if m: specline = m.group(1) m2 = block_pat.search(specline) if m2: _block = m2.group(1) if block == _block: m3 = end_pat.search(body, m.end()) if m3: code_block = body[m.end():m3.start()] else: code_block = "unknown" break i = m.end() if not code_block: return "Error: No code_block found" lines = code_block.split("\n") mylines = lines[start_line - 1:end_line] code_block = string.join(mylines, "\n") out = StringIO.StringIO() macro.request.redirect(out) wikiizer = wiki.Parser( "{{{\n" + specline + " start=%d" % start_line + "\n" + code_block + "\n}}}\n", macro.request) wikiizer.format(macro.formatter) result = out.getvalue() macro.request.redirect() del out return result
def format(self, formatter): raw = self.raw raw = raw.split('\n') parser_name = '' for line in raw: if line.strip().startswith("#!"): parser_name = line.strip()[2:].split()[0] for arg in line.split(','): if arg.find('=') > -1: key, value = arg.split('=') setattr(self, key, wikiutil.escape(value.strip(), quote=1)) pagename = formatter.page.page_name out = StringIO.StringIO() self.request.redirect(out) if parser_name != '': self.request.write(formatter.parser(parser_name, raw)) else: wikiizer = text_moin_wiki.Parser(self.raw, self.request) wikiizer.format(formatter) result = out.getvalue() self.request.redirect() del out if self.div_type in ['id,', 'class'] and self.div_name in [ 'header', 'logo', 'pagetrail', 'navibar', 'editbar', 'pagelocation', 'locationline', 'pageline', 'footer', 'sidepanel', 'pagebottom' ]: # needs to be changed to your css code div = '<div %(type)s="%(val)s">' % { "val": self.div_name, "type": self.div_type } self.request.write("%(div)s%(result)s</div>" % { "div": div, "result": result }) return if self.position in [ 'static', 'absolute', 'relative', 'fixed', 'inherit' ]: position = self.position else: position = 'relative' if self.thick in ['thin', 'medium', 'thick']: thick = self.thick else: units = ['px'] thick = self.value_check(self.thick, units, 1, 0) if self.text_align in ['left', 'right', 'center', 'justify']: text_align = self.text_align else: text_align = 'left' if self.style in [ 'none', 'hidden', 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset' ]: style = self.style else: style = 'solid' if self.color != 'transparent': try: color = web.Color(str(self.color)) except StandardError: color = 'black' else: color = 'black' if self.background != 'transparent': try: background = web.Color(str(self.background)) except StandardError: background = 'transparent' else: background = 'transparent' if self.width: units = ['%'] width = self.value_check(self.width, units, 1, 0) if self.padding: units = ['pt', 'pc', 'in', 'mm', 'cm', 'px', 'em', 'ex'] padding = self.value_check(self.padding, units, 4, 0) if self.margin: units = ['pt', 'pc', 'in', 'mm', 'cm', 'px', 'em', 'ex'] margin = self.value_check(self.margin, units, 4, 0) if self.text_font_size in [ 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', 'smaller', 'larger' ]: text_font_size = self.text_font_size else: units = ['pt', 'pc', 'in', 'mm', 'cm', 'px', 'em', 'ex', '%'] text_font_size = self.value_check(self.text_font_size, units, 1, '') if self.text_color != 'transparent': try: text_color = web.Color(str(self.text_color)) except StandardError: text_color = 'black' else: text_color = 'black' url = '' if self.background_image != None: attachment_path = AttachFile.getAttachDir(self.request, pagename) file = os.path.join(attachment_path, self.background_image) if os.path.exists(file): mime_type, enc = mimetypes.guess_type(file) if mime_type.startswith('image'): url = AttachFile.getAttachUrl(pagename, self.background_image, self.request) if self.background_repeat in [ 'repeat', 'repeat-x', 'repeat-y', 'no-repeat' ]: background_repeat = self.background_repeat else: background_repeat = 'no-repeat' if self.align in ['left', 'right', 'center', 'justify']: div = '<div align="%(align)s" style="border-width:%(thick)s; border-color:%(color)s; border-style:%(style)s; position:%(position)s; padding:%(padding)s; margin:%(margin)s; background-color:%(background)s; font-size:%(text_font_size)s; color:%(text_color)s; background-image:url(%(background_image)s); background-repeat:%(background_repeat)s;" width="%(width)s">' % { "thick": thick, "style": style, "color": color, "position": position, "padding": padding, "margin": margin, "background": background, "width": width, "text_align": text_align, "text_font_size": text_font_size, "text_color": text_color, "background_image": url, "background_repeat": background_repeat, "align": self.align, } self.request.write("%(div)s%(result)s</div>" % { "div": div, "result": result }) if self.align in ['float:left', 'float:right']: tab = '<table style="%(align)s; font-size:%(text_font_size)s; color:%(text_color)s; text-align:%(text_align)s; background-image:url(%(background_image)s); background-repeat:%(background_repeat)s; position:%(position)s; padding:%(padding)s; margin:%(margin)s;" width="%(width)s" border="%(thick)s" bgcolor="%(background)s"><tbody><tr><td style="border-style:none;">' % { "align": self.align, "text_font_size": text_font_size, "text_align": text_align, "text_color": text_color, "position": position, "padding": padding, "margin": margin, "width": width, "thick": thick, "background": background, "background_image": url, "background_repeat": background_repeat, } self.request.write("%(tab)s%(result)s</td></tr></tbody></table>" % { "tab": tab, "result": result }) if self.align in ['clear:both', 'clear:left', 'clear:right']: self.request.write('<br style=%(align)s;>' % {"align": self.align}) if self.align == 'clear': self.request.write('<br style="clear:both;">')