|
4 | 4 |
|
5 | 5 | import sys
|
6 | 6 | import mistune
|
7 |
| -import mistune.renderers |
| 7 | +from mistune import BlockState |
| 8 | + |
| 9 | +from typing import Dict, Any |
8 | 10 |
|
9 | 11 | print(sys.argv[1])
|
10 | 12 |
|
|
20 | 22 | print(html(source))
|
21 | 23 |
|
22 | 24 |
|
23 |
| -class AdafruitBBCodeRenderer(mistune.renderers.BaseRenderer): |
24 |
| - def placeholder(self): |
| 25 | +class AdafruitBBCodeRenderer(mistune.BaseRenderer): |
| 26 | + def render_token(self, token: Dict[str, Any], state: BlockState) -> str: |
| 27 | + # backward compatible with v2 |
| 28 | + func = self._get_method(token["type"]) |
| 29 | + attrs = token.get("attrs") |
| 30 | + |
| 31 | + if "raw" in token: |
| 32 | + text = token["raw"] |
| 33 | + elif "children" in token: |
| 34 | + text = self.render_tokens(token["children"], state) |
| 35 | + else: |
| 36 | + if attrs: |
| 37 | + return func(**attrs) |
| 38 | + else: |
| 39 | + return func() |
| 40 | + if attrs: |
| 41 | + return func(text, **attrs) |
| 42 | + else: |
| 43 | + return func(text) |
| 44 | + |
| 45 | + def blank_line(self): |
25 | 46 | return ""
|
26 | 47 |
|
27 | 48 | def paragraph(self, text):
|
28 |
| - return text + "\n\n" |
| 49 | + return f"{text}\n\n" |
29 | 50 |
|
30 | 51 | def block_text(self, text):
|
31 | 52 | return text
|
32 | 53 |
|
33 | 54 | def text(self, text):
|
34 | 55 | return text
|
35 | 56 |
|
36 |
| - def link(self, link, title, text): |
37 |
| - return "[url={}]{}[/url]".format(link, title) |
| 57 | + def block_code(self, text): |
| 58 | + return f"[code]{text}[/code]" |
| 59 | + |
| 60 | + def link(self, text, url): |
| 61 | + return f"[url={url}]{text}[/url]" |
38 | 62 |
|
39 | 63 | def autolink(self, link, is_email):
|
40 | 64 | if not is_email:
|
41 |
| - return "[url={}]{}[/url]".format(link, link) |
| 65 | + return f"[url={link}]{link}[/url]" |
42 | 66 | return link
|
43 | 67 |
|
44 | 68 | def heading(self, text, level):
|
45 |
| - return "[b][size=150]{}[/size][/b]\n".format(text) |
| 69 | + return f"[b][size={200 - level * 20}]{text}[/size][/b]\n" |
46 | 70 |
|
47 | 71 | def codespan(self, text):
|
48 |
| - return "[color=#E74C3C][size=95]{}[/size][/color]".format(text) |
| 72 | + return f"[color=#E74C3C][size=95]{text}[/size][/color]" |
49 | 73 |
|
50 |
| - def list_item(self, text, level): |
51 |
| - return "[*]{}[/*]\n".format(text.strip()) |
52 |
| - |
53 |
| - def list(self, text, ordered, level, start=None): |
| 74 | + def list(self, text, ordered: bool, start=None, depth=None): |
54 | 75 | ordered_indicator = "=" if ordered else ""
|
55 |
| - return "[list{}]\n{}[/list]".format(ordered_indicator, text) |
| 76 | + return f"[list{ordered_indicator}]\n{text}[/list]" |
| 77 | + |
| 78 | + def list_item(self, text): |
| 79 | + return f"[*]{text.strip()}[/*]\n" |
56 | 80 |
|
57 | 81 | def double_emphasis(self, text):
|
58 |
| - return "[b]{}[/b]".format(text) |
| 82 | + return f"[b]{text}[/b]" |
59 | 83 |
|
60 | 84 | def emphasis(self, text):
|
61 |
| - return "[i]{}[/i]".format(text) |
| 85 | + return f"[i]{text}[/i]" |
62 | 86 |
|
63 | 87 | def strong(self, text):
|
64 |
| - return "[b]{}[/b]".format(text) |
| 88 | + return f"[b]{text}[/b]" |
65 | 89 |
|
66 | 90 | def finalize(self, data):
|
67 | 91 | return "".join(data)
|
|
0 commit comments