-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.go
More file actions
72 lines (61 loc) · 1.98 KB
/
markdown.go
File metadata and controls
72 lines (61 loc) · 1.98 KB
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
package devflow
import (
"errors"
)
type MarkDown struct {
rootDir string
destination string
// input sources (one of these should be set before calling Extract)
inputPath string
readFile func(name string) ([]byte, error)
writeFile func(name string, data []byte) error
log func(...any)
}
// NewMarkDown creates a new MarkDown instance with the root directory.
// Destination (output directory) and input must be set via methods.
func NewMarkDown(rootDir, destination string, writerFile func(name string, data []byte) error) *MarkDown {
return &MarkDown{
rootDir: rootDir,
destination: destination,
readFile: func(name string) ([]byte, error) { return nil, errors.New("not configure reader func") },
writeFile: writerFile,
log: func(...any) {},
}
}
// SetLog sets a custom logger function
func (m *MarkDown) SetLog(fn func(...any)) {
if fn != nil {
m.log = fn
}
}
// InputPath sets the input as a file path (relative to rootDir)
func (m *MarkDown) InputPath(pathFile string, readerFile func(name string) ([]byte, error)) *MarkDown {
m.inputPath = pathFile
m.readFile = readerFile
return m
}
// InputByte sets the input as a byte slice (markdown content)
func (m *MarkDown) InputByte(content []byte) *MarkDown {
// clear other inputs
m.readFile = func(name string) ([]byte, error) {
return content, nil
}
return m
}
// InputEmbed sets the input as any ReaderFile implementation and a relative path inside it
func (m *MarkDown) InputEmbed(path string, readerFile func(name string) ([]byte, error)) *MarkDown {
m.readFile = readerFile
// clear other inputs
m.inputPath = path
return m
}
// writeIfDifferent writes data to filename only if content is different
func (m *MarkDown) writeIfDifferent(filename, content string) error {
// Try to read existing file
existing, err := m.readFile(filename)
if err == nil && string(existing) == content {
return nil // Content is the same
}
// Need to write
return m.writeFile(filename, []byte(content))
}