forked from linuxdeepin/developer-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.py
executable file
·63 lines (48 loc) · 1.27 KB
/
new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import locale
meta = '''Title: My Document
Summary: A brief description of my document.
Authors: Waylan Limberg
Date: October 2, 2007
'''
def usage():
help = u'''Use new.py like:
new.py category title path-to-file
example:
new.py 深度截图 入门指南 deepin-shot/guide.md
it will create a file at "docs/deepin-shot/guide.md" with this content:
title:
category:首页
'''
print(help)
if __name__=="__main__":
if len(sys.argv)!= 4:
usage()
sys.exit(1)
encoding = locale.getdefaultlocale()[1]
meta = {}
category = sys.argv[1].decode(encoding, 'ignore')
title = sys.argv[2].decode(encoding, 'ignore')
file = sys.argv[3]
if not file.endswith(".md"):
print ("path-to-file must end with .md\n")
usage()
sys.exit(2)
meta["category"]=category
meta["title"]=title
mdmeta = "<!--Meta\n"
for k, v in meta.items():
mdmeta += k+":"+v+"\n"
mdmeta += "DO NOT Delete Meta Above -->\n"
path = "docs/" + file
try:
dir, name = os.path.split(path)
os.makedirs(dir)
except:
pass
fp = open(path, "w")
fp.writelines(mdmeta.encode('utf8', 'ignore'))
fp.close()