forked from micheartin/docgen-yes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markup.go
238 lines (204 loc) · 6.45 KB
/
markup.go
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
package docgen
import (
"errors"
"fmt"
"sort"
"strings"
"github.com/go-chi/chi/v5"
)
// MarkupDoc describes a document to be generated.
type MarkupDoc struct {
Opts MarkupOpts
Router chi.Router
Doc Doc
Routes map[string]DocRouter // Pattern : DocRouter
FormattedHTML string
RouteHTML string
}
// MarkupOpts describes the options for a MarkupDoc.
type MarkupOpts struct {
// ProjectPath is the base Go import path of the project
ProjectPath string
// Intro text included at the top of the generated markdown file.
Intro string
// RouteText contains HTML generated from Route metadata
RouteText string
// ForceRelativeLinks to be relative even if they're not on github
ForceRelativeLinks bool
// URLMap allows specifying a map of package import paths to their link sources
// Used for mapping vendored dependencies to their upstream sources
// For example:
// map[string]string{"github.com/my/package/vendor/go-chi/chi/": "https://github.com/go-chi/chi/blob/master/"}
URLMap map[string]string
}
// MarkupRoutesDoc builds a document based on routes in a given router with given option set.
func MarkupRoutesDoc(r chi.Router, opts MarkupOpts) string {
// TODO: rewrite this class to have a single exported function(?)
// that returns a formatted HTML document based on a MarkupOpts
// 1) flatten router, build docs w/o recursion
// 2) Alternatively, build the JSON or Markdown doc and convert to HTML
mu := &MarkupDoc{
Opts: opts,
Router: r,
Doc: Doc{Router: DocRouter{
Middlewares: []DocMiddleware{},
Routes: map[string]DocRoute{},
}},
Routes: map[string]DocRouter{},
FormattedHTML: "",
RouteHTML: "",
}
if err := mu.generate(); err != nil {
return fmt.Sprintf("ERROR: %s\n", err.Error())
}
return mu.String()
}
// String pretty prints the document.
func (mu *MarkupDoc) String() string {
return mu.FormattedHTML
}
// Generate builds the document.
func (mu *MarkupDoc) generate() error {
if mu.Router == nil {
return errors.New("docgen: router is nil")
}
var err error
mu.Doc, err = BuildDoc(mu.Router)
if err != nil {
return err
}
mu.Routes = make(map[string]DocRouter)
mu.writeRoutes()
r := strings.NewReplacer(
"{title}", "go-chi Docgen",
"{css}", BassCSS(),
"{intro}", "Ding! Routes are Done!",
"{routes}", mu.RouteHTML,
"{favicon.ico}", FaviconIcoData(),
)
htmlString := r.Replace(BaseTemplate)
mu.FormattedHTML = htmlString
return nil
}
// writeRoutes generates the string for the Routes.
func (mu *MarkupDoc) writeRoutes() {
routesHeader := Head(2, "Routes")
mu.RouteHTML += routesHeader
// mu.Buf.WriteString(routesHeader)
// Build a route tree that consists of the full route pattern
// and the part of the tree for just that specific route, stored
// in routes map on the markdown struct. This is the structure we
// are going to render to markdown.
dr := mu.Doc.Router
ar := DocRouter{
Middlewares: []DocMiddleware{},
Routes: map[string]DocRoute{},
}
buildRoutesMap(mu, "", &ar, &ar, &dr)
routePaths := []string{}
for pat := range mu.Routes {
routePaths = append(routePaths, pat)
}
sort.Strings(routePaths)
for _, pat := range routePaths {
dr := mu.Routes[pat]
mu.RouteHTML += "<div>" + P(pat)
printRouter(mu, 0, dr)
mu.RouteHTML += "</div>"
}
}
// Generate the markup to render the above structure.
func printRouter(mu *MarkupDoc, depth int, dr DocRouter) {
// Middlewares
middleWares := make([]string, len(dr.Middlewares))
for j, mw := range dr.Middlewares {
middleWares[j] = ListItem(fmt.Sprintf("[%s](%s)", mw.Func, mu.githubSourceURL(mw.File, mw.Line)))
}
middleWaresList := UnorderedList(strings.Join(middleWares, ""))
mu.RouteHTML += Div(Head(3, "Middlewares") + middleWaresList)
// Routes
routeListItems := make([]string, len(dr.Routes))
ri := -1
for _, rt := range dr.Routes {
ri++
// RECURSION AAAAHHHHH NOOOOO
if rt.Router != nil {
printRouter(mu, depth+1, *rt.Router)
} else {
// Route Handler Methods
methods := make([]string, len(rt.Handlers))
mi := -1
for meth, dh := range rt.Handlers {
mi++
innerMiddles := make([]string, len(dh.Middlewares))
imi := -1
// Handler middlewares
for _, mw := range dh.Middlewares {
imi++
innerMiddles[imi] = ListItem(fmt.Sprintf("[%s](%s)", mw.Func, mu.githubSourceURL(mw.File, mw.Line)))
}
innerMiddlesList := UnorderedList(strings.Join(innerMiddles, ""))
handlerEndpoint := fmt.Sprintf("[%s](%s)", dh.Func, mu.githubSourceURL(dh.File, dh.Line))
methods[mi] = ListItem(meth + " " + handlerEndpoint + "<br />" + Div(innerMiddlesList))
}
methodList := UnorderedList(strings.Join(methods, ""))
routeListItems[ri] = ListItem(rt.Pattern + "<br />" + methodList)
}
}
routeList := UnorderedList(strings.Join(routeListItems, ""))
mu.RouteHTML += Head(3, "Middlewares") + Div(middleWaresList) + Head(3, "Routes") + Div(routeList)
}
func buildRoutesMap(mu *MarkupDoc, parentPattern string, ar, nr, dr *DocRouter) {
nr.Middlewares = append(nr.Middlewares, dr.Middlewares...)
for pat, rt := range dr.Routes {
pattern := parentPattern + pat
nr.Routes = DocRoutes{}
if rt.Router != nil {
nnr := &DocRouter{}
nr.Routes[pat] = DocRoute{
Pattern: pat,
Handlers: rt.Handlers,
Router: nnr,
}
buildRoutesMap(mu, pattern, ar, nnr, rt.Router)
} else if len(rt.Handlers) > 0 {
nr.Routes[pat] = DocRoute{
Pattern: pat,
Handlers: rt.Handlers,
Router: nil,
}
// Remove the trailing slash if the handler is a subroute for "/"
routeKey := pattern
if pat == "/" && len(routeKey) > 1 {
routeKey = routeKey[:len(routeKey)-1]
}
mu.Routes[routeKey] = copyDocRouter(*ar)
} else {
panic("not possible")
}
}
}
func (mu *MarkupDoc) githubSourceURL(file string, line int) string {
// Currently, we only automatically link to source for github projects
if strings.Index(file, "github.com/") != 0 && !mu.Opts.ForceRelativeLinks {
return ""
}
if mu.Opts.ProjectPath == "" {
return ""
}
for pkg, url := range mu.Opts.URLMap {
if idx := strings.Index(file, pkg); idx >= 0 {
pos := idx + len(pkg)
url = strings.TrimRight(url, "/")
filepath := strings.TrimLeft(file[pos:], "/")
return fmt.Sprintf("%s/%s#L%d", url, filepath, line)
}
}
if idx := strings.Index(file, mu.Opts.ProjectPath); idx >= 0 {
// relative
pos := idx + len(mu.Opts.ProjectPath)
return fmt.Sprintf("%s#L%d", file[pos:], line)
}
// absolute
return fmt.Sprintf("https://%s#L%d", file, line)
}