57 lines
1.2 KiB
Python
57 lines
1.2 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
Common functions to other scripts.
|
|
|
|
v2.0
|
|
https://github.com/cotes2020/jekyll-theme-chirpy
|
|
© 2018-2019 Cotes Chung
|
|
MIT License
|
|
'''
|
|
|
|
import sys
|
|
import os
|
|
import glob
|
|
|
|
|
|
def get_yaml(path):
|
|
"""
|
|
Return the Yaml block of a post and the linenumbers of it.
|
|
"""
|
|
end = False
|
|
yaml = ""
|
|
num = 0
|
|
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
for line in f.readlines():
|
|
if line.strip() == '---':
|
|
if end:
|
|
break
|
|
else:
|
|
end = True
|
|
continue
|
|
else:
|
|
num += 1
|
|
|
|
yaml += line
|
|
|
|
return yaml, num
|
|
|
|
|
|
def get_makrdown_files(path):
|
|
MD_EXTENSIONS = ["md", "markdown", "markdn", "mdown"]
|
|
ret_files = []
|
|
|
|
for extension in MD_EXTENSIONS:
|
|
ret_files.extend(glob.glob(os.path.join(path, "*." + extension)))
|
|
|
|
return ret_files
|
|
|
|
|
|
def check_py_version():
|
|
if not sys.version_info.major == 3 and sys.version_info.minor >= 5:
|
|
print("WARNING: This script requires Python 3.5 or higher, "
|
|
"however you are using Python {}.{}."
|
|
.format(sys.version_info.major, sys.version_info.minor))
|
|
sys.exit(1)
|