8 (
r'--[^\n]*',
'comment'),
9 (
r'[ \t]',
'whitespace'),
10 (
r'\b(print|and|break|do|else|elseif|end|false|for|function|if|in|local|nil|not|or|repeat|return|then|true|until|while)\b',
'keyword'),
11 (
r'[+-]?\d+\.\d+',
'number'),
12 (
r'[+-]?\d+',
'number'),
13 (
r'[a-zA-Z_][a-zA-Z0-9_]*',
'identifier'),
16 (
r'[\+\-\*\/\^%%#=<>~]',
'operator'),
17 (
r'[\[\]\{\}\(\)\.,;:]',
'punctuation'),
18 (
r'\b(and|or)\b',
'boolean'),
19 (
r'\b(nil)\b',
'null'),
24def tokenize_lua(code):
28 for pattern, token_type
in LUATOKENS:
29 match = re.match(pattern, code[i:])
31 value = match.group(0)
32 tokens.append((token_type, value))
40 (
r'[ \t]',
'whitespace'),
41 (
r'\b(true|false|null)\b',
'boolean'),
42 (
r'-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?',
'number'),
43 (
r'"(?:\\.|[^"\\])*"',
'string'),
44 (
r'[\[\]\{\}\:,]',
'punctuation'),
49def tokenize_json(code):
53 for pattern, token_type
in JSONTOKENS:
54 match = re.match(pattern, code[i:])
56 value = match.group(0)
57 tokens.append((token_type, value))
70 'operator':
'#C7C7C7',
71 'punctuation':
'#D4D4D4',
74 'identifier':
'#9CC9FA',
77TOKEN_DEFAULT_COLOR =
'#D4D4D4'
80code_block_regex = re.compile(
r'```(lua|json)([\s\S]+?)```|([\s\S]+?)')
82def extract_code_blocks(text):
85 while pos < len(text):
86 match = code_block_regex.match(text, pos)
88 code_type = match.group(1)
90 code_blocks.append((code_type, code))
92 code_blocks.append((
'text', match.group(3)))
96def decorate_tokens(tokens, spaces):
99 html +=
' ' * spaces[space_n]
101 if token[0] ==
'newline':
104 html +=
'<span>' +
' ' * spaces[space_n] +
'</span>'
105 elif token[0] ==
'whitespace':
107 elif token[0] ==
'comment':
108 color = TOKEN_COLORS[token[0]]
109 minus =
'<span class="' + token[0] +
'_code_block" style="color: ' + color +
'">-</span>'
111 html +=
'<span class="' + token[0] +
'_code_block" style="color: ' + color +
'">' + token[1][2:] +
'</span>'
113 color = TOKEN_COLORS[token[0]]
114 html +=
'<span class="' + token[0] +
'_code_block" style="color: ' + color +
'">' + token[1] +
'</span>'
118def decorate_codeblocks(code_blocks):
120 for code_block
in code_blocks:
121 if code_block[0] ==
'lua':
122 md +=
'<div class="fragment">\n'
123 tokens = tokenize_lua(code_block[1])
124 spaces = count_leading_spaces(code_block[1])
125 md += decorate_tokens(tokens, spaces)
127 elif code_block[0] ==
'json':
128 md +=
'<div class="fragment">\n'
129 tokens = tokenize_json(code_block[1])
130 spaces = count_leading_spaces(code_block[1])
131 md += decorate_tokens(tokens, spaces)
138def count_leading_spaces(text):
139 lines = text.splitlines()
142 stripped_line = line.lstrip()
143 leading_spaces.append(len(line) - len(stripped_line))
144 leading_spaces.append(0)
146 return leading_spaces
148for root, dirs, files
in os.walk(
'./'):
149 for filename
in files:
150 namepath = os.path.join(root, filename)
151 if namepath.find(
'.md') != -1:
152 with open(namepath,
'r')
as file:
155 if code.find(
'<div class="fragment">') != -1:
156 print(
'already decorated')
159 print(
'colorize code in ' + namepath)
161 code_blocks = extract_code_blocks(code)
162 md = decorate_codeblocks(code_blocks)
164 with open(namepath,
'w')
as file: