Ticket #780: patch_rst_coderole.diff
| File patch_rst_coderole.diff, 8.2 KB (added by tonib, 8 years ago) |
|---|
-
trac/wikimacros/rst.py
43 43 raise EnvironmentError, 'Docutils version >= %s required, %s found' % (docutils_required, __version__) 44 44 45 45 from trac.Href import Href 46 from trac.WikiFormatter import Formatter 46 47 47 48 __docformat__ = 'reStructuredText' 48 49 49 WIKI_LINK = re.compile(r'(?:wiki:)?( ?P<w>[A-Za-z][\w\#\?]*[^\w\#\?]*)') # Links must begin with Letters, \# ? so we can link inside pages.50 #WIKI_LINK = re.compile(r'(?:wiki:)?(?P<w >(^|(?<=[^A-Za-z]))[!]?[A-Z][a-z/]+(?:[A-Z][a-z/]+)+)')50 WIKI_LINK = re.compile(r'(?:wiki:)?(.+)') 51 #WIKI_LINK = re.compile(r'(?:wiki:)?(?P<wikilink>[A-Za-z][\w\-]*[^\w\#\?]*)') 51 52 TICKET_LINK = re.compile(r'(?:#(\d+))|(?:ticket:(\d+))') 52 53 REPORT_LINK = re.compile(r'(?:{(\d+)})|(?:report:(\d+))') 53 54 CHANGESET_LINK = re.compile(r'(?:\[(\d+)\])|(?:changeset:(\d+))') 54 55 FILE_LINK = re.compile(r'(?:browser|repos|source):([^#]+)#?(.*)') 55 56 57 #import trac.Logging 58 #log = trac.Logging.logger_factory(logtype='file', logfile="/tmp/debug_rst.txt", level='ALL') 59 56 60 def _wikipage(href, args): 57 61 return href.wiki(args[0]) 58 62 … … 71 75 return href.browser(path, rev) 72 76 73 77 # TracLink REs and callback functions 74 LINKS = [(WIKI_LINK, _wikipage), 75 (TICKET_LINK, _ticket), 78 LINKS = [(TICKET_LINK, _ticket), 76 79 (REPORT_LINK, _report), 77 80 (CHANGESET_LINK, _changeset), 78 (FILE_LINK, _browser)] 81 (FILE_LINK, _browser), 82 (WIKI_LINK, _wikipage)] 79 83 80 84 81 def trac_get_reference(env, rawtext, text): 85 def trac_get_reference(env, rawtext, link, text): 86 82 87 for (pattern, function) in LINKS: 83 m = pattern.match( text)88 m = pattern.match(link) 84 89 if m: 85 90 g = filter(None, m.groups()) 86 91 missing = False 92 if not text: 93 text = g[0] 87 94 if pattern == WIKI_LINK: 88 if not (env._wiki_pages.has_key(g[0])): 95 pagename = re.search(r'^[^\#]+',g[0]) 96 if not (env._wiki_pages.has_key(pagename.group())): 89 97 missing = True 90 text = text + "?" 98 text = text + "?" 91 99 uri = function(env.href, g) 92 100 reference = nodes.reference(rawtext, text) 93 101 reference['refuri']= uri 94 102 if missing: 95 103 reference.set_class('missing') 96 104 return reference 105 97 106 return None 98 107 99 108 def trac(env, name, arguments, options, content, lineno, … … 119 128 120 129 .. _TracLink: http://projects.edgewall.com/trac/wiki/TracLinks 121 130 """ 122 text = arguments[int(len(arguments) == 2)] 123 reference = trac_get_reference(env, block_text, text) 131 link = arguments[0] 132 if len(arguments) == 2: 133 text = arguments[1] 134 else: 135 text = None 136 reference = trac_get_reference(env, block_text, link, text) 124 137 if reference: 125 138 return reference 126 139 # didn't find a match (invalid TracLink), … … 133 146 134 147 135 148 def trac_role(env, name, rawtext, text, lineno, inliner, options={}, content=[]): 136 reference = trac_get_reference(env, rawtext, text) 149 args = text.split(" ",1) 150 link = args[0] 151 if len(args)==2: 152 text = args[1] 153 else: 154 text = None 155 reference = trac_get_reference(env, rawtext, link, text) 137 156 if reference: 138 157 return [reference], [] 139 158 warning = nodes.warning(None, … … 159 178 rst.roles.register_local_role('trac', do_trac_role) 160 179 161 180 # The code_block could is taken from the leo plugin rst2 181 def code_formatter(language, text): 182 #log.debug("language '%s' args '%s'", % (language, arguments)) 183 Format = Formatter(hdf, env, None) 184 Format.set_code_processor(language) 185 186 html = Format.code_processor(hdf, text, env) 187 #log.debug("language '%s' htmltext '%s'" % (language, html)) 188 raw = nodes.raw('',html, format='html') #(self, rawsource='', text='', *children, **attributes): 189 return raw 190 191 def code_role(name, rawtext, text, lineno, inliner, options={}, content=[]): 192 args = text.split(":",1) 193 language = args[0] 194 if len(args)==2: 195 text = args[1] 196 else: 197 text = "" 198 #log.debug("coderole '%s' text '%s'" % (language, text)) 199 reference = code_formatter(language, text) 200 return [reference], [] 201 202 203 162 204 def code_block(name,arguments,options,content,lineno,content_offset,block_text,state,state_machine): 163 205 164 206 """Create a code-block directive for docutils. … … 166 208 Usage: .. code-block:: language 167 209 168 210 If the language can be syntax highlighted it will be.""" 169 170 171 172 from trac.WikiFormatter import Formatter173 174 211 language = arguments[0] 212 text = '\n'.join(content) 213 reference = code_formatter(language, text) 214 return [reference] 175 215 176 code_processor = None177 if Formatter.builtin_processors.has_key(language):178 code_processor = Formatter.builtin_processors[language]179 else:180 code_processor = Formatter.builtin_processors['default']181 182 183 html = code_processor(hdf, '\n'.join(content), env)184 raw = nodes.raw('',html, format='html') #(self, rawsource='', text='', *children, **attributes):185 return [raw]186 187 216 # These are documented at http://docutils.sourceforge.net/spec/howto/rst-directives.html. 188 217 code_block.arguments = ( 189 218 1, # Number of required arguments. … … 199 228 code_block.content = 1 # True if content is allowed. 200 229 # Register the directive with docutils. 201 230 rst.directives.register_directive('code-block',code_block) 231 rst.roles.register_local_role('code-block', code_role) 202 232 203 233 204 234 -
trac/WikiFormatter.py
487 487 match = Formatter._processor_re.search(line) 488 488 if match: 489 489 name = match.group(1) 490 if Formatter.builtin_processors.has_key(name): 491 self.code_processor = Formatter.builtin_processors[name] 492 else: 493 try: 494 self.code_processor = self.load_macro(name) 495 except Exception, e: 496 mimeviewer, exists = self.env.mimeview.get_viewer(name) 497 if exists != -1: 498 self.mime_type = name 499 self.code_processor = self.mime_processor 500 else: 501 self.code_text += line + os.linesep 502 self.code_processor = Formatter.builtin_processors['default'] 503 self.out.write('<div class="system-message">Failed to load processor macro %s: %s t %s</div>' % (name, line, e)) 490 self.set_code_processor(name, line) 504 491 else: 505 492 self.code_text += line + os.linesep 506 493 self.code_processor = Formatter.builtin_processors['default'] 507 494 else: 508 495 self.code_text += line + os.linesep 509 496 497 def set_code_processor(self, name, line=""): 498 if Formatter.builtin_processors.has_key(name): 499 self.code_processor = Formatter.builtin_processors[name] 500 else: 501 try: 502 self.code_processor = self.load_macro(name) 503 except Exception, e: 504 mimeviewer, exists = self.env.mimeview.get_viewer(name) 505 if exists != -1: 506 self.mime_type = name 507 self.code_processor = self.mime_processor 508 else: 509 self.code_processor = Formatter.builtin_processors['default'] 510 self.out.write("<div class='system-message'>Failed to load processor macro '%s': '%s' '%s'</div>" % (name, line, e)) 511 self.code_text += line + os.linesep 512 513 514 510 515 def format(self, text, out): 511 516 self.out = out 512 517 self._open_tags = []
