-
Notifications
You must be signed in to change notification settings - Fork 0
/
colourise.py
85 lines (64 loc) · 2.42 KB
/
colourise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
import sys
from urllib.request import urlopen
# Disable the certificate verification for the GitHub wiki page.
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
COLOURS = {
"red": "\x1b[6;30;41m",
"green": "\x1b[6;30;42m",
"yellow": "\x1b[6;30;43m",
"blue": "\x1b[6;30;44m",
"magenta": "\x1b[6;30;45m",
"cyan": "\x1b[6;30;46m",
"light cyan": "\x1b[6;30;36m",
"empty": "\x1b[0m",
}
TERMS_URL = "https://github.com/ameteiko/goreleaser/wiki/Glossary.md"
# KeywordsReader reads the list of keywords from the remote Wiki page.
class KeywordsReader:
def __init__(self, file_url):
with urlopen(file_url) as response:
body = str(response.read().decode('utf-8'))
self._lines = body.split('\n')
self._terms_index = 0
self._terms_table_index = 0
# Returns the list of terms from the parsed Wiki page.
def read(self):
self._find_terms_section()
self._find_table()
return self._read_table_terms()
# Skips all the lines in the source until the ## Terms section.
def _find_terms_section(self):
for i, l in enumerate(self._lines):
if l.startswith("## Terms"):
self._terms_index = i
break
# Skips all the lines until the table node is found.
def _find_table(self):
for i in range(self._terms_index + 1, len(self._lines)):
if self._lines[i].startswith("| Term "):
self._terms_table_index = i
break
# Returns the terms from the first column of the table.
def _read_table_terms(self):
keys = []
for i in range(self._terms_table_index + 2, len(self._lines)):
table_line = self._lines[i]
if not table_line.startswith("| "):
break
keys.append(table_line[1:table_line.find("|", 1)].strip())
return keys
# Highlights the keywords in the input.
def highlight_keywords(text, kwds):
for keyword in kwds:
text = text.replace(keyword, colourise(keyword, COLOURS['light cyan']))
return text
# Highlights the text with a specified colour.
def colourise(text, colour):
return f'{colour}{text}{COLOURS["empty"]}'
report = sys.stdin.read()
if len(sys.argv) == 2:
TERMS_URL = sys.argv[1]
highlighted_report = highlight_keywords(report, KeywordsReader(TERMS_URL).read())
print(highlighted_report)