Re: [Trac] patch to run IncludeMacro with Trac 1.6
Jun Omae <[email protected]> Sun, 14 Sep 2025 12:52:50 +0900
| Newsgroups | gmane.comp.version-control.subversion.trac.general |
|---|---|
| Message-ID | <aMY8Eia7Pi0d/sOV@localhost> |
--FdMe/Uvu9HoewDnd Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi, On Sat, Sep 13, 2025 at 04:13:16AM -0700, Ales Horak wrote: > current https://trac-hacks.org/wiki/IncludeMacro does not run under Trac = 1.6 > due to genshi imports >=20 > see the attached patch to make it functional >=20 > --=C2=A0 > ales horak Thanks for the patch! However, it seems to be incomplete. $ pyflakes includemacro/macros.py includemacro/macros.py:214:59: undefined name 'TracHTMLSanitizer' includemacro/macros.py:244:23: undefined name 'ParseError' includemacro/macros.py:255:27: undefined name 'ParseError' includemacro/macros.py:288:20: undefined name 'ParseError' Could you please try the attached patch? --=20 You received this message because you are subscribed to the Google Groups "= Trac Users" group. To unsubscribe from this group and stop receiving emails from it, send an e= mail to trac-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected] To view this discussion visit https://groups.google.com/d/msgid/trac-users/= aMY8Eia7Pi0d/sOV%40localhost. --FdMe/Uvu9HoewDnd Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="includemacro-remove-uses-of-genshi.diff" Index: includemacro/macros.py =================================================================== --- includemacro/macros.py (revision 18705) +++ includemacro/macros.py (working copy) @@ -10,16 +10,11 @@ # try: from urllib2 import urlopen, URLError - from StringIO import StringIO except ImportError: from urllib.request import urlopen from urllib.error import URLError - from io import StringIO import re - -from genshi.filters.html import HTMLSanitizer -from genshi.input import HTMLParser, ParseError from trac.core import TracError, implements from trac.mimeview.api import Mimeview, get_mimetype from trac.perm import IPermissionRequestor @@ -26,14 +21,12 @@ from trac.resource import ResourceNotFound from trac.ticket.model import Ticket from trac.util import as_bool, as_int -from trac.util.html import escape -from trac.util.text import to_unicode -from trac.util.translation import _ +from trac.util.translation import _, cleandoc_ from trac.versioncontrol.api import NoSuchChangeset, NoSuchNode, \ RepositoryManager from trac.web.chrome import web_context from trac.wiki.api import WikiSystem, parse_args -from trac.wiki.formatter import WikiParser, system_message +from trac.wiki.formatter import WikiParser, WikiProcessor, system_message from trac.wiki.macros import WikiMacroBase from trac.wiki.model import WikiPage @@ -44,11 +37,18 @@ basestring = str +class IncludeMacroError(Exception): + + pass + + class IncludeMacro(WikiMacroBase): + _domain = 'messages' + _description = cleandoc_( """A macro to include other resources in wiki pages. More documentation to follow. - """ + """) implements(IPermissionRequestor) @@ -60,6 +60,8 @@ # IWikiMacroProvider methods def expand_macro(self, formatter, name, content): + self.env.log.debug("hello from IncludeMacro") + largs, kwargs = parse_args(content) if len(largs) == 1: largs.append(None) @@ -202,13 +204,8 @@ if dest_format: out = Mimeview(self.env).render(ctxt, dest_format, out, '', None, annotations) - # Escape if needed. - if not self.config.getbool('wiki', 'render_unsafe_content', False): - out = to_unicode(out) - try: - out = HTMLParser(StringIO(out)).parse() | HTMLSanitizer() - except ParseError: - out = escape(out) + # Sanitize + out = WikiProcessor(formatter, 'html').process(out) return out @@ -235,7 +232,7 @@ if match: start = src.count('\n', 0, match.start()) else: - raise ParseError('start regexp "%s" does not match any text' % start) + raise IncludeMacroError('start regexp "%s" does not match any text' % start) if end: end = as_int(end, end) @@ -246,7 +243,7 @@ if match: end = start + src2.count('\n', 0, match.start()) else: - raise ParseError('end regexp "%s" does not match any text' % end) + raise IncludeMacroError('end regexp "%s" does not match any text' % end) else: if start_regex: end += start @@ -279,7 +276,7 @@ dest_format = node.content_type or get_mimetype(path, out) try: out, start, end = self._handle_partial_source(out, start, end) - except ParseError as e: + except IncludeMacroError as e: return system_message(e), None, None, None annotations = lineno and ['lineno'] or None @@ -289,11 +286,11 @@ return out, ctxt, dest_format, annotations def _extract_section(self, text, section): - m1 = re.search("(^\s*(?P<heading>={1,6})\s(.*?)(\#%s)\s*$)" % section, + m1 = re.search(r"(^\s*(?P<heading>={1,6})\s(.*?)(\#%s)\s*$)" % section, text, re.MULTILINE) if m1: stext = text[m1.end(0):] - m2 = re.search("(^\s*%s\s(.*?)(\#%s)?\s*$)" + m2 = re.search(r"(^\s*%s\s(.*?)(\#%s)?\s*$)" % (m1.group('heading'), WikiParser.XML_NAME), stext, re.MULTILINE) if m2: --FdMe/Uvu9HoewDnd--