def expand_macro(self, formatter, name, content, args=None): req = formatter.req Chrome(self.env).add_jquery_ui(req) add_script(req, 'wikitabs/main.js') if args is None: args = {} class_ = filter(None, args.get('class', '').split()) class_.append('tracwikitabs') args['class'] = ' '.join(class_) div = WikiProcessor(formatter, 'div', args) return div.process(content)
def _macro_formatter(self, match, fullmatch): name = fullmatch.group('macroname') if name.lower() == 'br': return '<br />' args = fullmatch.group('macroargs') try: macro = WikiProcessor(self.env, name) rv = macro.process(self.req, args, True) except Exception, e: self.env.log.error('Macro %s(%s) failed' % (name, args), exc_info=True) rv = system_message('Error: Macro %s(%s) failed' % (name, args), e)
def expand_macro(self, formatter, name, content): macros, content = self._parse_args(content) if len(macros) < 1: raise Exception("Insufficient arguments.") for name in macros: try: macro = WikiProcessor(formatter, name) if macro.error: raise Exception('Failed to load macro %s: \'%s\'' % (name, macro.error)) content = str(macro.process(content)) self.env.log.debug('Macro %s returned \'%s\'' % (name, content)) except Exception, e: self.env.log.error('Macro %s(%s) failed' % (name, content), exc_info=True) raise e
def render_macro(self, req, name, content): macros, content = self._parse_args(content) if len(macros) < 1: raise Exception("Insufficient arguments.") for name in macros: try: macro = WikiProcessor(self.env, name) if macro.error: raise Exception('Failed to load macro %s: \'%s\'' % (name, macro.error)) content = macro.process(req, content) self.env.log.debug('Macro %s returned \'%s\'' % (name, content)) except Exception, e: self.env.log.error('Macro %s(%s) failed' % (name, content), exc_info=True) raise e
def _macro_formatter(self, match, fullmatch): name = fullmatch.group('macroname') if name in ['br', 'BR']: return '<br />' elif name in ['html']: args = fullmatch.group('macroargs') args = unescape(args) try: macro = WikiProcessor(self.env, name) return macro.process(self.req, args, 1) except Exception, e: self.env.log.error('Macro %s(%s) failed' % (name, args), exc_info=True) return system_message('Error: Macro %s(%s) failed' % (name, args), e) return match
def _macro_formatter(self, match, fullmatch): name = fullmatch.group('macroname') if name.lower() == 'latexdoccls': self.begindoc = True if name.lower() == 'latexenddoc': self.enddoc = True if name.lower() == 'br': return os.linesep + r'\\' + os.linesep name = 'Tex' + name args = fullmatch.group('macroargs') try: macro = WikiProcessor(self, name) return macro.process(args, True) except Exception, e: self.env.log.error('Macro %s(%s) failed' % (name, args), exc_info=True) return None
def pre_process_request(self, req, handler): if req.path_info.startswith('/wiki'): if req.method == 'POST' and req.args.get('action', 'view') == 'view': post_handler = None for poster in self.macro_posters: if not hasattr(poster, 'match_macro_post'): continue rv = poster.match_macro_post(req) if isinstance(rv, (str, unicode)): rv = rv in req.args.keys() if rv: post_handler = poster break if post_handler: post_handler.process_macro_post(req) else: # Silly stuff here self.log.debug( 'MacroPostModule: Unclaimed POST, scanning page %s', req.path_info[6:]) page = WikiPage(self.env, req.path_info[6:]) matches = self.macro_re.findall( page.text) + self.proc_re.findall(page.text) for name in matches: self.log.debug('MacroPostModule: Found macro "%s"', name) resource = Resource('wiki', name) context = Context.from_request(req, resource) wp = WikiProcessor(Formatter(self.env, context), name) if wp.macro_provider is None: self.log.debug( 'MacroPostModule: Invalid name!!! How did that happen' ) continue if hasattr(wp.macro_provider, 'process_macro_post') and \ not hasattr(wp.macro_provider, 'match_macro_post'): wp.macro_provider.process_macro_post(req) req.environ['REQUEST_METHOD'] = 'GET' # Revert back to a GET return handler
def code_formatter(env, context, language, text): processor = WikiProcessor(Formatter(env, context), language) html = processor.process(text) raw = nodes.raw('', html, format='html') return raw
class CKEditorFormatter(Formatter): """Extends base wiki formatter by setting code processor's name for code blocks. Thus CKEditor can save it, so it could be processed later by format processor like Pygments (see TracSyntaxColoring). """ data_code_style = None def __init__(self, env, context, wikidom, accepted_code_processors): self.env = env self.context = context self.accepted_code_processors = accepted_code_processors if isinstance(wikidom, basestring): wikidom = WikiParser(env).parse(wikidom) self.wikidom = wikidom Formatter.__init__(self, env, context) # copied from HtmlFormatter def generate(self, escape_newlines=False): """Generate HTML elements. newlines in the wikidom will be preserved if `escape_newlines` is set. """ # FIXME: compatibility code only for now out = StringIO() self.format(self.wikidom, out, escape_newlines) # self.env.log.debug('generated html: %s' % out.getvalue()) return Markup(out.getvalue()) def handle_code_block(self, line, startmatch=None): """Overrides Formatter.handle_code_block, so it adds an additional `pre`-tag with attribute `data-code-style`, in which the code-format is saved. Furthermore the code block is converted into HTML, because otherwise CKEditor ignores empty lines. In this method linebreaks `\n` are replaced by `<br/>`. """ handle_code_style = False if line.strip() == WikiParser.ENDBLOCK and self.code_processor: clean_processor_name = self.code_processor.name self.env.log.debug('clean_processor_name: %s' % clean_processor_name) idx = clean_processor_name.find('; ') if idx >= 0: clean_processor_name = clean_processor_name[:idx] if clean_processor_name == 'default': handle_code_style = True self.data_code_style = '' elif clean_processor_name not in ['diff', 'td']: try: from pygments.lexers import get_lexer_for_mimetype lexer = get_lexer_for_mimetype(clean_processor_name) proc_aliases = lexer.aliases if proc_aliases and len(proc_aliases) > 0: clean_processor_name = proc_aliases[0] else: clean_processor_name = lexer.name if clean_processor_name in self.accepted_code_processors: self.data_code_style = ' data-code-style="%s"' % clean_processor_name handle_code_style = True except Exception, e: self.env.log.warn( "Error when retrieving lexer by mimetype: %s" % e ) self.data_code_style = '' if handle_code_style: self.env.log.debug('processing self.data_code_style: %s' % self.data_code_style) code_text = os.linesep.join(self.code_buf) html_text = WikiProcessor(self, 'default').process(code_text) html_text = _markup_to_unicode( html_text ) html_text = html_text.replace('\n', '<br/>') html = HTML( html_text ) html |= Transformer('//pre').unwrap() buffer = StringIO() html.render(out=buffer, encoding='utf-8') self.out.write( '<pre%s>' % self.data_code_style ) self.out.write( _markup_to_unicode( buffer.getvalue() ) ) self.out.write('</pre>') self.in_code_block = 0 else: Formatter.handle_code_block(self, line, startmatch)
def code_formatter(language, text): processor = WikiProcessor(self.env, language) html = processor.process(req, text) raw = nodes.raw('', html, format='html') return raw