Skip to content

基于Markdown生成文件

代码

将md文件base64编码后做为命令参数

shell
python -c "import base64;exec(base64.b64decode('aW1wb3J0IHJlCmZyb20gYmFzZTY0IGltcG9ydCBiNjRkZWNvZGUKaW1wb3J0IHN5cwppbXBvcnQgb3MKCgpkZWYgZ2VuX2RhdGEodGV4dCk6CgljID0gMAoJbCA9IFtdCglkID0gewoJCSdmaWxlbmFtZSc6ICcnLAoJCSdjb250ZW50JzogJycsCgl9Cglmb3IgaXRlbSBpbiB0ZXh0LnNwbGl0KCdcbicpOgoJCW1hdGNoX2ZpbGVuYW1lID0gcmUubWF0Y2gocideIyMgKC4qPykkJywgaXRlbSkKCQlpZiBtYXRjaF9maWxlbmFtZToKCQkJZmlsZW5hbWUgPSBtYXRjaF9maWxlbmFtZS5ncm91cCgxKQoJCQlkWydmaWxlbmFtZSddID0gZmlsZW5hbWUKCQllbGlmIGxlbihkWydmaWxlbmFtZSddKToKCQkJaWYgaXRlbS5zdGFydHN3aXRoKCdgYGAnKToKCQkJCWlmIGMgPT0gMToKCQkJCQlsLmFwcGVuZChkKQoJCQkJCWMgPSAwCgkJCQkJZCA9IHsKCQkJCQkJJ2ZpbGVuYW1lJzogJycsCgkJCQkJCSdjb250ZW50JzogJycsCgkJCQkJfQoJCQkJZWxzZToKCQkJCQljICs9IDEKCQkJZWxzZToKCQkJCWlmIGMgPiAwOgoJCQkJCWRbJ2NvbnRlbnQnXSArPSBpdGVtICsgJ1xuJwoJcmV0dXJuIGwKCnRleHQgPSBiNjRkZWNvZGUoc3lzLmFyZ3ZbMV0uZW5jb2RlKCkpLmRlY29kZSgpCmZvciBpdGVtIGluIGdlbl9kYXRhKHRleHQpOgoJcCA9IGl0ZW1bJ2ZpbGVuYW1lJ10KCXRyeToKCQlpZiBub3Qgb3MucGF0aC5leGlzdHMob3MucGF0aC5kaXJuYW1lKHApKToKCQkJb3MubWtkaXIob3MucGF0aC5kaXJuYW1lKHApKQoJZXhjZXB0OgoJCXBhc3MKCXByaW50KHApCgl3aXRoIG9wZW4ocCwgJ3cnLCBlbmNvZGluZz0ndXRmLTgnKSBhcyBmcDoKCQlmcC53cml0ZShpdGVtWydjb250ZW50J10pCg=='.encode()).decode())"

源码

python
import re
from base64 import b64decode
import sys
import os


def gen_data(text):
	c = 0
	l = []
	d = {
		'filename': '',
		'content': '',
	}
	for item in text.split('\n'):
		match_filename = re.match(r'^## (.*?)$', item)
		if match_filename:
			filename = match_filename.group(1)
			d['filename'] = filename
		elif len(d['filename']):
			if item.startswith('```'):
				if c == 1:
					l.append(d)
					c = 0
					d = {
						'filename': '',
						'content': '',
					}
				else:
					c += 1
			else:
				if c > 0:
					d['content'] += item + '\n'
	return l

text = b64decode(sys.argv[1].encode()).decode()
for item in gen_data(text):
	p = item['filename']
	try:
		if not os.path.exists(os.path.dirname(p)):
			os.mkdir(os.path.dirname(p))
	except:
		pass
	print(p)
	with open(p, 'w', encoding='utf-8') as fp:
		fp.write(item['content'])

Released under the MIT License.