Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions campbot/checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ def get_report(self, bot, lang, with_date=False):
return "\n".join(result)


def build_legend(tests):
"""
Legend of the markers, as a Markdown table: BBCode ([color=red]) and Markdown
are not interpreted inside HTML blocks (<table>), only in Markdown tables.
"""

def cell(text):
return str(text).replace("|", "&#124;").replace("\n", " ") or " "

lines = [
"[details=Signification des icônes]\n",
"| Test | A relire | Corrigé |",
"|---|---|---|",
]
for test in tests:
lines.append(
"| {} | {} | {} |".format(
cell(test.name), cell(test.fail_marker), cell(test.success_marker)
)
)
lines.append("\n[/details]")
return "\n".join(lines)


def report_recent_changes(bot, days, lang, thread_url):

newest_date = utils.today().replace(hour=0, minute=0, second=0, microsecond=0)
Expand All @@ -223,18 +247,10 @@ def report_recent_changes(bot, days, lang, thread_url):
if len(reports) != 0:
messages = [
"[Explications]({})\n".format(thread_url),
"[details=Signification des icônes]\n<table>",
"<tr><th>Test</th><th>A relire</th><th>Corrigé</th></tr>",
build_legend(tests),
"\n----\n",
]

for test in tests:
messages.append("<tr>")
messages.append("<th>{}</th>".format(test.name))
messages.append("<td>{}</td>".format(test.fail_marker))
messages.append("<td>{}</td>".format(test.success_marker))
messages.append("</tr>")

messages.append("</table>\n[/details]\n\n----\n\n")
messages += [report.get_report(bot, lang, days > 1) for report in reports]

logging.info(f"Reporting {len(reports)} reports:\n\n" + "\n".join(messages))
Expand Down
32 changes: 32 additions & 0 deletions tests/test_legend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from types import SimpleNamespace

from campbot.checkers import build_legend


def _test(name, fail, success):
return SimpleNamespace(name=name, fail_marker=fail, success_marker=success)


def test_legend_is_a_markdown_table_without_html():
legend = build_legend(
[
_test("Termes suspects", "[color=red]**?**[/color]", "."),
_test(
"Diverses erreurs",
"[color=red]**X**[/color]",
"[color=green]**X**[/color]",
),
]
)

# BBCode is not interpreted inside HTML blocks, so no HTML table
assert "<table>" not in legend and "<td>" not in legend
assert legend.startswith("[details=Signification des icônes]\n\n| Test |")
assert "|---|---|---|" in legend
assert "| Termes suspects | [color=red]**?**[/color] | . |" in legend
assert legend.endswith("[/details]")


def test_legend_cells_cannot_break_the_table():
legend = build_legend([_test("a|b", "", "x\ny")])
assert "| a&#124;b | | x y |" in legend
Loading