• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / modo / 12877420203

20 Jan 2025 11:55PM UTC coverage: 60.111%. Remained the same
12877420203

Pull #86

github

web-flow
Merge 8163123e3 into 97ea5dccf
Pull Request #86: Improve site navigation, unfold user guide

972 of 1617 relevant lines covered (60.11%)

28.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.92
/document/processor.go
1
package document
2

3
import (
4
        "fmt"
5
        "log"
6
        "os"
7
        "strings"
8
        "text/template"
9
)
10

11
type Config struct {
12
        InputFile       string
13
        OutputDir       string
14
        DocTests        string
15
        TemplateDirs    []string
16
        UseExports      bool
17
        ShortLinks      bool
18
        CaseInsensitive bool
19
        Strict          bool
20
        DryRun          bool
21
}
22

23
type Processor struct {
24
        Config             *Config
25
        Template           *template.Template
26
        Formatter          Formatter
27
        Docs               *Docs
28
        ExportDocs         *Docs
29
        allPaths           map[string]bool
30
        linkTargets        map[string]elemPath
31
        linkExports        map[string]string
32
        linkExportsReverse map[string]*exportError
33
        docTests           []*docTest
34
        writer             func(file, text string) error
35
}
36

37
type exportError struct {
38
        NewPath  string
39
        OldPaths []string
40
}
41

42
type docTest struct {
43
        Name   string
44
        Path   []string
45
        Code   []string
46
        Global []string
47
}
48

49
func NewProcessor(docs *Docs, f Formatter, t *template.Template, config *Config) *Processor {
10✔
50
        return NewProcessorWithWriter(docs, f, t, config, func(file, text string) error {
19✔
51
                return os.WriteFile(file, []byte(text), 0666)
9✔
52
        })
9✔
53
}
54

55
func NewProcessorWithWriter(docs *Docs, f Formatter, t *template.Template, config *Config, writer func(file, text string) error) *Processor {
15✔
56
        return &Processor{
15✔
57
                Config:    config,
15✔
58
                Template:  t,
15✔
59
                Formatter: f,
15✔
60
                Docs:      docs,
15✔
61
                writer:    writer,
15✔
62
        }
15✔
63
}
15✔
64

65
// PrepareDocs processes the API docs for subsequent rendering.
66
func (proc *Processor) PrepareDocs() error {
6✔
67
        // Collect the paths of all (sub)-elements in the original structure.
6✔
68
        proc.collectElementPaths()
6✔
69

6✔
70
        // Extract doc tests.
6✔
71
        err := proc.extractDocTests()
6✔
72
        if err != nil {
6✔
73
                return err
×
74
        }
×
75
        err = proc.writeDocTests(proc.Config.DocTests)
6✔
76
        if err != nil {
6✔
77
                return err
×
78
        }
×
79

80
        // Re-structure according to exports.
81
        err = proc.filterPackages()
6✔
82
        if err != nil {
6✔
83
                return err
×
84
        }
×
85
        // Collect all link target paths.
86
        proc.collectPaths()
6✔
87
        if !proc.Config.UseExports {
9✔
88
                for k := range proc.linkTargets {
16✔
89
                        proc.linkExports[k] = k
13✔
90
                }
13✔
91
        }
92
        // Replaces cross-refs by placeholders.
93
        if err := proc.processLinks(proc.Docs); err != nil {
6✔
94
                return err
×
95
        }
×
96
        return nil
6✔
97
}
98

99
func (proc *Processor) WriteFile(file, text string) error {
33✔
100
        return proc.writer(file, text)
33✔
101
}
33✔
102

103
func (proc *Processor) warnOrError(pattern string, args ...any) error {
×
104
        if proc.Config.Strict {
×
105
                return fmt.Errorf(pattern, args...)
×
106
        }
×
107
        log.Printf("WARNING: "+pattern+"\n", args...)
×
108
        return nil
×
109
}
110

111
func (proc *Processor) addLinkExport(oldPath, newPath []string) {
70✔
112
        pNew := strings.Join(newPath, ".")
70✔
113
        pOld := strings.Join(oldPath, ".")
70✔
114
        if present, ok := proc.linkExportsReverse[pNew]; ok {
70✔
115
                present.OldPaths = append(present.OldPaths, pOld)
×
116
        } else {
70✔
117
                proc.linkExportsReverse[pNew] = &exportError{
70✔
118
                        NewPath:  pNew,
70✔
119
                        OldPaths: []string{pOld},
70✔
120
                }
70✔
121
        }
70✔
122
        proc.linkExports[pOld] = pNew
70✔
123
}
124

125
func (proc *Processor) addLinkTarget(elPath, filePath []string, kind string, isSection bool) {
76✔
126
        proc.linkTargets[strings.Join(elPath, ".")] = elemPath{Elements: filePath, Kind: kind, IsSection: isSection}
76✔
127
}
76✔
128

129
func (proc *Processor) addElementPath(elPath, filePath []string, kind string, isSection bool) {
90✔
130
        if isSection && kind != "package" && kind != "module" { // actually, we are catching aliases here
117✔
131
                return
27✔
132
        }
27✔
133
        proc.allPaths[strings.Join(elPath, ".")] = true
63✔
134
        _, _ = filePath, kind
63✔
135
}
136

137
func (proc *Processor) mkDirs(path string) error {
16✔
138
        if proc.Config.DryRun {
20✔
139
                return nil
4✔
140
        }
4✔
141
        if err := os.MkdirAll(path, os.ModePerm); err != nil && !os.IsExist(err) {
12✔
142
                return err
×
143
        }
×
144
        return nil
12✔
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc