/
leanpub.py
55 lines (38 loc) · 1.25 KB
/
leanpub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Leanpub role for Docutils."""
import typing as t
from docutils.parsers.rst.states import Inliner
from .common import generic_url_role
from .types import RoleFnReturnValue
def leanpub_role(
name: str,
rawtext: str,
text: str,
lineno: int,
inliner: Inliner,
options: t.Optional[t.Dict[str, t.Any]] = None,
content: t.Optional[str] = None,
) -> RoleFnReturnValue:
"""Role for linking to leanpub page.
Returns
-------
:data:`django_docutils.lib.roles.types.RoleFnReturnValue`
Examples
--------
`the-tao-of-tmux <https://leanpub.com/the-tao-of-tmux>`_:
.. code-block:: rst
:leanpub:`the-tao-of-tmux`
`my book <https://leanpub.com/the-tao-of-tmux>`_:
.. code-block:: rst
:leanpub:`my book <the-tao-of-tmux>`
`The Tao of tmux <https://leanpub.com/the-tao-of-tmux/read>`_:
.. code-block:: rst
:leanpub:`The Tao of tmux <the-tao-of-tmux:read>`
"""
if options is None:
options = {}
def url_handler(target: str) -> str:
if ":" in target:
project, path = target.split(":")
return f"https://leanpub.com/{project}/{path}"
return f"https://leanpub.com/{target}"
return generic_url_role(name, text, url_handler)