web/_scripts/py/utils/common.py

58 lines
1.2 KiB
Python
Raw Normal View History

2019-09-30 15:38:41 +03:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Common functions to other scripts.
2020-01-02 16:17:49 +03:00
v2.0
https://github.com/cotes2020/jekyll-theme-chirpy
2019-09-30 15:38:41 +03:00
© 2018-2019 Cotes Chung
MIT License
'''
import sys
2020-04-02 18:25:19 +03:00
import os
import glob
2019-09-30 15:38:41 +03:00
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
2020-04-02 18:25:19 +03:00
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
2019-09-30 15:38:41 +03:00
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)