forked from stevearc/resession.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
executable file
·99 lines (82 loc) · 2.55 KB
/
generate.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
import os
import re
from typing import List
from nvim_doc_tools import (
Vimdoc,
VimdocSection,
generate_md_toc,
indent,
parse_directory,
read_section,
render_md_api2,
render_vimdoc_api2,
replace_section,
)
HERE = os.path.dirname(__file__)
ROOT = os.path.abspath(os.path.join(HERE, os.path.pardir))
README = os.path.join(ROOT, "README.md")
DOC = os.path.join(ROOT, "doc")
VIMDOC = os.path.join(DOC, "resession.txt")
def update_config_options():
config_file = os.path.join(ROOT, "lua", "resession", "config.lua")
opt_lines = read_section(config_file, r"^local default_config =", r"^}$")
replace_section(
README,
r"^<!-- Setup -->$",
r"^<!-- /Setup -->$",
["\n", "```lua\n", 'require("resession").setup({\n']
+ opt_lines
+ ["})\n", "```\n", "\n"],
)
def get_options_vimdoc() -> "VimdocSection":
section = VimdocSection("options", "resession-options")
config_file = os.path.join(ROOT, "lua", "resession", "config.lua")
opt_lines = read_section(config_file, r"^local default_config =", r"^}$")
lines = ["\n", ">\n", ' require("resession").setup({\n']
lines.extend(indent(opt_lines, 4))
lines.extend([" })\n", "<\n"])
section.body = lines
return section
def generate_vimdoc():
doc = Vimdoc("resession.txt", "resession")
types = parse_directory(os.path.join(ROOT, "lua"))
funcs = types.files["resession/init.lua"].functions
doc.sections.extend(
[
get_options_vimdoc(),
VimdocSection(
"API", "resession-api", render_vimdoc_api2("resession", funcs, types)
),
]
)
with open(VIMDOC, "w", encoding="utf-8") as ofile:
ofile.writelines(doc.render())
def update_md_api():
types = parse_directory(os.path.join(ROOT, "lua"))
funcs = types.files["resession/init.lua"].functions
lines = ["\n"] + render_md_api2(funcs, types)
replace_section(
README,
r"^<!-- API -->$",
r"^<!-- /API -->$",
lines,
)
def update_md_toc(filename: str):
toc = ["\n"] + generate_md_toc(filename) + ["\n"]
replace_section(
filename,
r"^<!-- TOC -->$",
r"^<!-- /TOC -->$",
toc,
)
def add_md_link_path(path: str, lines: List[str]) -> List[str]:
ret = []
for line in lines:
ret.append(re.sub(r"(\(#)", "(" + path + "#", line))
return ret
def main() -> None:
"""Update the README"""
update_config_options()
update_md_api()
update_md_toc(README)
generate_vimdoc()