multimark 0.3.2__cp39-abi3-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- multimark/__init__.py +99 -0
- multimark/_binding.pyd +0 -0
- multimark/_build_cmark.py +47 -0
- multimark/_cli.py +93 -0
- multimark/_cmark.py +746 -0
- multimark/_node.py +623 -0
- multimark/_parser.py +280 -0
- multimark/cmark.cffi.h +152 -0
- multimark-0.3.2.dist-info/METADATA +169 -0
- multimark-0.3.2.dist-info/RECORD +14 -0
- multimark-0.3.2.dist-info/WHEEL +5 -0
- multimark-0.3.2.dist-info/entry_points.txt +2 -0
- multimark-0.3.2.dist-info/licenses/LICENSE +128 -0
- multimark-0.3.2.dist-info/top_level.txt +1 -0
multimark/__init__.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
from enum import IntFlag
|
|
2
|
+
from importlib.metadata import PackageNotFoundError, version as _get_version
|
|
3
|
+
|
|
4
|
+
try:
|
|
5
|
+
__version__ = _get_version("multimark")
|
|
6
|
+
except PackageNotFoundError:
|
|
7
|
+
__version__ = "0.0.0"
|
|
8
|
+
|
|
9
|
+
from multimark._cmark import (
|
|
10
|
+
markdown_to_html,
|
|
11
|
+
markdown_to_latex,
|
|
12
|
+
markdown_to_man,
|
|
13
|
+
markdown_to_commonmark,
|
|
14
|
+
markdown_to_xml,
|
|
15
|
+
cmark_version,
|
|
16
|
+
VALID_EXTENSIONS,
|
|
17
|
+
)
|
|
18
|
+
from multimark._parser import Parser
|
|
19
|
+
from multimark._node import Node, NodeType, ListType, DelimType, parse
|
|
20
|
+
from multimark._binding import lib
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class Options(IntFlag):
|
|
24
|
+
"""CommonMark/GFM parsing and rendering options.
|
|
25
|
+
|
|
26
|
+
An `IntFlag` enumeration of all option flags supported by the cmark-gfm library.
|
|
27
|
+
Options can be combined using the bitwise OR operator (`|`) and passed to any
|
|
28
|
+
renderer's `options=` parameter. This provides a composable, lower-level alternative
|
|
29
|
+
to the named boolean keyword arguments available on each renderer function.
|
|
30
|
+
|
|
31
|
+
Most users will prefer the named keyword arguments (e.g., `smart=True`) for common
|
|
32
|
+
options. The `Options` bitmask is useful when you need to combine multiple flags
|
|
33
|
+
into a single reusable value, or when working with GFM-specific flags that do not
|
|
34
|
+
have dedicated keyword arguments.
|
|
35
|
+
|
|
36
|
+
Examples
|
|
37
|
+
--------
|
|
38
|
+
Combine multiple flags with the `|` operator:
|
|
39
|
+
|
|
40
|
+
```{python}
|
|
41
|
+
from multimark import markdown_to_html, Options
|
|
42
|
+
|
|
43
|
+
opts = Options.SMART | Options.UNSAFE
|
|
44
|
+
markdown_to_html('"Hello" -- world', options=opts)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Mix a bitmask with named keyword arguments (they are OR'd together):
|
|
48
|
+
|
|
49
|
+
```{python}
|
|
50
|
+
text = "Here's some *text* to convert"
|
|
51
|
+
markdown_to_html(text, options=Options.SMART, footnotes=True)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Use GFM-specific flags that have no keyword shortcut:
|
|
55
|
+
|
|
56
|
+
```{python}
|
|
57
|
+
markdown_to_html(
|
|
58
|
+
"~~deleted~~",
|
|
59
|
+
options=Options.STRIKETHROUGH_DOUBLE_TILDE,
|
|
60
|
+
extensions=["strikethrough"],
|
|
61
|
+
)
|
|
62
|
+
```
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
DEFAULT = lib.CMARK_OPT_DEFAULT
|
|
66
|
+
SOURCEPOS = lib.CMARK_OPT_SOURCEPOS
|
|
67
|
+
HARDBREAKS = lib.CMARK_OPT_HARDBREAKS
|
|
68
|
+
SAFE = lib.CMARK_OPT_SAFE
|
|
69
|
+
UNSAFE = lib.CMARK_OPT_UNSAFE
|
|
70
|
+
NOBREAKS = lib.CMARK_OPT_NOBREAKS
|
|
71
|
+
NORMALIZE = lib.CMARK_OPT_NORMALIZE
|
|
72
|
+
VALIDATE_UTF8 = lib.CMARK_OPT_VALIDATE_UTF8
|
|
73
|
+
SMART = lib.CMARK_OPT_SMART
|
|
74
|
+
# GFM-specific options
|
|
75
|
+
GITHUB_PRE_LANG = lib.CMARK_OPT_GITHUB_PRE_LANG
|
|
76
|
+
LIBERAL_HTML_TAG = lib.CMARK_OPT_LIBERAL_HTML_TAG
|
|
77
|
+
FOOTNOTES = lib.CMARK_OPT_FOOTNOTES
|
|
78
|
+
STRIKETHROUGH_DOUBLE_TILDE = lib.CMARK_OPT_STRIKETHROUGH_DOUBLE_TILDE
|
|
79
|
+
TABLE_PREFER_STYLE_ATTRIBUTES = lib.CMARK_OPT_TABLE_PREFER_STYLE_ATTRIBUTES
|
|
80
|
+
FULL_INFO_STRING = lib.CMARK_OPT_FULL_INFO_STRING
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
__all__ = [
|
|
84
|
+
"markdown_to_html",
|
|
85
|
+
"markdown_to_latex",
|
|
86
|
+
"markdown_to_man",
|
|
87
|
+
"markdown_to_commonmark",
|
|
88
|
+
"markdown_to_xml",
|
|
89
|
+
"cmark_version",
|
|
90
|
+
"Options",
|
|
91
|
+
"Parser",
|
|
92
|
+
"parse",
|
|
93
|
+
"Node",
|
|
94
|
+
"NodeType",
|
|
95
|
+
"ListType",
|
|
96
|
+
"DelimType",
|
|
97
|
+
"VALID_EXTENSIONS",
|
|
98
|
+
"__version__",
|
|
99
|
+
]
|
multimark/_binding.pyd
ADDED
|
Binary file
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import glob
|
|
3
|
+
from cffi import FFI
|
|
4
|
+
|
|
5
|
+
ffi = FFI()
|
|
6
|
+
|
|
7
|
+
here = os.path.dirname(os.path.abspath(__file__))
|
|
8
|
+
|
|
9
|
+
# Read the CFFI declarations
|
|
10
|
+
with open(os.path.join(here, "cmark.cffi.h")) as f:
|
|
11
|
+
ffi.cdef(f.read())
|
|
12
|
+
|
|
13
|
+
# Locate vendored cmark-gfm C sources (use relative paths from project root for setuptools)
|
|
14
|
+
project_root = os.path.normpath(os.path.join(here, "..", ".."))
|
|
15
|
+
src_dir_abs = os.path.join(project_root, "third_party", "cmark-gfm", "src")
|
|
16
|
+
ext_dir_abs = os.path.join(project_root, "third_party", "cmark-gfm", "extensions")
|
|
17
|
+
src_dir_rel = os.path.join("third_party", "cmark-gfm", "src")
|
|
18
|
+
ext_dir_rel = os.path.join("third_party", "cmark-gfm", "extensions")
|
|
19
|
+
|
|
20
|
+
# Core library sources (exclude main.c)
|
|
21
|
+
src_sources_abs = sorted(glob.glob(os.path.join(src_dir_abs, "*.c")))
|
|
22
|
+
src_sources = [
|
|
23
|
+
os.path.join(src_dir_rel, os.path.basename(s))
|
|
24
|
+
for s in src_sources_abs
|
|
25
|
+
if not s.endswith("main.c")
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
# Extension sources
|
|
29
|
+
ext_sources_abs = sorted(glob.glob(os.path.join(ext_dir_abs, "*.c")))
|
|
30
|
+
ext_sources = [
|
|
31
|
+
os.path.join(ext_dir_rel, os.path.basename(s))
|
|
32
|
+
for s in ext_sources_abs
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
ffi.set_source(
|
|
36
|
+
"multimark._binding",
|
|
37
|
+
"""
|
|
38
|
+
#include "cmark-gfm.h"
|
|
39
|
+
#include "cmark-gfm-core-extensions.h"
|
|
40
|
+
""",
|
|
41
|
+
sources=src_sources + ext_sources,
|
|
42
|
+
include_dirs=[src_dir_abs, ext_dir_abs],
|
|
43
|
+
define_macros=[("CMARK_GFM_STATIC_DEFINE", None)],
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
if __name__ == "__main__":
|
|
47
|
+
ffi.compile(verbose=True)
|
multimark/_cli.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
|
|
5
|
+
from multimark import (
|
|
6
|
+
__version__,
|
|
7
|
+
markdown_to_html,
|
|
8
|
+
markdown_to_latex,
|
|
9
|
+
markdown_to_man,
|
|
10
|
+
markdown_to_commonmark,
|
|
11
|
+
markdown_to_xml,
|
|
12
|
+
VALID_EXTENSIONS,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
RENDERERS = {
|
|
16
|
+
"html": markdown_to_html,
|
|
17
|
+
"latex": markdown_to_latex,
|
|
18
|
+
"man": markdown_to_man,
|
|
19
|
+
"commonmark": markdown_to_commonmark,
|
|
20
|
+
"xml": markdown_to_xml,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@click.command()
|
|
25
|
+
@click.argument("file", type=click.File("r"), default="-")
|
|
26
|
+
@click.option(
|
|
27
|
+
"-t",
|
|
28
|
+
"--to",
|
|
29
|
+
"format",
|
|
30
|
+
type=click.Choice(list(RENDERERS.keys()), case_sensitive=False),
|
|
31
|
+
default="html",
|
|
32
|
+
help="Output format.",
|
|
33
|
+
)
|
|
34
|
+
@click.option(
|
|
35
|
+
"-o",
|
|
36
|
+
"--output",
|
|
37
|
+
type=click.File("w"),
|
|
38
|
+
default="-",
|
|
39
|
+
help="Output file (stdout if omitted).",
|
|
40
|
+
)
|
|
41
|
+
@click.option(
|
|
42
|
+
"-e",
|
|
43
|
+
"--extension",
|
|
44
|
+
"extensions",
|
|
45
|
+
multiple=True,
|
|
46
|
+
type=click.Choice(sorted(VALID_EXTENSIONS), case_sensitive=False),
|
|
47
|
+
help="Enable a GFM extension (repeatable).",
|
|
48
|
+
)
|
|
49
|
+
@click.option("--smart", is_flag=True, help="Use smart punctuation.")
|
|
50
|
+
@click.option("--unsafe", is_flag=True, help="Allow raw HTML and dangerous URLs.")
|
|
51
|
+
@click.option("--hardbreaks", is_flag=True, help="Render softbreaks as hard line breaks.")
|
|
52
|
+
@click.option("--sourcepos", is_flag=True, help="Include source position attributes (html/xml only).")
|
|
53
|
+
@click.option("--footnotes", is_flag=True, help="Enable footnote parsing.")
|
|
54
|
+
@click.option("--width", type=int, default=0, help="Wrap output at this column width (latex/man/commonmark only).")
|
|
55
|
+
@click.version_option(__version__, prog_name="multimark")
|
|
56
|
+
def main(
|
|
57
|
+
file,
|
|
58
|
+
format: str,
|
|
59
|
+
output,
|
|
60
|
+
extensions: tuple[str, ...],
|
|
61
|
+
smart: bool,
|
|
62
|
+
unsafe: bool,
|
|
63
|
+
hardbreaks: bool,
|
|
64
|
+
sourcepos: bool,
|
|
65
|
+
footnotes: bool,
|
|
66
|
+
width: int,
|
|
67
|
+
) -> None:
|
|
68
|
+
"""Convert CommonMark/GFM Markdown to various output formats.
|
|
69
|
+
|
|
70
|
+
Reads Markdown from FILE (or stdin if omitted) and writes the converted
|
|
71
|
+
output to stdout or the file specified by --output.
|
|
72
|
+
"""
|
|
73
|
+
text = file.read()
|
|
74
|
+
|
|
75
|
+
renderer = RENDERERS[format]
|
|
76
|
+
kwargs: dict = dict(
|
|
77
|
+
extensions=list(extensions) or None,
|
|
78
|
+
smart=smart,
|
|
79
|
+
unsafe=unsafe,
|
|
80
|
+
hardbreaks=hardbreaks,
|
|
81
|
+
footnotes=footnotes,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
# sourcepos is only supported by html and xml renderers
|
|
85
|
+
if format in ("html", "xml"):
|
|
86
|
+
kwargs["sourcepos"] = sourcepos
|
|
87
|
+
|
|
88
|
+
# width is only supported by latex, man, and commonmark renderers
|
|
89
|
+
if format in ("latex", "man", "commonmark"):
|
|
90
|
+
kwargs["width"] = width
|
|
91
|
+
|
|
92
|
+
result = renderer(text, **kwargs)
|
|
93
|
+
output.write(result)
|