File: __init__.py

package info (click to toggle)
mistune 3.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 856 kB
  • sloc: python: 4,006; makefile: 26; sh: 6
file content (55 lines) | stat: -rw-r--r-- 1,500 bytes parent folder | download | duplicates (2)
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
import os
import re
import json
from typing import Any, Iterable, Tuple

ROOT = os.path.join(os.path.dirname(__file__))

EXAMPLE_PATTERN = re.compile(
    r"^`{32} example\n([\s\S]*?)"
    r"^\.\n([\s\S]*?)"
    r"^`{32}$|^#{1,6} *(.*)$",
    flags=re.M,
)


def load_ast(filename: str) -> Any:
    with open(os.path.join(ROOT, "ast", filename)) as f:
        return json.load(f)


def load_json(filename: str) -> Any:
    with open(os.path.join(ROOT, filename)) as f:
        return json.load(f)


def load_examples(filename: str) -> Iterable[Tuple[str, str, str]]:
    if filename.endswith(".json"):
        data = load_json(filename)
        for item in data:
            section = item["section"].lower().replace(" ", "_")
            n = "%s_%03d" % (section, item["example"])
            yield n, item["markdown"], item["html"]
    else:
        with open(os.path.join(ROOT, filename), "rb") as f:
            content = f.read()
            s = content.decode("utf-8")
            yield from parse_examples(s)


def parse_examples(text: str) -> Iterable[Tuple[str, str, str]]:
    data = EXAMPLE_PATTERN.findall(text)

    section = None
    count = 0
    for md, html, title in data:
        if title:
            count = 0
            section = title.lower().replace(" ", "_")

        if md and html:
            count += 1
            n = "%s_%03d" % (section, count)
            md = md.replace("\u2192", "\t")
            html = html.replace("\u2192", "\t")
            yield n, md, html