markdown-customblocks


Namemarkdown-customblocks JSON
Version 1.4.1 PyPI version JSON
download
home_pagehttps://vokimon.github.io/markdown-customblocks
SummaryPython Markdown extension to add custom parametrizable and nestable blocks
upload_time2022-12-08 12:12:58
maintainer
docs_urlNone
authorDavid García Garzón
requires_python>=2.7
license
keywords markdown extension customblocks admonitions container figure map youtube vimeo twitter facebook instagram verkami goteo mastodon wikipedia peertube
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![Logo](http://vokimon.github.io/markdown-customblocks/img/logo-customblocks.svg)

# Customblocks for Markdown

[![CI](https://github.com/vokimon/markdown-customblocks/actions/workflows/main.yml/badge.svg)](https://github.com/vokimon/markdown-customblocks/actions/workflows/main.yml)
[![Coverage](https://img.shields.io/coveralls/vokimon/markdown-customblocks/master.svg?style=flat-square&label=Coverage)](https://coveralls.io/r/vokimon/markdown-customblocks)
[![PyPi](https://img.shields.io/pypi/v/markdown-customblocks.svg?style=flat-square&label=PyPI)](https://pypi.org/project/markdown-customblocks/)
[![license: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)
[![downloads](https://img.shields.io/pypi/dm/markdown-customblocks.svg?style=flat-square&label=PyPI%20Downloads)](https://pypi.org/project/markdown-customblocks/)
<!--
[![image](https://img.shields.io/pypi/pyversions/markdown-customblocks.svg?style=flat-square&label=Python%20Versions)](https://pypi.org/project/markdown-customblocks/)
[![image](https://img.shields.io/pypi/implementation/markdown-customblocks.svg?style=flat-square&label=Python%20Implementations)](https://pypi.org/project/markdown-customblocks/)
-->

Customblocks is an extension for [Python-Markdown]
that settles a **common markup** for **parametrizable and nestable components**
whose output can be **redefined** by means of a simple Python function.

Many off-the-shelf components are provided such as
div-containers, admonitions, figures, link cards, maps...
and some embeded widgets from common sites (wikipedia, youtube, vimeo, peertube, mastodon, twitter, facebook, instagram, goteo, verkami...)

It also includes convenience tools to ease component definition:
hyperscript html generation, cached page fetching and metadata page extraction.

[Python-Markdown]: https://python-markdown.github.io/


## Installation and setup

To install:

```bash
$ pip install markdown-customblocks
```

And then activate it as any other Markdown extension.

From command line:

```bash
$ markdown -x customblocks ...
```

In Python code:

```python
import markdown
md = markdown.Markdown(extensions=["customblocks"])
md.convert(markdowncontent)
```

In [Pelican](https://blog.getpelican.com/) config:

```python
MARKDOWN = {
    'extensions': [
        'customblocks',
    ],
}
```

For [MkDocs](https://www.mkdocs.org), add this to `mkdocs.yml`:

```yaml
markdown_extensions:
  - customblocks
```

If you need to specify additional parameters for the extension,
refer to the documentation of your generator.


## Basic usage

Customblocks extension parses markup structures like this one:

```markdown
::: mytype "value 1" param2=value2
    Indented **content**
```

And, by default, generates HTML like this:

```html
<div class="mytype value-1" param2="value2">
   <p>Indented <b>content</b></p>
</div>
```

This is the output of the default generator: `container`.
But we could bound `mytype` to a custom Python function like the following one:

```python
def mygenerator(ctx, param1, param2):
    return f"""<div attrib1="{param1}" attrib2="{param2}">{ctx.content}</div>"""
```

And then, previous Markdown would generate this other HTML:

```html
<div attrib1="value 1" attrib2="value2">Indented **content**</div>
```

Notice that this function is not escaping any received value
and is not converting anything in the content to Markdown.
Luckily, `customblocks` provides some useful tools for that:
the hyperscript generator and the Markdown subparser:

```python
from customblocks.utils import E, Markdown

def mygenerator(ctx, param1, param2):
    return E('', attrib1=param1, attrib2=param2,
        Markdown(ctx.content, ctx.parser)
    )
```

You can read more about them at the [related documentation](https://vokimon.github.io/markdown-customblocks/defining-generators/#hyperscript).

## Built-in generators

For convenience, `customblocks` also provides the following predefined generators:

- [`container`](https://vokimon.github.io/markdown-customblocks/generators-container/):
    A div element with arbitrary classes, attributes and content. This is the default when no type matches.
- [`admonition`](https://vokimon.github.io/markdown-customblocks/generators-admonition/):
    Admonitions, boxes for notes, warnings... (quite similar to the [standard extra extension][ExtraAdmonitions]).
    It is bound to types `note`,  `info`, `error`, `warning`....
- [`figure`](https://vokimon.github.io/markdown-customblocks/generators-figure/):
    Full featured figures with captions, lightbox...
- [`map`](https://vokimon.github.io/markdown-customblocks/generators-map/):
    Maps from [OpenStreetMaps.org](https://openstreetmap.org)
- [`linkcard`](https://vokimon.github.io/markdown-customblocks/generators-linkcard/):
    External link cards (like Facebook and Twitter do, when you post a link)
- [`wikipedia`](https://vokimon.github.io/markdown-customblocks/generators-wikipedia/):
    Wikipedia article card by lemma (and language)
- [`youtube`](https://vokimon.github.io/markdown-customblocks/generators-youtube/):
    Embeded videos from youtube.
- [`vimeo`](https://vokimon.github.io/markdown-customblocks/generators-vimeo/):
    Embeded videos from vimeo.
- [`peertube`](https://vokimon.github.io/markdown-customblocks/generators-peertube/):
    Embeded videos from [peertube](https://joinpeertube.org)
- [`mastodon`](https://vokimon.github.io/markdown-customblocks/generators-mastodon/):
    Embeded [mastodon](https://joinmastodon.org) posts
- [`twitter`](https://vokimon.github.io/markdown-customblocks/generators-twitter/):
    Embeded tweets
- [`facebook`](https://vokimon.github.io/markdown-customblocks/generators-facebook/):
    Embeded post from facebook
- [`instagram`](https://vokimon.github.io/markdown-customblocks/generators-instagram/):
    Embeded post from instagram
- [`verkami`](https://vokimon.github.io/markdown-customblocks/generators-verkami/):
    Fund raising project widget in [Verkami]
- [`goteo`](https://vokimon.github.io/markdown-customblocks/generators-goteo/):
    Fund raising project widget in [Goteo]

[ExtraAdmonitions]: https://python-markdown.github.io/extensions/admonition/


## General markup syntax

This is a more complete example of markup:

```markdown
::: mytype param1 key1=value1 "other param" key2='value2 with words' flag1 noflag2
    Indented **content**

    The block ends whenever the indentation stops
This unindented line is not considered part of the block
```

**The headline:**
The line starting with `:::` is the _headline_.
It specifies, first, the block type (`mytype`) followed by a set of _values_
that will be passed to the generator as parameters.


**Block type:**
The type is used to select the generator function.
If there is no generator bound to the type, the div-container generator, will be used by default.

**Quotes:**
Muti-word values can be passed by using either single or double quotes.
You can skip quotes if your value is single worded.

**Explicit keywords:**
Also some values may target an explicit parameter with a _key_.
This works as follows:
from the available block parameters, values with a key are set first,
then the remaining unset parameters are filled by position.

**Flags (bools):**
Boolean parameters (flags) can be set by just adding a value with the name of the flag, like `flag1` in the example.
And they can be unset by adding the name with a `no` prefix, like `noflag2` in the example.

**Content:**
After the _headline_, several lines of indented _content_ may follow.
The content ends with the very first non-emtpy line back on the previous indentation.

Indentation is removed from the content for the generator to process it.
A block type may choose to interpret this content as markdown as well.
So you can have nested blocks by adding extra indentation.
For example:

```markdown
::: recipe
    # Sweet water
    ::: ingredients "4 persons"
        - two spons of suggar
        - a glass of tap water
    ::: mealphoto sweetwater.jpg
        Looks gorgeus!
    Drop the suggar into the glass. Stir.
```

::: note
    A closing `:::` tag is optional.
    For most cases, indentation should be enough, visually, and functionally.
    But, seldomly, it is necessary.
    Like in the example below, where
    the mealphoto content would be mixed with 
    the later code block

        ::: mealphoto sweetwater.jpg
                Looks gorgeus!
        :::
                This is a code block by indentation


## Further reading

- [Full documentation](https://vokimon.github.io/markdown-customblocks)
- [Code](https://github.com/vokimon/markdown-customblocks)
- [Release History](https://vokimon.github.io/markdown-customblocks/CHANGES)
- [Road map](https://vokimon.github.io/markdown-customblocks/TODO)
- [Motivation and design choices](https://vokimon.github.io/markdown-customblocks/motivation)
- [Inspiration and kudos](https://vokimon.github.io/markdown-customblocks/inspiration)



            

Raw data

            {
    "_id": null,
    "home_page": "https://vokimon.github.io/markdown-customblocks",
    "name": "markdown-customblocks",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=2.7",
    "maintainer_email": "",
    "keywords": "markdown extension customblocks admonitions container figure map youtube vimeo twitter facebook instagram verkami goteo mastodon wikipedia peertube",
    "author": "David Garc\u00eda Garz\u00f3n",
    "author_email": "voki@canvoki.net",
    "download_url": "https://files.pythonhosted.org/packages/08/26/949556326fd44052c0fea891500b155e0c737bfa6d3b9a01fb6941800a6f/markdown-customblocks-1.4.1.tar.gz",
    "platform": null,
    "description": "![Logo](http://vokimon.github.io/markdown-customblocks/img/logo-customblocks.svg)\n\n# Customblocks for Markdown\n\n[![CI](https://github.com/vokimon/markdown-customblocks/actions/workflows/main.yml/badge.svg)](https://github.com/vokimon/markdown-customblocks/actions/workflows/main.yml)\n[![Coverage](https://img.shields.io/coveralls/vokimon/markdown-customblocks/master.svg?style=flat-square&label=Coverage)](https://coveralls.io/r/vokimon/markdown-customblocks)\n[![PyPi](https://img.shields.io/pypi/v/markdown-customblocks.svg?style=flat-square&label=PyPI)](https://pypi.org/project/markdown-customblocks/)\n[![license: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)\n[![downloads](https://img.shields.io/pypi/dm/markdown-customblocks.svg?style=flat-square&label=PyPI%20Downloads)](https://pypi.org/project/markdown-customblocks/)\n<!--\n[![image](https://img.shields.io/pypi/pyversions/markdown-customblocks.svg?style=flat-square&label=Python%20Versions)](https://pypi.org/project/markdown-customblocks/)\n[![image](https://img.shields.io/pypi/implementation/markdown-customblocks.svg?style=flat-square&label=Python%20Implementations)](https://pypi.org/project/markdown-customblocks/)\n-->\n\nCustomblocks is an extension for [Python-Markdown]\nthat settles a **common markup** for **parametrizable and nestable components**\nwhose output can be **redefined** by means of a simple Python function.\n\nMany off-the-shelf components are provided such as\ndiv-containers, admonitions, figures, link cards, maps...\nand some embeded widgets from common sites (wikipedia, youtube, vimeo, peertube, mastodon, twitter, facebook, instagram, goteo, verkami...)\n\nIt also includes convenience tools to ease component definition:\nhyperscript html generation, cached page fetching and metadata page extraction.\n\n[Python-Markdown]: https://python-markdown.github.io/\n\n\n## Installation and setup\n\nTo install:\n\n```bash\n$ pip install markdown-customblocks\n```\n\nAnd then activate it as any other Markdown extension.\n\nFrom command line:\n\n```bash\n$ markdown -x customblocks ...\n```\n\nIn Python code:\n\n```python\nimport markdown\nmd = markdown.Markdown(extensions=[\"customblocks\"])\nmd.convert(markdowncontent)\n```\n\nIn [Pelican](https://blog.getpelican.com/) config:\n\n```python\nMARKDOWN = {\n    'extensions': [\n        'customblocks',\n    ],\n}\n```\n\nFor [MkDocs](https://www.mkdocs.org), add this to `mkdocs.yml`:\n\n```yaml\nmarkdown_extensions:\n  - customblocks\n```\n\nIf you need to specify additional parameters for the extension,\nrefer to the documentation of your generator.\n\n\n## Basic usage\n\nCustomblocks extension parses markup structures like this one:\n\n```markdown\n::: mytype \"value 1\" param2=value2\n    Indented **content**\n```\n\nAnd, by default, generates HTML like this:\n\n```html\n<div class=\"mytype value-1\" param2=\"value2\">\n   <p>Indented <b>content</b></p>\n</div>\n```\n\nThis is the output of the default generator: `container`.\nBut we could bound `mytype` to a custom Python function like the following one:\n\n```python\ndef mygenerator(ctx, param1, param2):\n    return f\"\"\"<div attrib1=\"{param1}\" attrib2=\"{param2}\">{ctx.content}</div>\"\"\"\n```\n\nAnd then, previous Markdown would generate this other HTML:\n\n```html\n<div attrib1=\"value 1\" attrib2=\"value2\">Indented **content**</div>\n```\n\nNotice that this function is not escaping any received value\nand is not converting anything in the content to Markdown.\nLuckily, `customblocks` provides some useful tools for that:\nthe hyperscript generator and the Markdown subparser:\n\n```python\nfrom customblocks.utils import E, Markdown\n\ndef mygenerator(ctx, param1, param2):\n    return E('', attrib1=param1, attrib2=param2,\n        Markdown(ctx.content, ctx.parser)\n    )\n```\n\nYou can read more about them at the [related documentation](https://vokimon.github.io/markdown-customblocks/defining-generators/#hyperscript).\n\n## Built-in generators\n\nFor convenience, `customblocks` also provides the following predefined generators:\n\n- [`container`](https://vokimon.github.io/markdown-customblocks/generators-container/):\n    A div element with arbitrary classes, attributes and content. This is the default when no type matches.\n- [`admonition`](https://vokimon.github.io/markdown-customblocks/generators-admonition/):\n    Admonitions, boxes for notes, warnings... (quite similar to the [standard extra extension][ExtraAdmonitions]).\n    It is bound to types `note`,  `info`, `error`, `warning`....\n- [`figure`](https://vokimon.github.io/markdown-customblocks/generators-figure/):\n    Full featured figures with captions, lightbox...\n- [`map`](https://vokimon.github.io/markdown-customblocks/generators-map/):\n    Maps from [OpenStreetMaps.org](https://openstreetmap.org)\n- [`linkcard`](https://vokimon.github.io/markdown-customblocks/generators-linkcard/):\n    External link cards (like Facebook and Twitter do, when you post a link)\n- [`wikipedia`](https://vokimon.github.io/markdown-customblocks/generators-wikipedia/):\n    Wikipedia article card by lemma (and language)\n- [`youtube`](https://vokimon.github.io/markdown-customblocks/generators-youtube/):\n    Embeded videos from youtube.\n- [`vimeo`](https://vokimon.github.io/markdown-customblocks/generators-vimeo/):\n    Embeded videos from vimeo.\n- [`peertube`](https://vokimon.github.io/markdown-customblocks/generators-peertube/):\n    Embeded videos from [peertube](https://joinpeertube.org)\n- [`mastodon`](https://vokimon.github.io/markdown-customblocks/generators-mastodon/):\n    Embeded [mastodon](https://joinmastodon.org) posts\n- [`twitter`](https://vokimon.github.io/markdown-customblocks/generators-twitter/):\n    Embeded tweets\n- [`facebook`](https://vokimon.github.io/markdown-customblocks/generators-facebook/):\n    Embeded post from facebook\n- [`instagram`](https://vokimon.github.io/markdown-customblocks/generators-instagram/):\n    Embeded post from instagram\n- [`verkami`](https://vokimon.github.io/markdown-customblocks/generators-verkami/):\n    Fund raising project widget in [Verkami]\n- [`goteo`](https://vokimon.github.io/markdown-customblocks/generators-goteo/):\n    Fund raising project widget in [Goteo]\n\n[ExtraAdmonitions]: https://python-markdown.github.io/extensions/admonition/\n\n\n## General markup syntax\n\nThis is a more complete example of markup:\n\n```markdown\n::: mytype param1 key1=value1 \"other param\" key2='value2 with words' flag1 noflag2\n    Indented **content**\n\n    The block ends whenever the indentation stops\nThis unindented line is not considered part of the block\n```\n\n**The headline:**\nThe line starting with `:::` is the _headline_.\nIt specifies, first, the block type (`mytype`) followed by a set of _values_\nthat will be passed to the generator as parameters.\n\n\n**Block type:**\nThe type is used to select the generator function.\nIf there is no generator bound to the type, the div-container generator, will be used by default.\n\n**Quotes:**\nMuti-word values can be passed by using either single or double quotes.\nYou can skip quotes if your value is single worded.\n\n**Explicit keywords:**\nAlso some values may target an explicit parameter with a _key_.\nThis works as follows:\nfrom the available block parameters, values with a key are set first,\nthen the remaining unset parameters are filled by position.\n\n**Flags (bools):**\nBoolean parameters (flags) can be set by just adding a value with the name of the flag, like `flag1` in the example.\nAnd they can be unset by adding the name with a `no` prefix, like `noflag2` in the example.\n\n**Content:**\nAfter the _headline_, several lines of indented _content_ may follow.\nThe content ends with the very first non-emtpy line back on the previous indentation.\n\nIndentation is removed from the content for the generator to process it.\nA block type may choose to interpret this content as markdown as well.\nSo you can have nested blocks by adding extra indentation.\nFor example:\n\n```markdown\n::: recipe\n    # Sweet water\n    ::: ingredients \"4 persons\"\n        - two spons of suggar\n        - a glass of tap water\n    ::: mealphoto sweetwater.jpg\n        Looks gorgeus!\n    Drop the suggar into the glass. Stir.\n```\n\n::: note\n    A closing `:::` tag is optional.\n    For most cases, indentation should be enough, visually, and functionally.\n    But, seldomly, it is necessary.\n    Like in the example below, where\n    the mealphoto content would be mixed with \n    the later code block\n\n        ::: mealphoto sweetwater.jpg\n                Looks gorgeus!\n        :::\n                This is a code block by indentation\n\n\n## Further reading\n\n- [Full documentation](https://vokimon.github.io/markdown-customblocks)\n- [Code](https://github.com/vokimon/markdown-customblocks)\n- [Release History](https://vokimon.github.io/markdown-customblocks/CHANGES)\n- [Road map](https://vokimon.github.io/markdown-customblocks/TODO)\n- [Motivation and design choices](https://vokimon.github.io/markdown-customblocks/motivation)\n- [Inspiration and kudos](https://vokimon.github.io/markdown-customblocks/inspiration)\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Python Markdown extension to add custom parametrizable and nestable blocks",
    "version": "1.4.1",
    "split_keywords": [
        "markdown",
        "extension",
        "customblocks",
        "admonitions",
        "container",
        "figure",
        "map",
        "youtube",
        "vimeo",
        "twitter",
        "facebook",
        "instagram",
        "verkami",
        "goteo",
        "mastodon",
        "wikipedia",
        "peertube"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "396cf5028e5e09b4ee592284ba23eaed",
                "sha256": "4a526a19513f60f66014bf5fb44d9964cae7cfd333b97a504bd5734c97e0176b"
            },
            "downloads": -1,
            "filename": "markdown_customblocks-1.4.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "396cf5028e5e09b4ee592284ba23eaed",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=2.7",
            "size": 39798,
            "upload_time": "2022-12-08T12:12:56",
            "upload_time_iso_8601": "2022-12-08T12:12:56.901070Z",
            "url": "https://files.pythonhosted.org/packages/89/10/a98049829597d651fec0b8beaa840aaadcf97648764306ffbb3053557de0/markdown_customblocks-1.4.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "75631ab727568c923ec08de381060478",
                "sha256": "0d22c3829aacbf938afb0bad25062299dc5f51928991a877e658760e6f6d048a"
            },
            "downloads": -1,
            "filename": "markdown-customblocks-1.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "75631ab727568c923ec08de381060478",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=2.7",
            "size": 39847,
            "upload_time": "2022-12-08T12:12:58",
            "upload_time_iso_8601": "2022-12-08T12:12:58.383499Z",
            "url": "https://files.pythonhosted.org/packages/08/26/949556326fd44052c0fea891500b155e0c737bfa6d3b9a01fb6941800a6f/markdown-customblocks-1.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-08 12:12:58",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "markdown-customblocks"
}
        
Elapsed time: 0.01828s