基于mwparserfromhell的mediawiki wiki页面扩展引擎
wikiexpand的Python项目详细描述
WikiExpand
用于使用mediawiki语法编写的页面的扩展引擎,基于 mwparserfromhell。
wikiexpand试图模拟转义文本(模板)和 魔术词(解析器函数和变量)。
使用量
首先,我们需要创建一个扩展上下文(wikiexpand.expand.ExpansionContext), 并提供一个地方来查找我们的模板(wikiexpand.expand.templates.TemplateStore):
# expansion contextfromwikiexpand.expandimportExpansionContext# a template store using `dict` as storagefromwikiexpand.expand.templatesimportTemplateDicttpl=TemplateDict()# define a simple templatetpl["helloworld"]="Hello World"# create the expansion contextctx=ExpansionContext(templates=tpl)# expand text transcluding our templatectx.expand("Lorem {{helloworld}}! Ipsum")'Lorem Hello World! Ipsum'
语法分析器函数和幻词
最常用的解析器函数和幻词也可以展开:
# expand text using a parser functionsctx.expand("{{#if:x|1|0}} {{#if:|1|0}}")'1 0'
许多神奇的词汇提供了为站点定义的上下文数据(名称空间, 服务器时间等)或给定页面(标题、url等)。为了能够 扩展那些神奇的单词,提供site(wikiexpand.expand.context.SiteContext) 页上下文(wikiexpand.expand.context.PageContext)是必需的。
# implementation using pywikibot to retrieve info from a Wikimedia siteimportpywikibotaspwfromwikiexpand.wiki.contextimportWiki# site context for es.wiktionary.orgeswikt=Wiki(pw.Site("es","wiktionary"))# set page context for a page named "hello"ctx.set_context(eswikt.page_context("hello"))ctx.expand("Using page context: {{PAGENAME}}, {{TALKSPACE}}. Using site context: [{{fullurl:hello}}], {{NAMESPACE:Template:helloworld}}")'Using page context: hello, Discusión. Using site context: [https://es.wiktionary.org/wiki/hello], Plantilla'
实现的解析器函数
^{TT5}$,^{TT6}$,^{TT7}$,^{TT8}$,^{TT9}$,^{TT10}$, #time``*,``#timel,#titleparts,anchorencode,articlepagename, articlepagenamee,articlespace,articlespacee,basepagename, basepagenamee,defaultsort,fullpagename,fullpagenamee,fullurl, ^{TT24}$,^{TT25}$,^{TT26}$,^{TT27}$,^{TT28}$,^{TT29}$,^{TT30}$, ^{TT31}$,^{TT32}$,^{TT33}$,^{TT34}$,^{TT35}$, subjectpagenamee,subjectspace,subjectspacee,subpagename, subpagenamee,talkpagename,talkpagenamee,talkspace,talkspacee, uc,ucfirst,urlencode。
标记*的函数的实现行为与引用不同。
实施变量
!,ARTICLEPAGENAME,ARTICLEPAGENAMEE,ARTICLESPACE,ARTICLESPACEE, ^{TT53}$,^{TT54}$,^{TT55}$,^{TT56}$, ^{TT57}$,^{TT58}$,^{TT59}$,^{TT60}$, CURRENTMONTH1,CURRENTMONTHABBREV,CURRENTMONTHNAME,CURRENTTIME, ^{TT65}$,^{TT66}$,^{TT67}$,^{TT68}$, FULLPAGENAMEE,LOCALDAY,LOCALDAY2,LOCALDAYNAME,LOCALDOW, LOCALHOUR,LOCALMONTH,LOCALMONTH1,LOCALMONTHABBREV, LOCALMONTHNAME,LOCALTIME,LOCALTIMESTAMP,LOCALWEEK,LOCALYEAR, NAMESPACE,NAMESPACEE,PAGENAME,PAGENAMEE,ROOTPAGENAME, ROOTPAGENAMEE,SUBJECTPAGENAME,SUBJECTPAGENAMEE,SUBJECTSPACE, SUBJECTSPACEE,SUBPAGENAME,SUBPAGENAMEE,TALKPAGENAME, TALKPAGENAMEE,TALKSPACE,TALKSPACEE。
Lua模块
不识别用lua编写并使用{{#invoke:}}执行的模块,但是 可以通过实现可调用模板(即 呈现wikicode)。见医生 wikiexpand.expand.templates.TemplateStore.callable_templates。
变更日志
0.2
- 新的解析器函数&变量articlepagename,articlepagenamee
- 新的解析器函数&variable subagename,subagename
- 新的解析器函数&变量rootpagename,rootpagenamee
- 新的解析器函数&variable basepagename,basepagenamee
- 新的解析器函数titleparts
- 站点上下文:清理和规范标题执行先前的空间清理和HTML实体的取消隐藏。