Code Highlighting Test
Code syntax highlighting for this site is powered by Pygments and pymdown-extensions, which basically works in a local deploying manner.
The light mode color scheme used is a contrast enhanced version of Sublime's "Breakers", while the dark mode color scheme used is "Monokai". Light and dark themes synchronize with your device system settings (only tested on Windows 10 PC and iOS13).
Demonstrations
Python3
"""
docstring
"""
from bar import foo
import bar2 as b2
class myClass1(object):
"""docstring"""
def __init__(self, var1, var2=1):
"""Initialize"""
self.var1 = var1
# random hash comment
self.var2 = var2
def classMethod1(self, n):
return lambda x: x + n
@property
def Var3(self, mat1, mat2):
mat = mat1 @ mat2
for i in mat:
print(i)
str(i)
r'(?i)(?<!a)[.*?!]mvfk(?#Yo)i{1,2}}'
return self.var1 + self.var2
def myFun(a: str, b: int = 1) -> str:
try:
foo()
except ValueError:
pass
for i in range(len(a)):
a += str(b)
if __name__ == "__main__":
mc = myClass1(1, 2)
b = mc.classMethod1(2)(33) + mc.Var3
a = myFun('Hello', b=2)
print(a, f'fstring{1.23}', 'Format {}.'.format(233))
print("and %s like %s" % ("something", "this"))
Bash
files_list=( "a.txt" "b.txt" "c and space.txt" )
GREETING=Yoooooo
echo_greeting() {
echo $1
}
for i in "${files_list[@]}"; do
if [ -f "$i" ]; then
head -n5 "$i" | grep $GREETING >> all_greetings.txt
else
echo_greeting $GREETING
fi
done
Pygment highlighting performance is much better than Google's code-prettify that I adopted previously. However, none of these beats Sublime's highlighting strategy, in terms of syntax detection.
Currently, I have already implemented my customized Python syntax detection library, at pyHiliter, which work as a Pygments plugin, and can be directly adopted by PyMdown Extensions.
(Will be updated for other languages if needed. It might happen other languages are presented in other articles of mine.)
Python Markdown Module with Code Syntax Highlighting
First I would like to clarify that, due to the method I implemented this blog site, an almost fully DIY way, this might be hard to work for other deployment tool users.
Dependencies needed can be installed with the following command. I tried a few of other alternatives and found these ones most match my expectations. For my customized Python detection, refer to the repo's README.
pip install markdown pymdown-extensions Pygments
In Python3 deployment code:
import markdown
# Here I only list the extension needed for syntax highlighting
# Other extensions may also be necessary for other Markdown-to-HTML
# features, such as inserting an "id" attribute to header elements.
EXT = ['pymdownx.superfences']
def md_to_HTML(filename):
'''
Reads a Markdown file directly and returns HTML text
'''
with open(filename, 'r') as f:
raw_MD_text = f.read()
f.close()
html = markdown.markdown(raw_MD_text, extensions=EXT)
return html
if __name__ == '__main__':
HTML = md_to_HTML('my_post.md')
# And then you will insert this HTML element into a template document.
# BeautifulSoup4 is recommended.
Comments