-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.py
More file actions
566 lines (443 loc) · 19.1 KB
/
Copy pathmain.py
File metadata and controls
566 lines (443 loc) · 19.1 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import os
import re
import yaml
from jinja2 import Environment, FileSystemLoader
import fnmatch
from git import Repo
import uuid
import re
import shutil
from requests import get
DEBUG = False
def std_out(msg, debug=DEBUG):
if debug:
std_out (msg)
def get_frontmatter(content):
if '---\n' not in content:
std_out ('No frontmatter in content')
return None
if '---\n' in content[1:]:
frontmatter = yaml.load('\n'.join(content[1:content[1:].index('---\n')+1]), Loader=yaml.SafeLoader)
else:
frontmatter = None
return frontmatter
def get_ignores(source_folder="docs/"):
if os.path.exists(os.path.join(source_folder, '.macroignore')):
with open(os.path.join(source_folder, '.macroignore'), 'r') as ignore_file:
ignores = ignore_file.read().splitlines()
return ignores
return []
field_color_map = {
'air': 'orange',
'soil': 'red',
'water': 'blue',
'other': 'green'
}
colors = ['black', 'orange', 'red', 'blue', 'green', 'gray']
def on_pre_page_macros(env):
custom_dir = os.path.basename(os.path.normpath(env.conf.theme.custom_dir))
environment = Environment(loader=FileSystemLoader(f"{custom_dir}/templates/"), autoescape=True)
source_folder = os.path.join('docs', env.page.url)
if source_folder != 'docs/':
return
std_out('*****************************')
std_out ('Creating cards under:')
std_out (source_folder)
std_out('*****************************')
ignores = get_ignores(source_folder=source_folder)
for (root,_,files) in os.walk(source_folder):
for file in files:
if file in ignores:
# std_out ('\tIgnoring. Direct match')
continue
ignore_file = False
for ignore in ignores:
if (fnmatch.fnmatch(file, ignore)):
# std_out (ignore)
ignore_file = True
break
if ignore_file:
# std_out ('\tIgnoring. Regex match')
# std_out ('---')
continue
file_path = os.path.join(root, file)
std_out (f'File: {file_path}')
if os.path.exists(file_path):
with open(file_path, 'r') as _file:
content = _file.readlines()
frontmatter = get_frontmatter(content)
if frontmatter is not None:
if 'card' not in frontmatter:
continue
if not frontmatter['card']:
std_out ('\tNot a card!')
continue
if 'name' not in frontmatter:
std_out ('\tERROR: no name in frontmatter!')
continue
frontmatter['target_url'] = '/'+file_path.replace('docs/','').replace('.md','/').replace('index/', '')
if 'field' in frontmatter:
std_out (frontmatter['field'])
# We can only choose one
field = frontmatter['field'][0]
frontmatter['color'] = field_color_map[field]
frontmatter['field_url'] = frontmatter['target_url'].split(field)[0]+f'{field}/'
icon_file = f"{custom_dir}/templates/{field}-icon.html"
if os.path.exists(icon_file):
with open(icon_file, 'r') as iconfile:
icon_text = iconfile.read()
frontmatter['icon'] = icon_text
if 'custom_color' in frontmatter:
if frontmatter['custom_color'] in colors:
frontmatter['color'] = frontmatter['custom_color']
# Do this for different types of units
if 'type' in frontmatter:
if frontmatter['type'] == 'unit':
template = environment.get_template("unit-item.html")
elif frontmatter['type'] == 'sensor':
template = environment.get_template("sensor-item.html")
elif frontmatter['type'] == 'addon':
template = environment.get_template("addon-item.html")
# Firmware is hardcoded - TODO
firmware_repo = 'https://github.com/fablabbcn/smartcitizen-kit-2x/releases/tag/'
if 'versions' in frontmatter:
if 'firmware' in frontmatter['versions']:
frontmatter['firmware_url'] = f"{firmware_repo}{frontmatter['versions']['firmware'].replace('+', '')}"
if 'target' in frontmatter:
metric = frontmatter['target'][0]
frontmatter['metric_url'] = frontmatter['field_url']+f'{metric}/'
std_out ('\tFrontmatter')
std_out (f'\t{frontmatter}')
result = template.render(frontmatter)
os.makedirs(f"{custom_dir}/aux/", exist_ok=True)
html_path = os.path.join(f"{custom_dir}/aux", file.replace('.md', '.html')).replace("index", frontmatter['name'].lower().replace(" ","_"))
std_out (f'\tCreating card {html_path}')
with open(html_path, 'w', encoding='utf-8') as _file:
_file.write(result)
std_out ('--DONE--')
return ''
def define_env(env):
"""
This is the hook for defining variables, macros and filters
- variables: the dictionary that contains the environment variables
- macro: a decorator function, to declare a macro.
"""
@env.macro
def get_file(filename):
full_filename = os.path.join(env.project_dir, 'docs', filename)
if os.path.exists(full_filename):
with open(full_filename, 'r') as f:
lines = f.readlines()
else:
lines = ''
return lines
@env.macro
def get_content(filename):
lines = get_file(filename)
if '---\n' in lines:
content = '\n'.join(lines[lines[1:].index('---\n')+1:])
else:
content = lines
return content
@env.macro
# Inspired by function in mkdocs-snippets-plugin
def get_snippet_git(git_url, file_path, section_name, ignore_frontmatter = True):
repos = dict()
p = re.compile("^#+ ")
m = p.search(section_name)
if m:
section_level = m.span()[1] - 1
root = ""
if repos.get(git_url) is None:
root = "/tmp/" + uuid.uuid4().hex
repos[git_url] = root
Repo.clone_from(git_url, root)
else:
root = repos[git_url]
content = ""
with open(root + '/' + file_path, 'r') as myfile:
content = myfile.read()
p = re.compile("^" + section_name + "$", re.MULTILINE)
start = p.search(content)
start_index = start.span()[1]
p = re.compile("^#{1," + str(section_level) + "} ", re.MULTILINE)
result = ""
end = p.search(content[start_index:])
if end:
end_index = end.span()[0]
result = content[start_index:end_index + start_index]
else:
result = content[start_index:]
# If there are any images, find them, copy them
result = copy_markdown_images(root, result, only_url=False)
if ignore_frontmatter:
result = remove_frontmatter(result)
return result
else:
return "Markdown section doesn't exist in source"
@env.macro
# Inspired by function in mkdocs-snippets-plugin
def get_snippet_url(url, section_name, ignore_frontmatter = True):
repos = dict()
p = re.compile("^#+ ")
m = p.search(section_name)
if m:
section_level = m.span()[1] - 1
content = get(url).text
p = re.compile("^" + section_name + "$", re.MULTILINE)
start = p.search(content)
if start is not None:
start_index = start.span()[1]
p = re.compile("^#{1," + str(section_level) + "} ", re.MULTILINE)
result = ""
end = p.search(content[start_index:])
if end:
end_index = end.span()[0]
result = content[start_index:end_index + start_index]
else:
result = content[start_index:]
# If there are any images, find them, copy them
result = copy_markdown_images(None, result, only_url=True)
if ignore_frontmatter:
result = remove_frontmatter(result)
else:
result = None
return result
else:
return "Markdown section doesn't exist in source"
@env.macro
# Inspired by function in mkdocs-snippets-plugin
def get_snippet_rel(file_path, section_name = None, ignore_frontmatter = True):
ok_to_go = False
try:
with open(env.project_dir + '/' + file_path, 'r') as myfile:
content = myfile.read()
except:
std_out (f'Error found while rendering file: {env.page.file.src_uri}')
std_out (f"Can't find {env.project_dir + '/' + file_path}")
pass
else:
ok_to_go = True
if ok_to_go == False: return None
if section_name is None:
extract = content.split('\n')
if extract[0] == '---':
# std_out ('we have frontmatter!')
if ignore_frontmatter:
# std_out ('remove!')
result = '\n'.join(remove_frontmatter(extract))
else:
result = '\n'.join(extract[1:])
return result
p = re.compile("^#+ ")
m = p.search(section_name)
if m:
section_level = m.span()[1] - 1
p = re.compile("^" + section_name + "$", re.MULTILINE)
start = p.search(content)
if start is not None:
start_index = start.span()[1]
p = re.compile("^#{1," + str(section_level) + "} ", re.MULTILINE)
result = ""
end = p.search(content[start_index:])
if end:
end_index = end.span()[0]
result = content[start_index:end_index + start_index]
else:
result = content[start_index:]
# Images should be in absolute paths
if ignore_frontmatter:
result = remove_frontmatter(result)
else:
result = None
return result
else:
return "Markdown section doesn't exist in source"
@env.macro
def remove_frontmatter(markdown):
if markdown is not None:
if '---' in markdown:
result = markdown[markdown[1:].index('---')+2:]
return result
else:
return markdown
return None
@env.macro
def copy_markdown_images(tmpRoot, markdown, only_url):
# root = os.path.dirname(os.path.dirname(self.page.url))
# root = self.page.url
paths = []
p = re.compile("!\[.*\]\((.*)\)")
it = p.finditer(markdown)
for match in it:
std_out (match)
# Check if it's an URL
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
is_url = re.match(regex, match.group(1))
if is_url:
continue
path = match.group(1)
paths.append(path)
if not only_url:
# TODO - !ONLY URL CASE
destinationPath = os.path.realpath(env.project_dir + "/" +
root + "/gen_/" + path)
if not os.path.isfile(destinationPath):
std_out("Copying image: " + path + " to " + destinationPath)
os.makedirs(os.path.dirname(destinationPath), exist_ok=True)
shutil.copyfile(tmpRoot + "/" + path, destinationPath)
else:
destinationPath = "Image can't be replaced as it's relative to markdown"
for path in paths:
markdown = markdown.replace(path, "gen_/" + path)
return markdown
@env.macro
# TODO - Cleanup
def insert_cards(type = "", filter = None, value = list()):
std_out ('********')
std_out ('Insert cards')
std_out (f'Type: {type}')
std_out (f'Filter: {filter}')
std_out (f'Value: {value}')
std_out ('********')
custom_dir = os.path.basename(os.path.normpath(env.conf.theme.custom_dir))
environment = Environment(loader=FileSystemLoader(f"{custom_dir}/templates/"), autoescape=True)
template = environment.get_template("grid.html")
cards_to_get = []
source_folder = os.path.join('docs', env.page.url)
std_out ('Source folder:', source_folder)
ignores = get_ignores()
for (root,_,files) in os.walk(source_folder):
if files is None: continue
for file in sorted(files):
if file in ignores: continue
file_path = os.path.join(root, file)
if os.path.exists(file_path):
with open(file_path, 'r') as _file:
content = _file.readlines()
frontmatter = get_frontmatter(content)
if frontmatter is not None:
std_out (file)
if 'name' not in frontmatter:
std_out (f'ERROR: {file} has no \'name\' in frontmatter!')
continue
if 'card' not in frontmatter:
std_out (f'ERROR: {file} has no \'card\' in frontmatter!')
continue
if not frontmatter['card']:
std_out (f'INFO: {file} Skipping card')
continue
if filter is not None:
if filter in frontmatter:
item_values = frontmatter[filter]
else:
std_out (f'ERROR: {file} does not have {filter} in frontmatter')
continue
std_out (value, item_values)
if any([item_value in value for item_value in item_values]):
std_out ('adding to get')
cards_to_get.append(file.replace('.md', '.html'))
std_out ('----')
else:
cards_to_get.append(file.replace('.md', '.html'))
else:
std_out (f'ERROR: {file} has no frontmatter')
continue
std_out ('CARDS TO GET')
std_out (cards_to_get)
std_out ('---')
cards = ''
os.makedirs(f"{custom_dir}/aux/", exist_ok=True)
# TODO add a way to get mkdocs file order and sort cards here by that order
if cards_to_get:
std_out ('Adding cards')
for item in sorted(cards_to_get):
file_path = f"{custom_dir}/aux/{item}"
if os.path.exists(file_path):
std_out (f'Adding card: {file_path}')
with open(file_path, 'r', encoding='utf-8') as file:
card = file.read()
if card:
cards+=card
if cards:
result = template.render(cards=cards, wide=False)
return result
return None
@env.macro
def insert_custom_cards(custom_cards = list(), wide_columns = False):
std_out ('Insert custom cards')
custom_dir = os.path.basename(os.path.normpath(env.conf.theme.custom_dir))
environment = Environment(loader=FileSystemLoader(f"{custom_dir}/templates/"), autoescape=True)
template = environment.get_template("grid.html")
source_folder = os.path.join('docs', env.page.url)
std_out ('Source folder:', source_folder)
cards = ''
for item in custom_cards:
file_path = f"{custom_dir}/aux/{item}"
std_out (file_path)
if os.path.exists(file_path):
std_out (f'Adding card: {file_path}')
with open(file_path, 'r', encoding='utf-8') as file:
card = file.read()
if card:
cards+=card
if cards:
result = template.render(cards=cards, wide=wide_columns)
return result
return None
@env.macro
def insert_banner():
# TODO
return None
@env.macro
def insert_guides():
# TODO
return None
@env.macro
# Inspired by function in mkdocs-snippets-plugin
# TODO - Cleanup once it's final
def insert_references(file_path, ignore_frontmatter = True):
ok_to_go = False
try:
with open(env.project_dir + '/' + file_path, 'r') as myfile:
content = myfile.read()
except:
std_out (f'Error found while rendering file: {env.page.file.src_uri}')
std_out (f"Can't find {env.project_dir + '/' + file_path}")
pass
else:
ok_to_go = True
if not ok_to_go:
return None
extract = content.split('\n')
references = {}
std_out ('REFERENCES')
# std_out (extract)
# std_out (type(extract))
# std_out ('PAGE CONTENT')
# std_out (env.page.markdown)
# std_out ('-----------')
for item in extract:
# std_out (item)
if item.startswith('['):
# std_out ('reference key')
if item not in references:
# std_out ('new item')
if item in env.page.markdown:
# std_out ('item in md!')
references[item]=extract[extract.index(item)+1]
else:
std_out ("WARNING: Duplicated item in references")
# std_out ('references')
# std_out (references)
result = '\n'.join('{}\n {}'.format(key, value) for key, value in references.items())
# std_out ('result')
# std_out (result)
std_out ('---')
return result