• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rafalp / Misago / 15379643178

01 Jun 2025 09:31PM UTC coverage: 97.18% (+0.002%) from 97.178%
15379643178

push

github

web-flow
Update formatting bbcode to respect escaping (#1935)

268 of 288 new or added lines in 13 files covered. (93.06%)

9 existing lines in 3 files now uncovered.

71712 of 73793 relevant lines covered (97.18%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.31
/misago/parser/plugins/codebbcode.py
1
from textwrap import dedent
1✔
2
from typing import Sequence
1✔
3

4
from markdown_it import MarkdownIt
1✔
5
from markdown_it.common.utils import escapeHtml
1✔
6
from markdown_it.renderer import RendererHTML
1✔
7
from markdown_it.rules_block.state_block import StateBlock
1✔
8
from markdown_it.token import Token
1✔
9
from markdown_it.utils import EnvType, OptionsDict
1✔
10

11
from ..bbcode import BBCodeBlockRule
1✔
12
from ..codeargs import parse_code_args
1✔
13

14

15
def code_bbcode_plugin(md: MarkdownIt):
1✔
16
    md.block.ruler.before(
1✔
17
        "paragraph",
18
        "code_bbcode",
19
        CodeBBCodeBlockRule(
20
            name="code_bbcode",
21
            bbcode="code",
22
            element="code",
23
            args_parser=parse_code_args,
24
        ),
25
        {"alt": ["paragraph"]},
26
    )
27

28
    md.add_render_rule("code_bbcode", code_bbcode_renderer)
1✔
29

30

31
class CodeBBCodeBlockRule(BBCodeBlockRule):
1✔
32
    def parse_single_line(
1✔
33
        self,
34
        state: StateBlock,
35
        line: int,
36
        silent: bool,
37
    ):
38
        start = self.find_single_line_bbcode_block_start(state, line)
1✔
39
        if not start:
1✔
40
            return False
1✔
41

42
        end = self.find_single_line_bbcode_block_end(state, line, start.end)
1✔
43
        if not end:
1✔
44
            return False
1✔
45

46
        if silent:
1✔
47
            return True
1✔
48

49
        token = self.state_push_void_token(state, line, start.markup, start.attrs)
1✔
50
        token.content = state.src[start.end : end.start].strip()
1✔
51

52
        state.line += 1
1✔
53
        return True
1✔
54

55
    def parse_multiple_lines(
1✔
56
        self,
57
        state: StateBlock,
58
        startLine: int,
59
        endLine: int,
60
        silent: bool,
61
    ) -> bool:
62
        line = startLine
1✔
63

64
        start = self.find_multi_line_bbcode_block_start(state, line)
1✔
65
        if not start:
1✔
66
            return False
1✔
67

68
        line += 1
1✔
69
        while line < endLine:
1✔
70
            if self.find_multi_line_bbcode_block_end(state, line):
1✔
71
                break
1✔
72

73
            line += 1
1✔
74
        else:
NEW
75
            return False
×
76

77
        if silent:
1✔
78
            return True
1✔
79

80
        token = self.state_push_void_token(state, startLine, start.markup, start.attrs)
1✔
81
        token.content = self.get_lines(state, startLine + 1, line)
1✔
82

83
        state.line = line + 1
1✔
84
        return True
1✔
85

86
    def get_lines(self, state: StateBlock, startLine: int, endLine: int):
1✔
87
        length = state.sCount[startLine]
1✔
88
        content = state.getLines(startLine, endLine, length, False).rstrip()
1✔
89

90
        lines: list[str] = []
1✔
91
        for line in content.splitlines():
1✔
92
            if line.strip() or lines:
1✔
93
                lines.append(line)
1✔
94

95
        return dedent("\n".join(lines))
1✔
96

97
    def get_meta(self, attrs: dict) -> dict | None:
1✔
98
        if attrs.get("syntax"):
1✔
99
            return {"syntax": attrs["syntax"]}
1✔
100

101
        return None
1✔
102

103

104
def code_bbcode_renderer(
1✔
105
    renderer: RendererHTML,
106
    tokens: Sequence[Token],
107
    idx: int,
108
    options: OptionsDict,
109
    env: EnvType,
110
) -> str:
111
    token = tokens[idx]
1✔
112

113
    return (
1✔
114
        "<misago-code"
115
        + renderer.renderAttrs(token)
116
        + ">"
117
        + escapeHtml(token.content)
118
        + "</misago-code>\n"
119
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc