This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcategory.py
111 lines (98 loc) · 3.7 KB
/
category.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
"""
A simple script that help to create a category in the database. Create the folder and the `index.md` file.
Usage :
usage: category.py [-h] [--parent PARENT] [--description DESCRIPTION] [--toc] [--nav] name
positional arguments:
name Name of the category.
options:
-h, --help show this help message and exit
--parent PARENT Parent category.
--description DESCRIPTION Description of the category.
--toc hide toc
--nav hide nav
"""
import argparse
import yaml
from pathlib import Path
import re
def create_category(path, index_contents):
path.mkdir(parents=True, exist_ok=True)
index_file = Path(path, 'index.md')
with open(index_file, 'w', encoding='utf-8') as f:
f.write(index_contents.lstrip())
def index_contents(name, description_yaml, hider, description):
index_contents = f'''
---
index: true{description_yaml}
hidden: true
category: {name}
template: article.html
comments: false
title: {name}{hider}
---
{description}
'''
index_contents = re.sub(' ', '', index_contents)
return index_contents
def resolving_args(args, docs_dir):
if args.parent:
path = Path(docs_dir, args.parent, args.name)
else:
path = Path(docs_dir, args.name)
if args.description:
description_yaml = '\ndescription: ' + args.description
description_contents = args.description
else:
description_yaml = ''
description_contents = ''
hider = []
if args.toc:
hider.append('toc')
if args.nav:
hider.append('navigation')
hider = '\nhide:\n- ' + '\n- '.join(hider) if hider else ''
return path, description_yaml, hider, description_contents
def main():
parser = argparse.ArgumentParser(
description='Create a new category your mkdocs documentations from your docs (or configured directory).')
parser.add_argument('name', help='Name of the category.')
parser.add_argument(
'--parent',
help='Parent category, in path. Example : "category/subcategory"')
parser.add_argument('--description', help='Description of the category.')
parser.add_argument('--toc', help='hide toc', action='store_true')
parser.add_argument('--nav', help='hide nav', action='store_true')
parser.add_argument(
'--dry-run',
help='Dry run, do not create the category, just log',
action='store_true')
args = parser.parse_args()
mkdocs_config = Path('mkdocs.yml').resolve()
with open(mkdocs_config, 'r', encoding='utf-8') as f:
config = yaml.load(f, Loader=yaml.BaseLoader)
docs_dir = config.get('docs_dir', 'docs')
docs_dir = Path(docs_dir).resolve()
path, description_yaml, hider, description_contents = resolving_args(
args, docs_dir)
index = index_contents(
args.name,
description_yaml,
hider,
description_contents)
if not args.dry_run:
print('📤 Creating category 📤...')
print(
f'\nArguments used :\n- Name: {args.name}\n- Parents: {args.parent}\n- Description: {args.description}\n- Toc: {args.toc}\n- Nav: {args.nav}\n')
print(f'📌 Creating with path : {path}')
create_category(path, index)
print('Category created ! 🎉')
else:
print('🐍 Dry Run 🐍')
print(
f'\nArguments used :\n- Name: {args.name}\n- Parents: {args.parent}\n- Description: {args.description}\n- Toc: {args.toc}\n- Nav: {args.nav}\n')
print(f'🚃 Path : {path}')
print(f'🗃️ Index : {index}')
if __name__ == '__main__':
main()
print('Done.')
exit(0)