-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-frontmatter.py
executable file
·332 lines (295 loc) · 12.5 KB
/
add-frontmatter.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python3
import argparse
import logging
import os
import sys
import frontmatter
import shutil
from glob import glob
from pathlib import Path
from editfrontmatter import EditFrontMatter
"""Adds frontmatter to markdown file/s in a specified location
This script is meant to be executed on a base Hugo site and would
usually be used as a precursor to markdown-to-confluence
"""
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
log = logging.getLogger(__name__)
parentlist = []
skippedpages = []
title_list = []
num_title_list = []
SUPPORTED_FORMATS = ['.md']
DIFF_FILES = ['.diff']
def find_sub_dirs(path, depth=1):
path = Path(path)
assert path.exists(), f'Path: {path} does not exist'
depth_search = '*/' * depth + '*.md'
search_pattern = os.path.join(path, depth_search)
return list(glob(f'{search_pattern}'))
def find_file_depth(file,args):
for i in range (0,10):
directories=find_sub_dirs(f'{args.dir}/content/',i)
if len(directories) > 0:
if file in directories and args.root:
return (i+1)
elif file in directories:
return (i)
def add_numbering(files, args):
"""Adds frontmatter to markdown files in specified directory
Arguments:
files {list} -- The path to the markdown file/s to be processed
args -- List of runtime arguments
"""
numbering = []
s_files=sorted(files, key=str.lower)
for file in s_files:
depth = find_file_depth(file,args)
fm = frontmatter.load(file)
ctitle = fm['title']
if not file.endswith('_index.md'):
depth = depth + 1
if not depth == 0:
title_list.append(f'{depth} {ctitle}±{file}')
for item in title_list:
num, _, text = item.partition(" ")
text, _,file = text.partition("±")
if int(num) == len(numbering):
numbering[-1] += 1
elif int(num) > len(numbering):
numbering.extend([1] * (int(num) - len(numbering)))
elif int(num) < len(numbering):
numbering = numbering[:int(num)]
numbering[-1] += 1
num_title = (".".join(map(str, numbering)) + ". " + text)
num_title_list.append(num_title + '±' + file)
return(num_title_list)
def populate_title(file):
for title in num_title_list:
num_title, _, file_tracking = title.partition("±")
if file_tracking == file:
return num_title
return ''
def add_frontmatter(files, args):
"""Adds frontmatter to markdown files in specified directory
Arguments:
files {list} -- The path to the markdown file/s to be processed
args -- List of runtime arguments
"""
template_str = ''.join(open(os.path.abspath(os.path.dirname(__file__) + "/template.j2"), "r").readlines())
for file in files:
bakfilename = os.path.basename(file) + '.bak'
bakfile = os.path.join(os.path.dirname(file), bakfilename)
path = Path(file)
if file.endswith('_index.md') and not os.path.dirname(file).endswith('content'):
path = Path(file)
parentpath = os.path.join(path.parents[1], "_index.md")
if not os.path.dirname(parentpath).endswith('content'):
if not os.path.exists(bakfile) and not args.nobackup:
shutil.copyfile(file,bakfile)
pfm = frontmatter.load(parentpath)
cfm = frontmatter.load(file)
if args.number:
num_ctitle = populate_title(file)
cfm['title'] = num_ctitle
num_ptitle = populate_title(parentpath)
pfm['title']= num_ptitle
proc = EditFrontMatter(file_path=file, template_str=template_str)
proc.run({'title': cfm['title'],'parent': pfm['title'] })
log.info(f'Writing frontmatter for {file}')
proc.writeFile(file)
else:
if not os.path.exists(bakfile) and not args.nobackup:
shutil.copyfile(file,bakfile)
cfm = frontmatter.load(file)
if args.number:
num_ctitle = populate_title(file)
cfm['title'] = num_ctitle
proc = EditFrontMatter(file_path=file, template_str=template_str)
if args.root:
pfm = frontmatter.load(parentpath)
if args.number:
num_ptitle = populate_title(parentpath)
pfm['title']= num_ptitle
proc.run({'title': cfm['title'],'parent': pfm['title'] })
log.info(f'Writing frontmatter for {file}')
proc.writeFile(file)
else:
proc.run({'title': cfm['title'] })
log.info(f'Writing frontmatter for {file}')
proc.writeFile(file)
elif not file.endswith('_index.md'):
if not os.path.exists(bakfile) and not args.nobackup:
shutil.copyfile(file,bakfile)
path = Path(file)
parentpath = os.path.join(path.parents[0], "_index.md")
pfm = frontmatter.load(parentpath)
cfm = frontmatter.load(file)
if args.number:
num_ctitle = populate_title(file)
cfm['title'] = num_ctitle
num_ptitle = populate_title(parentpath)
pfm['title']= num_ptitle
proc = EditFrontMatter(file_path=file, template_str=template_str)
proc.run({'title': cfm['title'],'parent': pfm['title'] })
log.info(f'Writing frontmatter for {file}')
proc.writeFile(file)
elif file.endswith('_index.md') and os.path.dirname(file).endswith('content'):
if not os.path.exists(bakfile) and not args.nobackup:
shutil.copyfile(file,bakfile)
cfm = frontmatter.load(file)
if args.root:
if args.number:
num_ctitle = populate_title(file)
cfm['title'] = num_ctitle
proc = EditFrontMatter(file_path=file, template_str=template_str)
proc.run({'title': cfm['title'] })
log.info(f'Writing frontmatter for {file}')
proc.writeFile(file)
log.info(f'Write complete for {len(files)} files')
def parse_args():
# Parse command line arguments
parser = argparse.ArgumentParser(
description='Adds required frontmatter to markdown page/s so they can be synced using markdown-to-confluence')
parser.add_argument(
'--dir',
dest='dir',
default='None',
help='The path to your directory containing markdown pages (default: None)'
)
parser.add_argument(
'--root',
dest='root',
action='store_true',
help='Sets all base pages parent to be the root page'
)
parser.add_argument(
'--nobackup',
dest='nobackup',
action='store_true',
help='Does not create backups of all modified files'
)
parser.add_argument(
'--number',
dest='number',
action='store_true',
help='Adds numbering to the titles in the frontmatter'
)
parser.add_argument(
'--force',
dest='force',
action='store_true',
help='Forces overwrite of the frontmatter if it already exists'
)
parser.add_argument(
'--diff',
dest='diff',
action='store_true',
help='Forces overwrite of the frontmatter from a matching diff file in same directory'
)
parser.add_argument(
'--dryrun',
dest='dryrun',
action='store_true',
help='Prints changes that would be made to files without modifying them'
)
parser.add_argument(
'posts',
type=str,
nargs='*',
help=
'Individual pages to add frontmatter to'
)
args = parser.parse_args()
if len(sys.argv)==1:
parser.print_help(sys.stderr)
sys.exit(1)
return parser.parse_args()
def main():
args = parse_args()
if args.posts:
changed_posts = [os.path.abspath(post) for post in args.posts]
for post_path in changed_posts:
if not os.path.exists(post_path) or not os.path.isfile(post_path):
log.error('File doesn\'t exist: {}'.format(post_path))
sys.exit(1)
elif args.dir != 'None':
log.info(f'Checking content pages in directory {args.dir}')
changed_posts = [
os.path.join(path, name) for path, subdirs, files in os.walk(args.dir) for name in files
]
for filepath in changed_posts[:]:
_,ext = os.path.splitext(f'{filepath}')
if ext not in SUPPORTED_FORMATS:
changed_posts.remove(f'{filepath}')
elif not filepath.startswith(f'{args.dir}/content/'):
changed_posts.remove(f'{filepath}')
else:
log.info('No pages found in input source')
return
if not args.force:
for p in changed_posts[:]:
fm = frontmatter.load(p)
try:
test = fm['wiki']
except:
log.info(f'{p} set to be processed as it contains no Wiki frontmatter')
else:
changed_posts.remove(p)
if args.number:
add_numbering(changed_posts,args)
add_frontmatter(changed_posts, args)
if args.diff:
diff_posts = [
os.path.join(path, name) for path, subdirs, files in os.walk(args.dir) for name in files
]
log.info('\n\n')
log.info('Processing any existing diff files ........')
for filepath in diff_posts[:]:
_,ext = os.path.splitext(f'{filepath}')
if ext not in DIFF_FILES or not filepath.startswith(f'{args.dir}/content/'):
diff_posts.remove(f'{filepath}')
for filepath in diff_posts[:]:
file,ext = os.path.splitext(f'{filepath}')
if file in changed_posts:
template_str = ''.join(open(os.path.abspath(os.path.dirname(__file__) + "/template-diff.j2"), "r").readlines())
difffm = frontmatter.load(filepath)
origfm = frontmatter.load(file)
try:
test = difffm['wiki']
except:
log.info(f'{filepath} No Wiki frontmatter in diff file. Ignoring.')
else:
if difffm['wiki'].get('title') == None:
log.info(f'{filepath} - No Wiki title frontmatter tag in diff file. Ignoring.')
else:
origfm['wiki']['title'] = difffm['wiki'].get('title')
if difffm['wiki'].get('parent') == None:
log.info(f'{filepath} - No Wiki parent frontmatter tag in diff file. Ignoring.')
else:
origfm['wiki']['parent'] = difffm['wiki'].get('parent')
if difffm['wiki'].get('share') == None:
log.info(f'{filepath} - No Wiki share frontmatter tag in diff file. Ignoring.')
else:
origfm['wiki']['share'] = difffm['wiki'].get('share')
proc = EditFrontMatter(file_path=file, template_str=template_str)
proc.run({'share': (str(origfm['wiki'].get('share')).lower()),'title': origfm['wiki'].get('title'),'parent': origfm['wiki'].get('parent')})
log.info(f'Writing diff frontmatter from {filepath} to {file}')
proc.writeFile(file)
log.info(f'Diff processing complete for {len(diff_posts)} diff files')
seen = set()
for p in changed_posts[:]:
fm = frontmatter.load(p)
if fm['wiki'].get('title') not in seen:
seen.add(fm['wiki'].get('title'))
else:
skippedpages.append('"' + fm['wiki'].get('title') + '"' + ' at ' + p)
if len(skippedpages) > 0:
log.info('\n\n')
log.info('---------- WARNING -----------')
log.info('Confluence does not allow the creation of pages with the same title within the same Confluence Space.')
log.info(f'{len(skippedpages)} pages could cause errors due to duplicate page titles. Please check the pages listed below for duplicate titles:\n')
for s in skippedpages:
log.info(s)
log.info('--------------------------------------')
if __name__ == '__main__':
main()