DaFluffyPotato's Blog

a blog for stuff I don't want to put on YouTube | main website

I've updated the website to support code in blog posts!

posted April 19, 2020 @ 07:25 UTC

I'll be able to post some code snippets in a useful way now.
```python
# this is embedded code
for i in range(10):
print('Hello world!')
```
I'm considering writing out a text version of my YouTube tutorials in the future, so I thought I'd implement this.

For the fun of it, I've added the code for my markup script below:
```python
import keyword
built_in_list = ['abs', 'delattr', 'hash', 'memoryview', 'set', 'all', 'dict', 'help', 'min', 'setattr', 'any', 'dir', 'hex', 'next', 'slice', 'ascii', 'divmod', 'id', 'object', 'sorted', 'bin', 'enumerate', 'input', 'oct', 'staticmethod', 'bool', 'eval', 'int', 'open', 'str', 'breakpoint', 'exec', 'isinstance', 'ord', 'sum', 'bytearray', 'filter', 'issubclass', 'pow', 'super', 'bytes', 'float', 'iter', 'print', 'tuple', 'callable', 'format', 'len', 'property', 'type', 'chr', 'frozenset', 'list', 'range', 'vars', 'classmethod', 'getattr', 'locals', 'repr', 'zip', 'compile', 'globals', 'map', 'reversed', '__import__', 'complex', 'hasattr', 'max', 'round']
keyword_list = keyword.kwlist

keyword_begin = ''
built_in_begin = ''
all_end = '
'

var_chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_']

def parse_code(code, encoding):
parse_index = {
'python': parse_python_code,
}
if encoding in parse_index:
return parse_index[encoding](code)
else:
return parse_basic_code(code)

def parse_basic_code(code):
output = ''
for line in code.split('\n'):
line = line.replace('&', '&')
line = line.replace('<', '<')
line = line.replace('>', '>')
line = line.replace(' ', ' ')
line += '
'
output += line
return output

def parse_python_code(code):
output = ''
for line in code.split('\n'):
line = line.replace('&', '&')
line = line.replace('<', '<')
line = line.replace('>', '>')
line = line.replace(' ', ' ')
line += '
'
in_comment = False
in_string = False
current_word = ''
escape = False
for char in line:
char_pre = ''
char_post = ''
if char.lower() in var_chars:
current_word += char
else:
word_highlight_begin = ''
word_highlight_end = ''
if (not in_comment) and (not in_string):
if current_word in keyword_list:
word_highlight_begin = keyword_begin
word_highlight_end = all_end
if current_word in built_in_list:
word_highlight_begin = built_in_begin
word_highlight_end = all_end
if char in ['\'', '"']:
if not in_comment:
if not in_string:
in_string = char
char_pre = ''
elif not escape:
if char == in_string:
in_string = False
char_post = '
'
if char == '#':
if not in_string:
in_comment = True
char_pre = ''
output += word_highlight_begin + current_word + word_highlight_end + char_pre + char + char_post
current_word = ''
escape = False
if char == '\\':
escape = True
if (in_comment) or (in_string):
output = output[:-4] + '
' + output[-4:]

return output
```

6.5K views

Comments: