Skip to content

Commit

Permalink
Add line numbers to Markdown code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Apr 10, 2024
1 parent 12c563d commit 5567130
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions diff_cover/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,30 @@ def markdown(self):
Return a Markdown representation of the snippet using Markdown fenced code blocks.
See https://github.github.com/gfm/#fenced-code-blocks.
"""

line_number_length = len(str(self._last_line))

text = ""
for i, line in enumerate(self.text().splitlines(), start=self._start_line):
notice = " "
if i in self._violation_lines:
notice = "!"

format_string = "{} {:>" + str(line_number_length) + "}:{}"
text += format_string.format(notice, i, line) + "\n"

header = "Lines %d-%d\n\n" % (self._start_line, self._last_line)
if self._lexer_name in self.LEXER_TO_MARKDOWN_CODE_HINT:
return header + (
"```"
+ self.LEXER_TO_MARKDOWN_CODE_HINT[self._lexer_name]
+ "\n"
+ self.text()
+ text
+ "\n```\n"
)

# unknown programming language, return a non-decorated fenced code block:
return "```\n" + self.text() + "\n```\n"
return "```\n" + text + "\n```\n"

def terminal(self):
"""
Expand Down

0 comments on commit 5567130

Please sign in to comment.