markdown.py (1645B)
1 # mypy: allow-untyped-defs 2 3 from functools import reduce 4 5 def format_comment_title(product): 6 """Produce a Markdown-formatted string based on a given "product"--a string 7 containing a browser identifier optionally followed by a colon and a 8 release channel. (For example: "firefox" or "chrome:dev".) The generated 9 title string is used both to create new comments and to locate (and 10 subsequently update) previously-submitted comments.""" 11 parts = product.split(":") 12 title = parts[0].title() 13 14 if len(parts) > 1: 15 title += " (%s)" % parts[1] 16 17 return "# %s #" % title 18 19 20 def markdown_adjust(s): 21 """Escape problematic markdown sequences.""" 22 s = s.replace('\t', '\\t') 23 s = s.replace('\n', '\\n') 24 s = s.replace('\r', '\\r') 25 s = s.replace('`', '') 26 s = s.replace('|', '\\|') 27 return s 28 29 30 def table(headings, data, log): 31 """Create and log data to specified logger in tabular format.""" 32 cols = range(len(headings)) 33 assert all(len(item) == len(cols) for item in data) 34 max_widths = reduce(lambda prev, cur: [(len(cur[i]) + 2) 35 if (len(cur[i]) + 2) > prev[i] 36 else prev[i] 37 for i in cols], 38 data, 39 [len(item) + 2 for item in headings]) 40 log("|%s|" % "|".join(item.center(max_widths[i]) for i, item in enumerate(headings))) 41 log("|%s|" % "|".join("-" * max_widths[i] for i in cols)) 42 for row in data: 43 log("|%s|" % "|".join(" %s" % row[i].ljust(max_widths[i] - 1) for i in cols)) 44 log("")