forked from xot/ssst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssst-add
executable file
·264 lines (230 loc) · 7.94 KB
/
ssst-add
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/local/bin/python3
#
# SSST-ADD
#
# Add a new blogpost
#
# Part of SSST:
# A simple static site tool to maintain websites based on markdown and pandoc
#
# Author: Jaap-henk Hoepman ([email protected])
import argparse, sys, os, yaml, datetime, shutil, string, html
from pathlib import Path
# Path to the root of the source files tree
SRC_ROOT = Path('./src')
# Path to the root of the auxiliary files tree
AUX_ROOT = Path('./aux')
# ---
# USER INPUT
# ---
def yes_or_no(question):
while True:
reply = str(input(question+' (y/n): ')).lower().strip()
if reply[0] == 'y':
return True
if reply[0] == 'n':
return False
#---
# WARNINGS AND ERRORS
#---
# logfile
logfile = None
# Abort after warning
pedantic = False
# Verbosity
loglevel = -1
# Outpur the error message, and log it when required. Then exit
# - msg: the error message
def error (msg: str):
m = 'ssst-add: error: %s\n' % str(msg)
sys.stderr.write(m)
if logfile != None:
logfile.write(m)
sys.exit (1)
# Output the warning message, and write it to the log file if it was specified.
# Exit when pedantic processing was specified.
# - msg: the warning message
def warning (msg: str):
m = 'ssst-add: warning: %s\n' % str(msg)
sys.stderr.write(m)
if logfile != None:
logfile.write(m)
if pedantic:
sys.exit (1)
# Output the warning message, and write it to the log file if it was specified.
# Exit when pedantic processing was specified, otherwise ask user whether to
# continue or not.
# - msg: the warning message
def interactive_warning (msg: str):
warning(msg)
if not yes_or_no('Do you want to continue?'):
error('Stopped')
# Output a log message when its level is lower than the specifief verbosity
# on the command line. Always write the message to the log file if it was
# specified
# - level: the log-level for this message
# - msg: the log message
def log (level: int, msg: str):
if level == 0:
m = '# '
else:
m = '-' * level + ' '
m = m + str(msg) + '\n'
if (level <= loglevel):
sys.stdout.write(m)
if logfile != None:
logfile.write(m)
# ---
# YAML
# ---
def get_yaml_dict(src):
try:
with open(src,'r') as f:
yamls = yaml.load_all(f,Loader=yaml.SafeLoader)
for y in yamls:
return y
except:
warning('Invalid YAML block in ' + str(src))
return {}
# ---
# Utility
# ---
# Return the full path of the folder that contains the item
def item_folder (item):
if type(item) is str:
item = Path(item)
return item.parents[0]
# Replace any non letter or digit with '-'; turn uppercase to lowercase
def map_char (c):
if (c in string.ascii_lowercase) or (c in string.digits):
return c
elif (c in string.ascii_uppercase):
return c.lower()
else:
return '-'
# Remove any html escapes (like ) fromt the title. Replace any
# non-letter or digit in the title with -, avoiding repetitions
# of '-' or a '-' at the start or the end. Convert to lowercase
def normalise_title(title):
title = html.unescape(title)
res = ''
lastchar = '-'
for i in range (0,len(title)):
c = map_char(title[i])
if not ((c == '-') and (lastchar == '-')):
lastchar = c
res = res + c
if res[-1] == '-':
return res[:-1]
else:
return res
# Check a list of known categories (determined by inspecting the src-tree)
def check_known_categories(src_root,categories):
# careful: categories may be a single string or None
if categories == None:
categories = [ 'uncategorized' ]
if type(categories) is str:
categories = [ categories ]
cats = []
# TODO: careful with capitalisations of categories
for f in Path(src_root / 'category').glob('*'):
cats.append(f.name)
log(2,'Checking known categories ' + str(cats))
for c in categories:
if c.lower() not in cats:
interactive_warning('Unknown category ' + c)
# Check a list of known tags (determined by inspecting the src-tree)
def check_known_tags(src_root,keywords):
# careful: taks/keywords may be a single string
if type(keywords) is str:
keywords = [ keywords ]
tags = []
# TODO: careful with capitalisations of tags; also file tree unreliable
for f in Path(src_root / 'tag').glob('*'):
tags.append(f.name)
log(2,'Checking known tags ' + str(tags))
for k in keywords:
if k.lower() not in tags:
interactive_warning('Unknown tag ' + k)
# add post at the appropriate location in the src_root (determined by the date
# and the title, using today if no date found in the post).
# Add the date to the post when neccessary
def add(src_root,aux_root,post):
yaml_header = get_yaml_dict(post)
if 'title' in yaml_header:
title = yaml_header['title']
else:
error('Post is missing a title')
# get the date; replace with today if not present (or in wrong format)
if 'date' in yaml_header:
date = yaml_header['date']
if type(date) is str:
interactive_warning('Cannot parse date in post; using today instead')
date = datetime.date.today()
yaml_header['date'] = date
else:
date = datetime.date.today()
yaml_header['date'] = date
# warn for missing or unknown categories
if not 'categories' in yaml_header:
interactive_warning('Post is missing a category')
else:
check_known_categories(aux_root,yaml_header['categories'])
# warn for missing or unknown keywords/tags
if not 'keywords' in yaml_header:
interactive_warning('Post is missing a keyword/tag')
else:
check_known_tags(aux_root,yaml_header['keywords'])
# determine the full pathname for the post
dest = str(src_root) + date.strftime('/%Y/%m/%d/') + \
normalise_title(title) + '/index.md'
# make the parent folder if it doesn't exist yet
parent = item_folder(dest)
log(1,'Making parent ' + str(parent))
os.makedirs(parent, exist_ok=True)
# copy the post to its destination, replacing the yaml
# block with the one with the correct date
log(0,'Adding ' + post + ' to ' + dest)
with open(post,'r') as inf:
s = inf.read()
# the content is after the second occurance of the YML separator '---'
content = s.split('---',maxsplit=2)[2]
with open(dest,'w') as outf:
yaml.dump(yaml_header,outf,explicit_start=True)
outf.write('---\n')
outf.write(content)
# Set up arguments parsing
parser = argparse.ArgumentParser(description='SSST-add. Add a post to a SSST managed blog.')
parser.add_argument('post', help='Filename of post to add')
parser.add_argument('-s', '--source', default=SRC_ROOT,
help='Path to the root of the source files tree')
parser.add_argument('-a', '--aux', default=AUX_ROOT,
help='Path to the root of the auxiliary files tree')
parser.add_argument('-v', '--verbosity', default=loglevel,
help='Verbosity (-1, the default, is silent)')
parser.add_argument('-l', '--logfile', default='ssst-add.log',
help='Name of file to store all log messages in')
parser.add_argument('-p', '--pedantic', default = False, action='store_true',
help='Abort after warning')
args = parser.parse_args()
# Apply arguments
SRC_ROOT = Path(args.source)
AUX_ROOT = Path(args.aux)
if not SRC_ROOT.exists():
error('Source tree %s does not exist.' % SRC_ROOT)
if not AUX_ROOT.exists():
error('Aux tree %s does not exist.' % AUX_ROOT)
post = args.post
if not os.path.exists(post):
error('Post %s does not exist.' % post)
pedantic = args.pedantic
loglevel = int(args.verbosity)
logname = args.logfile
# open logfile
if logname != '':
logfile = open(logname,'w')
# do it
add(SRC_ROOT,AUX_ROOT,post)
# close the logfile if necessary
if logfile != None:
logfile.close()