-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct_repository.go
204 lines (176 loc) · 5.01 KB
/
product_repository.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
package docweaver
import (
"bytes"
"fmt"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"os"
"os/exec"
"strings"
)
type Cleaner interface {
CleanTempVersions() error
}
type ProductRepository interface {
Cleaner
FindAllProducts() ([]Product, error)
FindProduct(productName string) (*Product, error)
GetDir() string
GetPage(productName, version, pagePath string) (*Page, error)
GetIndex(productName string) (*Page, error)
ListProductKeys() ([]string, error)
}
type productRepository struct {
dir string
}
const (
defaultPagePath = "installation"
pageExt = "md"
indexPath = "documentation"
)
func GetRepository(dir string) ProductRepository {
if dir == "" {
dir = getDocsDir()
}
return &productRepository{dir: dir}
}
func (pr *productRepository) GetDir() string {
return pr.dir
}
// FindAllProducts finds all products from the documentations' directory.
func (pr *productRepository) FindAllProducts() ([]Product, error) {
pn, err := pr.ListProductKeys()
if err != nil {
return nil, err
}
var products []Product
for _, n := range pn {
p, err := pr.FindProduct(n)
if err != nil {
return nil, err
}
products = append(products, *p)
}
return products, nil
}
// ListProductKeys lists keys of all available products.
func (pr *productRepository) ListProductKeys() ([]string, error) {
var productNames []string
cmd := exec.Command("ls")
cmd.Dir = pr.dir
out, err := cmd.Output()
if err != nil {
return productNames, simpleError{fmt.Sprintf("Failed to list all products in docs dir: ``. %s\n", err)}
}
for _, pn := range strings.Split(string(out), "\n") {
n := strings.TrimSpace(pn)
if n != "" {
productNames = append(productNames, n)
}
}
return productNames, nil
}
func (pr *productRepository) FindProduct(productKey string) (*Product, error) {
r := productRoot{ParentDir: pr.dir, Key: productKey}
var versions []string
entries, err := os.ReadDir(r.filePath())
if err != nil {
return nil, err
}
for _, f := range entries {
if f.IsDir() {
vt := f.Name()
if strings.Contains(vt, tempNameSuffix) {
continue
}
versions = append(versions, vt)
}
}
return pr.newProduct(r, versions), nil
}
func (pr *productRepository) GetPage(productKey, version, pagePath string) (*Page, error) {
if productKey == "" {
err := simpleError{err: "No product key provided."}
log(lError, err.Error())
return nil, err
}
if version == "" {
log(lInfo, "Using default version (%s) for product `%s`, page path: `%s`.\n", defaultVersion, productKey, pagePath)
version = defaultVersion
}
if pagePath == "" {
log(lInfo, "Using default page path (%s) for product `%s`, version: `%s`.\n", defaultPagePath, productKey, version)
pagePath = defaultPagePath
}
r := productRoot{ParentDir: pr.dir, Key: productKey}
p, err := pr.FindProduct(productKey)
if err != nil {
return nil, simpleError{fmt.Sprintf("Failed to init product for page. %s", err)}
}
filePath := fmt.Sprintf("%s%c%s.%s", r.versionFilePath(version), os.PathSeparator, pagePath, pageExt)
md, err := os.ReadFile(filePath)
if err != nil {
log(lWarn, "Failed to read product page from file path `%s`.\n", filePath)
return nil, err
}
var rawContent bytes.Buffer
err = goldmark.New(
goldmark.WithExtensions(extension.GFM, emoji.Emoji),
goldmark.WithParserOptions(parser.WithAutoHeadingID()),
goldmark.WithRendererOptions(html.WithHardWraps(), html.WithXHTML(), html.WithUnsafe()),
).Convert(md, &rawContent)
if err != nil {
return nil, err
}
content := replaceLinks(productKey, version, rawContent.String())
var index *Page = nil
if pagePath != indexPath {
index, err = pr.GetPage(r.Key, version, indexPath)
if err != nil {
log(lWarn, "Failed to read product index for page (%s) from path `%s`.\n", pagePath, indexPath)
index = nil
}
}
return &Page{
UrlPath: pagePath,
Title: getPageTitleFromHtml(content),
Content: content,
Product: p,
Version: version,
Index: index,
}, nil
}
func (pr *productRepository) GetIndex(productName string) (*Page, error) {
return pr.GetPage(productName, defaultVersion, defaultPagePath)
}
// CleanTempVersions removes all temporary documentation versions. Only returns the last error that occurred.
func (pr *productRepository) CleanTempVersions() (lastErr error) {
products, err := pr.FindAllProducts()
if err != nil {
lastErr = err
return
}
for _, p := range products {
for _, ver := range p.Versions {
lastErr = removeDir(p.root.versionFilePath(versionTempName(ver)))
}
}
return
}
func (pr *productRepository) newProduct(r productRoot, versions []string) (product *Product) {
latestV := latestVersion(versions)
product = &Product{
Name: cases.Title(language.English).String(r.Key),
BaseUrl: fmt.Sprintf("%s/%s", GetRoutePrefix(), r.Key),
LatestVersion: latestV,
Versions: versions,
root: r,
}
product.loadMeta()
return
}