<s> import re <EOL> from genshi . builder import tag <EOL> from trac . cache import cached <EOL> from trac . config import BoolOption , ListOption <EOL> from trac . core import * <EOL> from trac . resource import IResourceManager <EOL> from trac . util . text import unquote_label <EOL> from trac . util . translation import _ <EOL> from trac . wiki . parser import WikiParser <EOL> class IWikiChangeListener ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def wiki_page_added ( page ) : <EOL> """<STR_LIT>""" <EOL> def wiki_page_changed ( page , version , t , comment , author , ipnr ) : <EOL> """<STR_LIT>""" <EOL> def wiki_page_deleted ( page ) : <EOL> """<STR_LIT>""" <EOL> def wiki_page_version_deleted ( page ) : <EOL> """<STR_LIT>""" <EOL> def wiki_page_renamed ( page , old_name ) : <EOL> """<STR_LIT>""" <EOL> def wiki_page_comment_modified ( page , old_comment ) : <EOL> """<STR_LIT>""" <EOL> class IWikiPageManipulator ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def prepare_wiki_page ( req , page , fields ) : <EOL> """<STR_LIT>""" <EOL> def validate_wiki_page ( req , page ) : <EOL> """<STR_LIT>""" <EOL> class IWikiMacroProvider ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def get_macros ( ) : <EOL> """<STR_LIT>""" <EOL> def get_macro_description ( name ) : <EOL> """<STR_LIT>""" <EOL> def is_inline ( content ) : <EOL> """<STR_LIT>""" <EOL> def expand_macro ( formatter , name , content , args = None ) : <EOL> """<STR_LIT>""" <EOL> class IWikiSyntaxProvider ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def get_wiki_syntax ( ) : <EOL> """<STR_LIT>""" <EOL> def get_link_resolvers ( ) : <EOL> """<STR_LIT>""" <EOL> def parse_args ( args , strict = True ) : <EOL> """<STR_LIT>""" <EOL> largs , kwargs = [ ] , { } <EOL> if args : <EOL> for arg in re . split ( r'<STR_LIT>' , args ) : <EOL> arg = arg . replace ( r'<STR_LIT>' , '<STR_LIT:U+002C>' ) <EOL> if strict : <EOL> m = re . match ( r'<STR_LIT>' , arg ) <EOL> else : <EOL> m = re . match ( r'<STR_LIT>' , arg ) <EOL> if m : <EOL> kw = arg [ : m . end ( ) - <NUM_LIT:1> ] . strip ( ) <EOL> if strict : <EOL> kw = unicode ( kw ) . encode ( '<STR_LIT:utf-8>' ) <EOL> kwargs [ kw ] = arg [ m . end ( ) : ] <EOL> else : <EOL> largs . append ( arg ) <EOL> return largs , kwargs <EOL> def validate_page_name ( pagename ) : <EOL> """<STR_LIT>""" <EOL> return pagename and all ( part not in ( '<STR_LIT>' , '<STR_LIT:.>' , '<STR_LIT:..>' ) for part in pagename . split ( '<STR_LIT:/>' ) ) <EOL> class WikiSystem ( Component ) : <EOL> """<STR_LIT>""" <EOL> implements ( IWikiSyntaxProvider , IResourceManager ) <EOL> change_listeners = ExtensionPoint ( IWikiChangeListener ) <EOL> macro_providers = ExtensionPoint ( IWikiMacroProvider ) <EOL> syntax_providers = ExtensionPoint ( IWikiSyntaxProvider ) <EOL> realm = '<STR_LIT>' <EOL> ignore_missing_pages = BoolOption ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:false>' , <EOL> """<STR_LIT>""" ) <EOL> split_page_names = BoolOption ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:false>' , <EOL> """<STR_LIT>""" ) <EOL> render_unsafe_content = BoolOption ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:false>' , <EOL> """<STR_LIT>""" ) <EOL> safe_schemes = ListOption ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> doc = """<STR_LIT>""" ) <EOL> @ cached <EOL> def pages ( self ) : <EOL> """<STR_LIT>""" <EOL> return set ( name for name , in <EOL> self . env . db_query ( "<STR_LIT>" ) ) <EOL> def get_pages ( self , prefix = None ) : <EOL> """<STR_LIT>""" <EOL> for page in self . pages : <EOL> if not prefix or page . startswith ( prefix ) : <EOL> yield page <EOL> def has_page ( self , pagename ) : <EOL> """<STR_LIT>""" <EOL> return pagename . rstrip ( '<STR_LIT:/>' ) in self . pages <EOL> def resolve_relative_name ( self , pagename , referrer ) : <EOL> """<STR_LIT>""" <EOL> if pagename . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) or pagename in ( '<STR_LIT:.>' , '<STR_LIT:..>' ) : <EOL> return self . _resolve_relative_name ( pagename , referrer ) <EOL> return pagename <EOL> XML_NAME = r"<STR_LIT>" <EOL> PAGE_SPLIT_RE = re . compile ( r"<STR_LIT>" ) <EOL> Lu = '<STR_LIT>' . join ( unichr ( c ) for c in range ( <NUM_LIT:0> , <NUM_LIT> ) if unichr ( c ) . isupper ( ) ) <EOL> Ll = '<STR_LIT>' . join ( unichr ( c ) for c in range ( <NUM_LIT:0> , <NUM_LIT> ) if unichr ( c ) . islower ( ) ) <EOL> def format_page_name ( self , page , split = False ) : <EOL> if split or self . split_page_names : <EOL> return self . PAGE_SPLIT_RE . sub ( r"<STR_LIT>" , page ) <EOL> return page <EOL> def make_label_from_target ( self , target ) : <EOL> """<STR_LIT>""" <EOL> label = target . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if not label : <EOL> return target <EOL> components = label . split ( '<STR_LIT:/>' ) <EOL> for i , comp in enumerate ( components ) : <EOL> if comp not in ( '<STR_LIT>' , '<STR_LIT:.>' , '<STR_LIT:..>' ) : <EOL> label = '<STR_LIT:/>' . join ( components [ i : ] ) <EOL> break <EOL> return self . format_page_name ( label ) <EOL> def get_wiki_syntax ( self ) : <EOL> wiki_page_name = ( <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" <EOL> r"<STR_LIT>" <EOL> % { '<STR_LIT>' : self . Lu , '<STR_LIT>' : self . Ll , '<STR_LIT>' : self . XML_NAME } ) <EOL> def wikipagename_link ( formatter , match , fullmatch ) : <EOL> return self . _format_link ( formatter , '<STR_LIT>' , match , <EOL> self . format_page_name ( match ) , <EOL> self . ignore_missing_pages , match ) <EOL> yield ( r"<STR_LIT>" <EOL> + wiki_page_name , wikipagename_link ) <EOL> def wikipagename_with_label_link ( formatter , match , fullmatch ) : <EOL> page = fullmatch . group ( '<STR_LIT>' ) <EOL> label = fullmatch . group ( '<STR_LIT>' ) <EOL> return self . _format_link ( formatter , '<STR_LIT>' , page , label . strip ( ) , <EOL> self . ignore_missing_pages , match ) <EOL> yield ( r"<STR_LIT>" <EOL> % ( wiki_page_name , WikiParser . QUOTED_STRING ) , <EOL> wikipagename_with_label_link ) <EOL> def internal_free_link ( fmt , m , fullmatch ) : <EOL> page = fullmatch . group ( '<STR_LIT>' ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> label = fullmatch . group ( '<STR_LIT>' ) <EOL> if label is None : <EOL> label = self . make_label_from_target ( page ) <EOL> return self . _format_link ( fmt , '<STR_LIT>' , page , label . strip ( ) , False ) <EOL> yield ( r"<STR_LIT>" <EOL> % ( WikiParser . QUOTED_STRING , WikiParser . QUOTED_STRING ) , <EOL> internal_free_link ) <EOL> def get_link_resolvers ( self ) : <EOL> def link_resolver ( formatter , ns , target , label , fullmatch = None ) : <EOL> if fullmatch is not None : <EOL> groups = fullmatch . groupdict ( ) <EOL> if groups . get ( '<STR_LIT>' ) and not groups . get ( '<STR_LIT:label>' ) : <EOL> label = self . make_label_from_target ( target ) <EOL> return self . _format_link ( formatter , ns , target , label , False ) <EOL> yield ( '<STR_LIT>' , link_resolver ) <EOL> def _format_link ( self , formatter , ns , pagename , label , ignore_missing , <EOL> original_label = None ) : <EOL> pagename , query , fragment = formatter . split_link ( pagename ) <EOL> version = None <EOL> if '<STR_LIT:@>' in pagename : <EOL> pagename , version = pagename . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> if version and query : <EOL> query = '<STR_LIT:&>' + query [ <NUM_LIT:1> : ] <EOL> pagename = pagename . rstrip ( '<STR_LIT:/>' ) or '<STR_LIT>' <EOL> referrer = '<STR_LIT>' <EOL> if formatter . resource and formatter . resource . realm == self . realm : <EOL> referrer = formatter . resource . id <EOL> if pagename . startswith ( '<STR_LIT:/>' ) : <EOL> pagename = pagename . lstrip ( '<STR_LIT:/>' ) <EOL> elif pagename . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) or pagename in ( '<STR_LIT:.>' , '<STR_LIT:..>' ) : <EOL> pagename = self . _resolve_relative_name ( pagename , referrer ) <EOL> else : <EOL> pagename = self . _resolve_scoped_name ( pagename , referrer ) <EOL> label = unquote_label ( label ) <EOL> if '<STR_LIT>' in formatter . perm ( self . realm , pagename , version ) : <EOL> href = formatter . href . wiki ( pagename , version = version ) + query + fragment <EOL> if self . has_page ( pagename ) : <EOL> return tag . a ( label , href = href , class_ = '<STR_LIT>' ) <EOL> else : <EOL> if ignore_missing : <EOL> return original_label or label <EOL> if '<STR_LIT>' in formatter . perm ( self . realm , pagename , version ) : <EOL> return tag . a ( label + '<STR_LIT:?>' , class_ = '<STR_LIT>' , <EOL> href = href , rel = '<STR_LIT>' ) <EOL> else : <EOL> return tag . a ( label + '<STR_LIT:?>' , class_ = '<STR_LIT>' ) <EOL> elif ignore_missing and not self . has_page ( pagename ) : <EOL> return original_label or label <EOL> else : <EOL> return tag . a ( label , class_ = '<STR_LIT>' , <EOL> title = _ ( "<STR_LIT>" ) ) <EOL> def _resolve_relative_name ( self , pagename , referrer ) : <EOL> base = referrer . split ( '<STR_LIT:/>' ) <EOL> components = pagename . split ( '<STR_LIT:/>' ) <EOL> for i , comp in enumerate ( components ) : <EOL> if comp == '<STR_LIT:..>' : <EOL> if base : <EOL> base . pop ( ) <EOL> elif comp != '<STR_LIT:.>' : <EOL> base . extend ( components [ i : ] ) <EOL> break <EOL> return '<STR_LIT:/>' . join ( base ) <EOL> def _resolve_scoped_name ( self , pagename , referrer ) : <EOL> referrer = referrer . split ( '<STR_LIT:/>' ) <EOL> if len ( referrer ) == <NUM_LIT:1> : <EOL> return pagename <EOL> for i in range ( len ( referrer ) - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> name = '<STR_LIT:/>' . join ( referrer [ : i ] ) + '<STR_LIT:/>' + pagename <EOL> if self . has_page ( name ) : <EOL> return name <EOL> if self . has_page ( pagename ) : <EOL> return pagename <EOL> if '<STR_LIT:/>' in pagename : <EOL> ( first , rest ) = pagename . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> for ( i , part ) in enumerate ( referrer ) : <EOL> if first == part : <EOL> anchor = '<STR_LIT:/>' . join ( referrer [ : i + <NUM_LIT:1> ] ) <EOL> if self . has_page ( anchor ) : <EOL> return anchor + '<STR_LIT:/>' + rest <EOL> return '<STR_LIT:/>' . join ( referrer [ : - <NUM_LIT:1> ] ) + '<STR_LIT:/>' + pagename <EOL> def get_resource_realms ( self ) : <EOL> yield self . realm <EOL> def get_resource_description ( self , resource , format , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . format_page_name ( resource . id ) <EOL> def resource_exists ( self , resource ) : <EOL> """<STR_LIT>""" <EOL> if resource . version is None : <EOL> return resource . id in self . pages <EOL> return bool ( self . env . db_query ( <EOL> "<STR_LIT>" , <EOL> ( resource . id , resource . version ) ) ) </s>
