ãTracã®Wikiè¨æ³å¦çãç¥ãããã£ãããYouãã½ã¼ã¹ããèªãã°ãããããã¨ãã¹ã¿ã¼ããã´ãã«æãã¦ããã ããã®ã§ãèªãã§ã¿ããã¨æããPythonåå¼·ãã¦ãªãã§ãããæ£è¦è¡¨ç¾ã¨ãã¯åãã§ããã¨æå¾ ãã¦ã¿ããããããªããªã£ããåå¼·ãããã
- (3/1追è¨) Python ãã¥ã¼ããªã¢ã« http://www.python.jp/doc/release/tut/ ã§å¦ãã ãã¨ã追è¨ãã¾ãã
é·ãã®ã§ã¹ã«ã¼ãã¦ãã ããã
OSXã®æå ã®ç°å¢ã ã¨
/Library/Python/2.5/site-packages/Trac-0.11.2.1-py2.5.egg/trac/wiki/parser.py
ã«ãã£ãã
èä½æ¨©è¡¨è¨
- 詳ãã㯠http://trac.edgewall.org/wiki/TracLicense
# -*- coding: utf-8 -*- # # Copyright (C) 2005-2008 Edgewall Software # Copyright (C) 2003-2006 Jonas Borgström <[email protected]> # Copyright (C) 2004-2006 Christopher Lenz <[email protected]> # Copyright (C) 2005-2007 Christian Boos <[email protected]> # 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 <[email protected]> # Christopher Lenz <[email protected]> # Christian Boos <[email protected]>
import
import re from trac.core import * from trac.notification import EMAIL_LOOKALIKE_PATTERN
ã¯ã©ã¹å®ç¾©
class WikiParser(Component): """wiki subsystem dedicated to the Wiki text parsing."""
- Wiki text ã Parseããã¨ããã§ãããã¨ã
å®æ°
# 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/?!#@](?<!_)" # we don't want "_" SHREF_TARGET_MIDDLE = r"(?:\|(?=[^|\s])|[^|<>\s])" SHREF_TARGET_LAST = r"[\w/=](?<!_)" # we don't want "_" LHREF_RELATIVE_TARGET = r"[/#][^\s\]]*|\.\.?(?:[/#][^\s\]]*)?" XML_NAME = r"[\w:](?<!\d)[\w:.-]*?" # See http://www.w3.org/TR/REC-xml/#id
- è¨æ³ãå®æ°åãã¦ããã¾ããããããã¯å¤æ´å¯è½æ§ãããã¾ããã
- BNFè¨æ³ã¿ããã«ãããã®ãã
å¤æã«ã¼ã«å®ç¾© - åå¦ç
# Sequence of regexps used by the engine _pre_rules = [ # Font styles r"(?P<bolditalic>!?%s)" % BOLDITALIC_TOKEN, r"(?P<bold>!?%s)" % BOLD_TOKEN, r"(?P<italic>!?%s)" % ITALIC_TOKEN, r"(?P<underline>!?%s)" % UNDERLINE_TOKEN, r"(?P<strike>!?%s)" % STRIKE_TOKEN, r"(?P<subscript>!?%s)" % SUBSCRIPT_TOKEN, r"(?P<superscript>!?%s)" % SUPERSCRIPT_TOKEN, r"(?P<inlinecode>!?%s(?P<inline>.*?)%s)" \ % (STARTBLOCK_TOKEN, ENDBLOCK_TOKEN), r"(?P<inlinecode2>!?%s(?P<inline2>.*?)%s)" \ % (INLINE_TOKEN, INLINE_TOKEN)]
- rã¨ããã®ããæ£è¦è¡¨ç¾ãªãã¬ã¼ã¹ã®ã³ãã³ãã£ã½ãã§ãããã
- (3/1追è¨) Pythonã¡ã¢: r"æåå" ã¯ãrawãã©ã¼ãããæååã§ãã¤ã³ã©ã¤ã³å±éããªãæååã¨ããæå³ã ããã§ãã
- 第ï¼å¼æ°ãå¤æå ã§ã第ï¼å¼æ°ã¯è¿éå ã
- è¡ã¬ãã«ã§ã¯ãªããã¤ã³ã©ã¤ã³ã§å¤æããç³»ã®ã«ã¼ã«ãããæã
å¤æã«ã¼ã«å®ç¾©2 - å¾å¦ç
# Rules provided by IWikiSyntaxProviders will be inserted here _post_rules = [ # e-mails r"(?P<email>!?%s)" % EMAIL_LOOKALIKE_PATTERN, # > ... r"(?P<citation>^(?P<cdepth>>(?: *>)*))", # &, < and > to &, < and > r"(?P<htmlescape>[&<>])", # wiki:TracLinks r"(?P<shref>!?((?P<sns>%s):(?P<stgt>%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<lhref>!?\[(?:" r"(?P<rel>%s)|" % LHREF_RELATIVE_TARGET + # ./... or /... r"(?P<lns>%s):(?P<ltgt>%s|[^\]\s]*))" % \ (LINK_SCHEME, QUOTED_STRING) + # wiki:TracLinks or wiki:"trac links" r"(?:\s+(?P<label>%s|[^\]]+))?\])" % QUOTED_STRING), # optional label # [[macro]] call (r"(?P<macro>!?\[\[(?P<macroname>[\w/+-]+)" r"(\]\]|\((?P<macroargs>.*?)\)\]\]))"), # == heading == #hanchor r"(?P<heading>^\s*(?P<hdepth>=+)\s.*\s(?P=hdepth)\s*" r"(?P<hanchor>#%s)?(?:\s|$))" % XML_NAME, # * list r"(?P<list>^(?P<ldepth>\s+)(?:[-*]|\d+\.|[a-zA-Z]\.|[ivxIVX]{1,5}\.) )", # definition:: r"(?P<definition>^\s+((?:%s[^%s]*%s|%s(?:%s{,2}[^%s])*?%s|[^%s%s:]|:[^:])+::)(?:\s+|$))" % (INLINE_TOKEN, INLINE_TOKEN, INLINE_TOKEN, STARTBLOCK_TOKEN, ENDBLOCK[0], ENDBLOCK[0], ENDBLOCK_TOKEN, INLINE_TOKEN, STARTBLOCK[0]), # (leading space) r"(?P<indent>^(?P<idepth>\s+)(?=\S))", # || table || r"(?P<last_table_cell>\|\|\s*$)", r"(?P<table_cell>\|\|)"]
- ããã¼ãã«ã¼ã«å¤ããªãã
- ã§ã11åãããªãã®ã§ãã·ã³ãã«ããã·ã³ãã«ã£ããã
- Wikiããã¹ããããããã®é¨åã«å¯¾å¿ãã表示è¦åãä½ã£ã¦ãããã°ãè¡ã¬ãã«ã®æ§é ã¯ã«ãã¼ã§ãããã
- ãã£ãã®å®æ°ã使ã£ã¦ã«ã¼ã«ãçµã¿ç«ã¦ã¦ããã¿ããã
å¤æã«ã¼ã«å®ç¾©3 - ã³ã¼ããããã¯
_processor_re = re.compile('#\!([\w+-][\w+-/]*)') _processor_param_re = re.compile(r'''(\w+)=(".*?"|'.*?'|\w+)''') _anchor_re = re.compile('[^\w:.-]+', re.UNICODE)
- reãç»å ´ãæ£è¦è¡¨ç¾ã©ã¤ãã©ãªãªã®ãã
- ããã§ã³ã³ãã¤ã«ãã¦ããã°å¾ã§ä½¿ãã¨ãã«ã¯é«éã«å©ç¨ã§ããã¨ãããã¤ã§ãããã
- ã³ã¼ããããã¯ã®å¦çã®ããã®ã«ã¼ã«ãè¨è¿°ãã¦ããããã§ããã
- formatter.py ã® def handle_code_block(self, line) ã§ä½¿ããã¦ããã¿ããã§ãã
__init__ ã¡ã½ãã
def __init__(self): self._compiled_rules = None self._link_resolvers = None self._helper_patterns = None self._external_handlers = None
- ããã¯Cã§ãã mainé¢æ°ã§ããã
- ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã£ã½ããã®ãåæåä¸ã
- Pythonã¯ã¤ã³ãã³ãã§ç¤ºãã®ã§ãæ¬å¼§ãEndããªããdefãéãã¦ããã§ããã
ã¤ã³ã¿ãã§ã¼ã¹
def _get_rules(self): self._prepare_rules() return self._compiled_rules rules = property(_get_rules) def _get_helper_patterns(self): self._prepare_rules() return self._helper_patterns helper_patterns = property(_get_helper_patterns) def _get_external_handlers(self): self._prepare_rules() return self._external_handlers external_handlers = property(_get_external_handlers)
- åå é¨ã¡ã½ãããå¼ã³åºããããå é¨å¤æ°ãå¤ããå¼ã³åºãã¤ã³ã¿ãã§ã¼ã¹å®ç¾©ã£ã½ã
_prepare_rules ã¡ã½ãã
def _prepare_rules(self): from trac.wiki.api import WikiSystem if not self._compiled_rules: helpers = [] handlers = {} syntax = self._pre_rules[:] i = 0 for resolver in WikiSystem(self.env).syntax_providers: for regexp, handler in resolver.get_wiki_syntax(): handlers['i' + str(i)] = handler syntax.append('(?P<i%d>%s)' % (i, regexp)) i += 1 syntax += self._post_rules[:] helper_re = re.compile(r'\?P<([a-z\d_]+)>') for rule in syntax: helpers += helper_re.findall(rule)[1:] rules = re.compile('(?:' + '|'.join(syntax) + ')', re.UNICODE) self._external_handlers = handlers self._helper_patterns = helpers self._compiled_rules = rules
- ã«ã¼ã«å®ç¾©ãçµã¿ç«ã¦ã¦è¿ãé¨åãåºåã¯ï¼ã¤ã®å¤æ°ã
- self._external_handlers = handlers
- self._helper_patterns = helpers
- self._compiled_rules = rules
- å¦çã追ã£ã¦ããã¨ã
- trac.wiki.api ããWikiSystemèªã¿è¾¼ãã§
- WikiSystem.syntax_providers ãã resolver ãåå¾
- resolver㯠[regexp, handler] ã®é åã«ãªã£ã¦ãã
- => å¤æã«ã¼ã«ã handlersããã·ã¥, syntaxé å ã«è©°ãè¾¼ã
- syntaxé åãããã¿ã°åãåãåºããã®ãhelpers
- syntaxé åããORæ¡ä»¶ã§å¼ã£æããæ£è¦è¡¨ç¾
_get_link_resolvers ã¡ã½ãã
def _get_link_resolvers(self): if not self._link_resolvers: from trac.wiki.api import WikiSystem resolvers = {} for resolver in WikiSystem(self.env).syntax_providers: for namespace, handler in resolver.get_link_resolvers(): resolvers[namespace] = handler self._link_resolvers = resolvers return self._link_resolvers link_resolvers = property(_get_link_resolvers)
- ãªã³ã¯ãHTMLã«æ¸ãæããã«ã¼ã«éãä½ã£ã¦ãã
parse ã¡ã½ãã
def parse(self, wikitext): """Parse `wikitext` and produce a WikiDOM tree.""" # obviously still some work to do here ;) return wikitext
- wikiããã¹ããWiki DOM Treeã«å¤æ
- Wiki DOM Treeã¨ããã®ãããã®ã... ã§ãã¾ã ãªã«ããã£ã¦ããªãããã«ã¿ãã
以ä¸ãã ãã ãé·æã§ãã¿ã¾ããã