wikiexpand
Expansion engine for pages written using MediaWiki syntax, based on mwparserfromhell.
Wikiexpand tries to mimic the expansion of transcluded text (templates) and magic words (parser functions and variables).
Usage
First, we need to create an expansion context (wikiexpand.expand.ExpansionContext), and provide a place where to look for our templates (wikiexpand.expand.templates.TemplateStore):
# expansion context
from wikiexpand.expand import ExpansionContext
# a template store using `dict` as storage
from wikiexpand.expand.templates import TemplateDict
tpl = TemplateDict()
# define a simple template
tpl["helloworld"] = "Hello World"
# create the expansion context
ctx = ExpansionContext(templates=tpl)
# expand text transcluding our template
ctx.expand("Lorem {{helloworld}}! Ipsum")
'Lorem Hello World! Ipsum'
Parser functions and magic words
Most common used parser functions and magic words can also be expanded:
# expand text using a parser functions
ctx.expand("{{#if:x|1|0}} {{#if:|1|0}}")
'1 0'
Many magic words provide contextual data which are defined for a site (namespaces, server time, etc.) or a given page (title, url, etc.). In order to be able to expand those magic words, providing site (wikiexpand.expand.context.SiteContext) and page context (wikiexpand.expand.context.PageContext) is required.
# implementation using pywikibot to retrieve info from a Wikimedia site
import pywikibot as pw
from wikiexpand.wiki.context import Wiki
# site context for es.wiktionary.org
eswikt = 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'
Implemented parser functions
#expr *, #if, #ifeq, #ifexist, #rel2abs, #switch, #tag, #time *, #timel, #titleparts, anchorencode, articlepagename, articlepagenamee, articlespace, articlespacee, basepagename, basepagenamee, defaultsort, fullpagename, fullpagenamee, fullurl, lc, lcfirst, namespace, namespacee, ns, padleft, padright, pagename, pagenamee, rootpagename, rootpagenamee, subjectpagename, subjectpagenamee, subjectspace, subjectspacee, subpagename, subpagenamee, talkpagename, talkpagenamee, talkspace, talkspacee, uc, ucfirst, urlencode.
Implemented behaviour of the functions marked with * differs from the reference.
Implemented variables
!, ARTICLEPAGENAME, ARTICLEPAGENAMEE, ARTICLESPACE, ARTICLESPACEE, BASEPAGENAME, BASEPAGENAMEE, CURRENTDAY, CURRENTDAY2, CURRENTDAYNAME, CURRENTDOW, CURRENTHOUR, CURRENTMONTH, CURRENTMONTH1, CURRENTMONTHABBREV, CURRENTMONTHNAME, CURRENTTIME, CURRENTTIMESTAMP, CURRENTWEEK, CURRENTYEAR, FULLPAGENAME, 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 modules
Modules written in Lua and executed using {{#invoke:}} are not recognised, but can be replaced by implementing callable templates (that is, functions that render Wikicode). See the doc in wikiexpand.expand.templates.TemplateStore.callable_templates.
Changelog
0.2.1
fixed dependencies versions
fixed caching for parameterized templates
0.2
new parser function & variable ARTICLEPAGENAME, ARTICLEPAGENAMEE
new parser function & variable SUBPAGENAME, SUBPAGENAMEE
new parser function & variable ROOTPAGENAME, ROOTPAGENAMEE
new parser function & variable BASEPAGENAME, BASEPAGENAMEE
new parser function #titleparts
site context: clean and canonical title perform previous space cleaning and unescaping of html entities.
Metadata
Release files for wikiexpand 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| wikiexpand-0.2.2.tar.gz | 31.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wikiexpand-0.2.2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 67.7 kB
Release files / wikiexpand-0.2.2.tar.gz
| Download URL | wikiexpand-0.2.2.tar.gz |
|---|---|
| Size | 31.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
12e7535e840aacc473584ea11f72d4e475edff266d5949e5fb329193cb527c50
|
|
BLAKE2b-256 checksum How to use checksums |
1caabb6ad41fcd3a97ed5d66fd1af553d5d840590dc86b6acb63a861397cc9d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
|
Release files / wikiexpand-0.2.2-py2.py3-none-any.whl
| Download URL | wikiexpand-0.2.2-py2.py3-none-any.whl |
|---|---|
| Size | 36.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
32a35ed874807daffa378978884bfa871073fec0974ba0a356084643ffbb5fd2
|
|
BLAKE2b-256 checksum How to use checksums |
b7dcf31d26ee3b152daabf6c8e25b97985d29f5b564eebba6ac9c2aa145b53ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5
|
Security for wikiexpand 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.