py-wikimarkup


Namepy-wikimarkup JSON
Version 2.3.0 PyPI version JSON
download
home_pagehttp://www.github.com/dgilman/py-wikimarkup/
SummaryA basic MediaWiki markup parser.
upload_time2023-01-26 02:44:27
maintainer
docs_urlNone
authorDavid Cramer
requires_python>=3.7
license
keywords
VCS
bugtrack_url
requirements bleach unidecode
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Summary
=======

Formats text following the `MediaWiki <http://meta.wikimedia.org/wiki/Help:Editing>`_
syntax.


Usage
=====

To return HTML from Wiki::

    from wikimarkup.parser import Parser

    parser = Parser()
    html = parser.parse(text[, show_toc=True])

To return HTML without certain "annoying" (TODO: define annoying) elements, such as headings::

    from wikimarkup.parser import parselite

    parselite(text)


Adding New Tags
===============

You can add new tags with the `registerTagHook` method.::

    from wikimarkup.parser import Parser
    import cgi
    
    def blockquoteTagHook(parser_env, body, attributes={}):
        """<quote[ cite="Person"]>A paragraph of text.</quote>"""
        text = ['<blockquote>']
        if 'cite' in attributes:
            text.append('<cite>%s</cite>' % (cgi.escape(attributes['cite']),))
        text.append(parse(body.strip()))
        text.append('</blockquote>')
        return u'\n'.join(text)

    parser = Parser()
    parser.registerTagHook('quote', blockquoteTagHook)


Adding Internal Links
=====================

You can support ``[[internal links]]`` with the `registerInternalLinkHook`
method.  There is no default handling for internal links.  If no hook
handles the link, it will appear unchanged in the output.  An internal
link may have a `namespace:` prefix.  Hooks are registered per namespace,
with 'None' for unprefixed links::

    def internalLinkHook(parser_env, namespace, body):
        ...
        return replacement

    parser.registerInternalLinkHook(None, internalLinkHook)  # called for [[link]]
    parser.registerInternalLinkHook('Wikipedia', hook)       # called for [[Wikipedia: Link]]
    parser.registerInternalLinkHook(':en', hook)             # called for [[:en:link]
    parser.registerInternalLinkHook(':', hook)               # called for [[:any:link]]
    parser.registerInternalLinkHook('*', hook)               # called for [[anything]]


Examples::

    from wikimarkup.parser import Parser

    def wikipediaLinkHook(parser_env, namespace, body):
        # namespace is going to be 'Wikipedia'
        (article, pipe, text) = body.partition('|')
        href = article.strip().capitalize().replace(' ', '_')
        text = (text or article).strip()
        return '<a href="http://en.wikipedia.org/wiki/%s">%s</a>' % (href, text)

    parser = Parser()
    parser.registerInternalLinkHook('Wikipedia', wikipediaLinkHook)

    print parser.parse("[[Wikipedia:public transport|public transportation]]")
    print parser.parse("[[Wikipedia: bus]]")

    import settings
    from pytils.translit import slugify
    from blog.models import Post

    def byteflowLinkHook(parser_env, namespace, body):
        (article, pipe, text) = body.partition('|')
        slug = slugify(article.strip())
        text = (text or article).strip()
        try:
            post = Post.objects.get(slug=slug)
            href = post.get_absolute_url()
        except Post.DoesNotExist:
            href = '#'
        return '<a href="%s">%s</a>' % (href, text)

    parser.registerInternalLinkHook(None, byteflowLinkHook)

    parser.parse("[[Blog post title]]")

            

Raw data

            {
    "_id": null,
    "home_page": "http://www.github.com/dgilman/py-wikimarkup/",
    "name": "py-wikimarkup",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "David Cramer",
    "author_email": "dcramer@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/31/4a/6ea908153b339aafcf4dd25a3a391c0d23efb39f2d21748725f8f2708e44/py-wikimarkup-2.3.0.tar.gz",
    "platform": null,
    "description": "Summary\n=======\n\nFormats text following the `MediaWiki <http://meta.wikimedia.org/wiki/Help:Editing>`_\nsyntax.\n\n\nUsage\n=====\n\nTo return HTML from Wiki::\n\n    from wikimarkup.parser import Parser\n\n    parser = Parser()\n    html = parser.parse(text[, show_toc=True])\n\nTo return HTML without certain \"annoying\" (TODO: define annoying) elements, such as headings::\n\n    from wikimarkup.parser import parselite\n\n    parselite(text)\n\n\nAdding New Tags\n===============\n\nYou can add new tags with the `registerTagHook` method.::\n\n    from wikimarkup.parser import Parser\n    import cgi\n    \n    def blockquoteTagHook(parser_env, body, attributes={}):\n        \"\"\"<quote[ cite=\"Person\"]>A paragraph of text.</quote>\"\"\"\n        text = ['<blockquote>']\n        if 'cite' in attributes:\n            text.append('<cite>%s</cite>' % (cgi.escape(attributes['cite']),))\n        text.append(parse(body.strip()))\n        text.append('</blockquote>')\n        return u'\\n'.join(text)\n\n    parser = Parser()\n    parser.registerTagHook('quote', blockquoteTagHook)\n\n\nAdding Internal Links\n=====================\n\nYou can support ``[[internal links]]`` with the `registerInternalLinkHook`\nmethod.  There is no default handling for internal links.  If no hook\nhandles the link, it will appear unchanged in the output.  An internal\nlink may have a `namespace:` prefix.  Hooks are registered per namespace,\nwith 'None' for unprefixed links::\n\n    def internalLinkHook(parser_env, namespace, body):\n        ...\n        return replacement\n\n    parser.registerInternalLinkHook(None, internalLinkHook)  # called for [[link]]\n    parser.registerInternalLinkHook('Wikipedia', hook)       # called for [[Wikipedia: Link]]\n    parser.registerInternalLinkHook(':en', hook)             # called for [[:en:link]\n    parser.registerInternalLinkHook(':', hook)               # called for [[:any:link]]\n    parser.registerInternalLinkHook('*', hook)               # called for [[anything]]\n\n\nExamples::\n\n    from wikimarkup.parser import Parser\n\n    def wikipediaLinkHook(parser_env, namespace, body):\n        # namespace is going to be 'Wikipedia'\n        (article, pipe, text) = body.partition('|')\n        href = article.strip().capitalize().replace(' ', '_')\n        text = (text or article).strip()\n        return '<a href=\"http://en.wikipedia.org/wiki/%s\">%s</a>' % (href, text)\n\n    parser = Parser()\n    parser.registerInternalLinkHook('Wikipedia', wikipediaLinkHook)\n\n    print parser.parse(\"[[Wikipedia:public transport|public transportation]]\")\n    print parser.parse(\"[[Wikipedia: bus]]\")\n\n    import settings\n    from pytils.translit import slugify\n    from blog.models import Post\n\n    def byteflowLinkHook(parser_env, namespace, body):\n        (article, pipe, text) = body.partition('|')\n        slug = slugify(article.strip())\n        text = (text or article).strip()\n        try:\n            post = Post.objects.get(slug=slug)\n            href = post.get_absolute_url()\n        except Post.DoesNotExist:\n            href = '#'\n        return '<a href=\"%s\">%s</a>' % (href, text)\n\n    parser.registerInternalLinkHook(None, byteflowLinkHook)\n\n    parser.parse(\"[[Blog post title]]\")\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A basic MediaWiki markup parser.",
    "version": "2.3.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "06cdc56a20509043b194aab20ac0e1f23bbfcfc284f105c779640c6f9b8f6dd4",
                "md5": "9795ef91f325a8ddb077cfbb05caf995",
                "sha256": "90060fbdfda0e2c611d04fe963be4a9e894f12cb28a21d2d515fe504925fc55b"
            },
            "downloads": -1,
            "filename": "py_wikimarkup-2.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9795ef91f325a8ddb077cfbb05caf995",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 34381,
            "upload_time": "2023-01-26T02:44:26",
            "upload_time_iso_8601": "2023-01-26T02:44:26.012619Z",
            "url": "https://files.pythonhosted.org/packages/06/cd/c56a20509043b194aab20ac0e1f23bbfcfc284f105c779640c6f9b8f6dd4/py_wikimarkup-2.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "314a6ea908153b339aafcf4dd25a3a391c0d23efb39f2d21748725f8f2708e44",
                "md5": "4313b4dadda1f3b144d608222cb66c9f",
                "sha256": "7081ab990afb95bf9d55b762a3db6f779838844a8de0bf8d2589443a6e378e1c"
            },
            "downloads": -1,
            "filename": "py-wikimarkup-2.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4313b4dadda1f3b144d608222cb66c9f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 34601,
            "upload_time": "2023-01-26T02:44:27",
            "upload_time_iso_8601": "2023-01-26T02:44:27.564175Z",
            "url": "https://files.pythonhosted.org/packages/31/4a/6ea908153b339aafcf4dd25a3a391c0d23efb39f2d21748725f8f2708e44/py-wikimarkup-2.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-26 02:44:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "dgilman",
    "github_project": "py-wikimarkup",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "bleach",
            "specs": [
                [
                    "==",
                    "6.*"
                ]
            ]
        },
        {
            "name": "unidecode",
            "specs": [
                [
                    "==",
                    "1.*"
                ]
            ]
        }
    ],
    "lcname": "py-wikimarkup"
}
        
Elapsed time: 0.03349s