forked from go-rel/doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
81 lines (70 loc) · 2.4 KB
/
main.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
import os
import textwrap
import requests
import subprocess
import re
from datetime import datetime
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 embed_code(filename, fragment=None, prefix=''):
"""
Embed code, optionally specified by fragment name.
"""
full_filename = os.path.join(env.project_dir, filename)
extension = filename.rsplit('.', 1)[-1]
with open(full_filename, 'r') as f:
lines = f.readlines()
str = None
if fragment != None:
i = 0
found = None
for line in lines:
if line.strip() == '/// ['+fragment+']':
if found == None:
found = i+1
else:
str = ''.join(lines[found:i])
str = textwrap.dedent(str)
break
i += 1
else:
str = ''.join(lines)
return textwrap.indent('```'+extension+'\n'+str+'\n```', prefix)[len(prefix):]
@env.macro
def changelog():
"""
Generate Changelog.
"""
result = ""
url = "https://api.github.com/repos/go-rel/rel/releases"
releases = requests.get(url).json()
for release in releases:
body = ""
if release["author"]["login"] == "github-actions[bot]":
pattern = re.compile(r"^\w{7}\s.+\(\#\d+\)$")
lines = release["body"].splitlines()
for line in lines:
line = line.lstrip("* ")
if pattern.match(line):
body += "\n- " + line[7:]
else:
body = release["body"]
time = datetime.strptime(
release["created_at"], '%Y-%m-%dT%H:%M:%SZ')
result += "\n## **" + \
release["name"] + "** - " + \
time.strftime("%B %-d, %Y") + "\n\n" + body
return result
@env.macro
def godoc(pkg):
"""
Generate Godoc
"""
result = subprocess.run(
['godoc2md', '-template=docs/reference/godoc.tpl', pkg], stdout=subprocess.PIPE)
return result.stdout.decode("utf-8")