-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverter.go
232 lines (185 loc) · 4.92 KB
/
converter.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
// SPDX-FileCopyrightText: 2019 Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package ciigo
import (
"fmt"
"html/template"
"log"
"os"
"path/filepath"
"time"
"git.sr.ht/~shulhan/asciidoctor-go"
"github.com/yuin/goldmark"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
)
// Converter a single, reusable AsciiDoc converter.
type Converter struct {
tmpl *template.Template
tmplSearch *template.Template
// htmlTemplateModtime modification time for HTMLTemplate.
htmlTemplateModtime time.Time
htmlTemplate string // Path to HTML template in storage.
}
// NewConverter create and initialize Converter with HTML template.
// If htmlTemplate is empty, it will use the internal, predefined template.
func NewConverter(htmlTemplate string) (converter *Converter, err error) {
var (
logp = `NewConverter`
tmplContent string
)
converter = &Converter{}
converter.tmpl = template.New(``)
if len(htmlTemplate) == 0 {
tmplContent = templateIndexHTML
} else {
converter.htmlTemplate = filepath.Clean(htmlTemplate)
var fi os.FileInfo
fi, err = os.Stat(converter.htmlTemplate)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
converter.htmlTemplateModtime = fi.ModTime()
var bhtml []byte
bhtml, err = os.ReadFile(converter.htmlTemplate)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
tmplContent = string(bhtml)
}
converter.tmpl, err = converter.tmpl.Parse(tmplContent)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
converter.tmplSearch = template.New(`search`)
converter.tmplSearch, err = converter.tmplSearch.Parse(templateSearch)
if err != nil {
return nil, fmt.Errorf(`%s: %s: %w`, logp, templateSearch, err)
}
return converter, nil
}
// convertFileMarkups convert markup files into HTML.
func (converter *Converter) convertFileMarkups(fileMarkups map[string]*FileMarkup, isForce bool) {
var (
logp = `convertFileMarkups`
fmarkup *FileMarkup
htmlInfo os.FileInfo
htmlModtime time.Time
err error
skip bool
)
for _, fmarkup = range fileMarkups {
skip = true
if !isForce {
htmlInfo, _ = os.Stat(fmarkup.pathHTML)
if htmlInfo == nil {
// HTML file may not exist.
skip = false
} else {
htmlModtime = htmlInfo.ModTime()
if converter.htmlTemplateModtime.After(htmlModtime) {
skip = false
} else if fmarkup.info.ModTime().After(htmlModtime) {
skip = false
}
}
if skip {
continue
}
}
err = converter.ToHTMLFile(fmarkup)
if err != nil {
log.Printf(`%s: %s`, logp, err)
} else {
log.Printf(`%s: converting %s`, logp, fmarkup.path)
}
}
}
func (converter *Converter) htmlTemplateUseInternal() (err error) {
converter.tmpl, err = converter.tmpl.Parse(templateIndexHTML)
if err != nil {
return err
}
return nil
}
// SetHTMLTemplateFile set the HTML template from file.
func (converter *Converter) SetHTMLTemplateFile(pathHTMLTemplate string) (err error) {
converter.tmpl, err = template.ParseFiles(pathHTMLTemplate)
if err != nil {
return err
}
converter.htmlTemplate = pathHTMLTemplate
return nil
}
// ToHTMLFile convert the AsciiDoc file to HTML.
func (converter *Converter) ToHTMLFile(fmarkup *FileMarkup) (err error) {
var (
logp = `ToHTMLFile`
fhtml *fileHTML
f *os.File
)
switch fmarkup.kind {
case markupKindAdoc:
fhtml, err = converter.adocToHTML(fmarkup)
case markupKindMarkdown:
fhtml, err = converter.markdownToHTML(fmarkup)
}
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
fhtml.Body = template.HTML(fhtml.rawBody.String())
f, err = os.Create(fmarkup.pathHTML)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
err = converter.tmpl.Execute(f, fhtml)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
err = f.Close()
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
return nil
}
func (converter *Converter) adocToHTML(fmarkup *FileMarkup) (fhtml *fileHTML, err error) {
var (
logp = `adocToHTML`
doc *asciidoctor.Document
)
doc, err = asciidoctor.Open(fmarkup.path)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
fhtml = newFileHTML()
err = doc.ToHTMLBody(&fhtml.rawBody)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
fhtml.unpackAdocMetadata(doc)
return fhtml, nil
}
func (converter *Converter) markdownToHTML(fmarkup *FileMarkup) (fhtml *fileHTML, err error) {
var (
logp = `markdownToHTML`
mdg = goldmark.New(
goldmark.WithExtensions(
meta.Meta,
),
)
in []byte
parserCtx parser.Context
)
in, err = os.ReadFile(fmarkup.path)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
fhtml = newFileHTML()
parserCtx = parser.NewContext()
err = mdg.Convert(in, &fhtml.rawBody, parser.WithContext(parserCtx))
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
fhtml.unpackMarkdownMetadata(meta.Get(parserCtx))
return fhtml, nil
}