# -*- coding: utf-8 -*- # # Copyright (C) 2005-2006 Edgewall Software # Copyright (C) 2003-2006 Jonas Borgström # Copyright (C) 2004-2006 Christopher Lenz # Copyright (C) 2005-2007 Christian Boos # All rights reserved. # # This software is licensed as described in the file COPYING, which # you should have received as part of this distribution. The terms # are also available at http://trac.edgewall.org/wiki/TracLicense. # # This software consists of voluntary contributions made by many # individuals. For the exact contribution history, see the revision # history and logs, available at http://trac.edgewall.org/log/. # # Author: Jonas Borgström # Christopher Lenz # Christian Boos import re from trac.core import * class WikiParser(Component): """wiki subsystem dedicated to the Wiki text parsing.""" # Some constants used for clarifying the Wiki regexps: BOLDITALIC_TOKEN = "'''''" BOLD_TOKEN = "'''" ITALIC_TOKEN = "''" UNDERLINE_TOKEN = "__" STRIKE_TOKEN = "~~" SUBSCRIPT_TOKEN = ",," SUPERSCRIPT_TOKEN = r"\^" INLINE_TOKEN = "`" STARTBLOCK_TOKEN = r"\{\{\{" STARTBLOCK = "{{{" ENDBLOCK_TOKEN = r"\}\}\}" ENDBLOCK = "}}}" LINK_SCHEME = r"[\w.+-]+" # as per RFC 2396 INTERTRAC_SCHEME = r"[a-zA-Z.+-]*?" # no digits (support for shorthand links) QUOTED_STRING = r"'[^']+'|\"[^\"]+\"" SHREF_TARGET_FIRST = r"[\w/?!#@](?\s])" SHREF_TARGET_LAST = r"[\w/=](?!?%s)" % BOLDITALIC_TOKEN, r"(?P!?%s)" % BOLD_TOKEN, r"(?P!?%s)" % ITALIC_TOKEN, r"(?P!?%s)" % UNDERLINE_TOKEN, r"(?P!?%s)" % STRIKE_TOKEN, r"(?P!?%s)" % SUBSCRIPT_TOKEN, r"(?P!?%s)" % SUPERSCRIPT_TOKEN, r"(?P!?%s(?P.*?)%s)" \ % (STARTBLOCK_TOKEN, ENDBLOCK_TOKEN), r"(?P!?%s(?P.*?)%s)" \ % (INLINE_TOKEN, INLINE_TOKEN)] # Rules provided by IWikiSyntaxProviders will be inserted here _post_rules = [ # e-mails r"(?P\w[\w.]+@\w[\w.]+\w)", # > ... r"(?P^(?P>(?: *>)*))", # &, < and > to &, < and > r"(?P[&<>])", # wiki:TracLinks r"(?P!?((?P%s):(?P%s|%s(?:%s*%s)?)))" \ % (LINK_SCHEME, QUOTED_STRING, SHREF_TARGET_FIRST, SHREF_TARGET_MIDDLE, SHREF_TARGET_LAST), # [wiki:TracLinks with optional label] or [/relative label] (r"(?P!?\[(?:" r"(?P%s)|" % LHREF_RELATIVE_TARGET + # ./... or /... r"(?P%s):(?P%s|[^\]\s]*))" % \ (LINK_SCHEME, QUOTED_STRING) + # wiki:TracLinks or wiki:"trac links" r"(?:\s+(?P