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 24
/
generate_template.py
161 lines (141 loc) · 5.26 KB
/
generate_template.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
import argparse
from pathlib import Path
from string import Template
from typing import Literal
import requests
from pydantic import BaseModel
class TemplateModel(BaseModel):
template_type: Literal["gh_pages", "netlify", "vercel"]
site_name: str
site_url: str
site_description: str
site_author: str
language: str
auto_h1: bool
comments: bool
generate_graph: bool = False
class Environment(BaseModel):
env: str
deploy: str
netlify = Environment(
env="https://github.com/ObsidianPublisher/actions/raw/main/template/netlify/.env",
deploy="https://raw.githubusercontent.com/ObsidianPublisher/actions/main/template/netlify/deploy.yml",
)
vercel = Environment(
deploy="https://raw.githubusercontent.com/ObsidianPublisher/actions/main/template/vercel/deploy.yml",
env="https://raw.githubusercontent.com/ObsidianPublisher/actions/main/template/vercel/.env",
)
gh_pages = Environment(
deploy="https://raw.githubusercontent.com/ObsidianPublisher/actions/main/template/gh-pages/deploy.yml",
env="https://raw.githubusercontent.com/ObsidianPublisher/actions/main/template/gh-pages/.env",
)
def main() -> None:
parser = argparse.ArgumentParser(
description="Generate a template for Obsidian Publisher"
)
parser.add_argument(
"template",
type=str,
help="The template to generate",
choices=["gh_pages", "netlify", "vercel"],
)
parser.add_argument("site_name", type=str, help="The name of the site")
parser.add_argument("site_url", type=str, help="The url of the site")
parser.add_argument(
"site_description", type=str, help="The description of the site"
)
parser.add_argument("site_author", type=str, help="The author of the site")
parser.add_argument("language", type=str, help="The language of the site")
parser.add_argument(
"--auto-h1", action="store_true", help="Automatically add h1 to the title"
)
parser.add_argument(
"--comments", action="store_true", help="Enable comments on the site"
)
args = parser.parse_args()
template = TemplateModel(
template_type=args.template,
site_name=args.site_name,
site_url=args.site_url,
site_description=args.site_description,
site_author=args.site_author,
language=args.language,
auto_h1=args.auto_h1,
comments=args.comments,
generate_graph=True if args.template == "gh_pages" else False,
)
env = gh_pages
if args.template == "netlify":
env = netlify
elif args.template == "vercel":
env = vercel
# download the files
env_path = Path(".github/.env")
deploy_path = Path(".github/workflows/deploy.yml")
with env_path.open("w", encoding="UTF-8") as f:
f.write(requests.get(env.env).text)
with deploy_path.open("w", encoding="UTF-8") as f:
f.write(requests.get(env.deploy).text)
# write the template
mkdocs_yaml = Path("mkdocs.yml")
with mkdocs_yaml.open("r", encoding="UTF-8") as f:
mkdocs = f.read()
mkdocs = Template(mkdocs)
s = mkdocs.substitute(
site_name=template.site_name,
site_url=template.site_url,
site_description=template.site_description,
site_author=template.site_author,
language=template.language,
auto_h1=template.auto_h1,
comments=template.comments,
generate_graph=template.generate_graph,
)
requirements = [
"mkdocs==1.5.3",
"mkdocs-material==9.5.3",
"mkdocs-ezlinked-plugin==0.3.3",
"mkdocs-awesome-pages-plugin==2.9.2",
"mdx_breakless_lists==1.0.1",
"mkdocs-embed-file-plugins==2.0.9",
"mkdocs_custom_fences==0.1.2",
"mkdocs-git-revision-date-localized-plugin==1.2.2",
"mkdocs-encryptcontent-plugin==3.0.2",
"mkdocs-callouts==1.10.0",
"mkdocs-custom-tags-attributes==0.3.1",
"mkdocs-meta-descriptions-plugin==3.0.0",
"mkdocs-glightbox==0.3.6",
]
requirements_actions_content = [
"obsidiantools==0.10.0",
"pyvis==0.3.2",
"cairosvg==2.7.1",
"pillow==10.2.0",
]
requirements_actions = Path("requirements_actions.txt")
if template.template_type in ["netlify", "vercel"]:
# create requirements_actions.txt
with requirements_actions.open("w", encoding="UTF-8") as f:
f.write("\n".join(requirements_actions_content))
if template.template_type == "netlify":
with Path("runtime.txt").open("w", encoding="UTF-8") as f:
f.write("3.8")
elif template.template_type == "gh_pages":
requirements += requirements_actions_content
# delete requirements_actions.txt
if requirements_actions.exists():
requirements_actions.unlink()
with Path("requirements.txt").open("w", encoding="UTF-8") as f:
f.write("\n".join(requirements))
with mkdocs_yaml.open("w", encoding="UTF-8") as f:
f.write(s)
print("Mkdocs template generated:")
print(s)
print("Requirements generated:")
print("\n".join(requirements))
if template.template_type in ["netlify", "vercel"]:
print("Requirements for actions generated:")
print("\n".join(requirements_actions_content))
print("Done!")
if __name__ == "__main__":
main()