[文档]classParserInline:def__init__(self):self.ruler=Ruler()forname,rulein_rules:self.ruler.push(name,rule)# Second ruler used for post-processing (e.g. in emphasis-like rules)self.ruler2=Ruler()forname,rule2in_rules2:self.ruler2.push(name,rule2)
[文档]defskipToken(self,state:StateInline)->None:"""Skip single token by running all rules in validation mode; returns `True` if any rule reported success """ok=Falsepos=state.posrules=self.ruler.getRules("")maxNesting=state.md.options["maxNesting"]cache=state.cacheifposincache:state.pos=cache[pos]returnifstate.level<maxNesting:forruleinrules:# Increment state.level and decrement it later to limit recursion.# It's harmless to do here, because no tokens are created.# But ideally, we'd need a separate private state variable for this purpose.state.level+=1ok=rule(state,True)state.level-=1ifok:breakelse:# Too much nesting, just skip until the end of the paragraph.## NOTE: this will cause links to behave incorrectly in the following case,# when an amount of `[` is exactly equal to `maxNesting + 1`:## [[[[[[[[[[[[[[[[[[[[[foo]()## TODO: remove this workaround when CM standard will allow nested links# (we can replace it by preventing links from being parsed in# validation mode)#state.pos=state.posMaxifnotok:state.pos+=1cache[pos]=state.pos
[文档]deftokenize(self,state:StateInline)->None:"""Generate tokens for input range."""ok=Falserules=self.ruler.getRules("")end=state.posMaxmaxNesting=state.md.options["maxNesting"]whilestate.pos<end:# Try all possible rules.# On success, rule should:## - update `state.pos`# - update `state.tokens`# - return trueifstate.level<maxNesting:forruleinrules:ok=rule(state,False)ifok:breakifok:ifstate.pos>=end:breakcontinuestate.pending+=state.src[state.pos]state.pos+=1ifstate.pending:state.pushPending()
[文档]defparse(self,src:str,md,env,tokens:List[Token])->List[Token]:"""Process input string and push inline tokens into `tokens`"""state=StateInline(src,md,env,tokens)self.tokenize(state)rules2=self.ruler2.getRules("")forruleinrules2:rule(state)returnstate.tokens