可爱的 Python: 使用 Spark 模块解析
2007-03-29 12:07:02 来源:WEB开发网清单 6. 删节后的 markupbuilder.py Spark 脚本 class MarkupBuilder(GenericASTBuilder):
"Write out HTML markup based on matched markup"
def p_para(self, args):
'''
para ::= plain
para ::= markup
para ::= para plain
para ::= para emph
para ::= para strong
para ::= para module
para ::= para code
para ::= para title
plain ::= whitespace
plain ::= alphanums
plain ::= contraction
plain ::= safepunct
plain ::= mdash
plain ::= wordpunct
plain ::= plain plain
emph ::= dash plain dash
strong ::= asterisk plain asterisk
module ::= bracket plain bracket
code ::= apostrophe plain apostrophe
title ::= underscore plain underscore
'''
def nonterminal(self, type_, args):
# Flatten AST a bit by not making nodes if only one child.
if len(args)==1: return args[0]
if type_=='para': return nonterminal(self, type_, args)
if type_=='plain':
args[0].attr = foldtree(args[0])+foldtree(args[1])
args[0].type = type_
return nonterminal(self, type_, args[:1])
phrase_node = AST(type_)
phrase_node.attr = foldtree(args[1])
return phrase_node
更多精彩
赞助商链接