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

mlange-42 / modo / 13092654165

01 Feb 2025 09:34PM CUT coverage: 73.324%. Remained the same
13092654165

push

github

web-flow
Tweak presentation slides (#181)

1509 of 2058 relevant lines covered (73.32%)

24.78 hits per line

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

77.55
/format/hugo.go
1
package format
2

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

11
        "github.com/mlange-42/modo/document"
12
        "gopkg.in/ini.v1"
13
)
14

15
const landingPageContentHugo = `---
16
title: Landing page
17
type: docs
18
---
19

20
JSON created by mojo doc should be placed next to this file.
21

22
Additional documentation files go here, too.
23
They will be processed for doc-tests and copied to folder 'site/content'.
24
`
25

26
type Hugo struct{}
27

28
type hugoConfig struct {
29
        Title  string
30
        Repo   string
31
        Module string
32
        Pages  string
33
}
34

35
func (f *Hugo) Accepts(files []string) error {
×
36
        return nil
×
37
}
×
38

39
func (f *Hugo) ProcessMarkdown(element any, text string, proc *document.Processor) (string, error) {
1✔
40
        b := strings.Builder{}
1✔
41
        err := proc.Template.ExecuteTemplate(&b, "hugo_front_matter.md", element)
1✔
42
        if err != nil {
1✔
43
                return "", err
×
44
        }
×
45
        b.WriteRune('\n')
1✔
46
        b.WriteString(text)
1✔
47
        return b.String(), nil
1✔
48
}
49

50
func (f *Hugo) WriteAuxiliary(p *document.Package, dir string, proc *document.Processor) error {
×
51
        return nil
×
52
}
×
53

54
func (f *Hugo) ToFilePath(p string, kind string) string {
6✔
55
        if kind == "package" || kind == "module" {
10✔
56
                return path.Join(p, "_index.md")
4✔
57
        }
4✔
58
        return p + ".md"
2✔
59
}
60

61
func (f *Hugo) ToLinkPath(p string, kind string) string {
3✔
62
        p = f.ToFilePath(p, kind)
3✔
63
        return fmt.Sprintf("{{< ref \"%s\" >}}", p)
3✔
64
}
3✔
65

66
func (f *Hugo) Input(in string, sources []document.PackageSource) string {
1✔
67
        return in
1✔
68
}
1✔
69

70
func (f *Hugo) Output(out string) string {
2✔
71
        return path.Join(out, "content")
2✔
72
}
2✔
73

74
func (f *Hugo) GitIgnore(in, out string, sources []document.PackageSource) []string {
1✔
75
        return []string{
1✔
76
                "# files generated by 'mojo doc'",
1✔
77
                fmt.Sprintf("/%s/*.json", in),
1✔
78
                "# files generated by Modo",
1✔
79
                fmt.Sprintf("/%s/%s/", out, "content"),
1✔
80
                "# files generated by Hugo",
1✔
81
                fmt.Sprintf("/%s/%s/", out, "public"),
1✔
82
                fmt.Sprintf("/%s/*.lock", out),
1✔
83
                "# test file generated by Modo",
1✔
84
                "/test/",
1✔
85
        }
1✔
86
}
1✔
87

88
func (f *Hugo) CreateDirs(base, in, out string, sources []document.PackageSource, templ *template.Template) error {
1✔
89
        inDir, outDir := path.Join(base, in), path.Join(base, f.Output(out))
1✔
90
        testDir := path.Join(base, "test")
1✔
91
        if err := mkDirs(inDir); err != nil {
1✔
92
                return err
×
93
        }
×
94
        if err := os.WriteFile(path.Join(inDir, "_index.md"), []byte(landingPageContentHugo), 0644); err != nil {
1✔
95
                return err
×
96
        }
×
97
        if err := mkDirs(outDir); err != nil {
1✔
98
                return err
×
99
        }
×
100
        if err := mkDirs(testDir); err != nil {
1✔
101
                return err
×
102
        }
×
103
        return f.createInitialFiles(base, path.Join(base, out), templ)
1✔
104
}
105

106
func (f *Hugo) createInitialFiles(docDir, hugoDir string, templ *template.Template) error {
1✔
107
        config, err := getGitOrigin(docDir)
1✔
108
        if err != nil {
1✔
109
                return err
×
110
        }
×
111

112
        files := [][]string{
1✔
113
                {"hugo.yaml", "hugo.yaml"},
1✔
114
                {"hugo.mod", "go.mod"},
1✔
115
                {"hugo.sum", "go.sum"},
1✔
116
        }
1✔
117
        for _, f := range files {
4✔
118
                outFile := path.Join(hugoDir, f[1])
3✔
119
                exists, _, err := fileExists(outFile)
3✔
120
                if err != nil {
3✔
121
                        return err
×
122
                }
×
123
                if exists {
3✔
124
                        fmt.Printf("WARNING: Hugo file %s already exists, skip creating\n", outFile)
×
125
                        return nil
×
126
                }
×
127
        }
128

129
        for _, f := range files {
4✔
130
                outFile := path.Join(hugoDir, f[1])
3✔
131
                b := bytes.Buffer{}
3✔
132
                if err := templ.ExecuteTemplate(&b, f[0], &config); err != nil {
3✔
133
                        return err
×
134
                }
×
135
                if err := os.WriteFile(outFile, b.Bytes(), 0644); err != nil {
3✔
136
                        return err
×
137
                }
×
138
        }
139
        return nil
1✔
140
}
141

142
func (f *Hugo) Clean(out, tests string) error {
1✔
143
        if err := emptyDir(out); err != nil {
1✔
144
                return err
×
145
        }
×
146
        return emptyDir(tests)
1✔
147
}
148

149
func getGitOrigin(outDir string) (*hugoConfig, error) {
2✔
150
        gitFiles := []string{
2✔
151
                ".git/config",
2✔
152
                "../.git/config",
2✔
153
        }
2✔
154

2✔
155
        var content *ini.File
2✔
156
        found := false
2✔
157
        for _, f := range gitFiles {
6✔
158
                exists, isDir, err := fileExists(f)
4✔
159
                if err != nil {
4✔
160
                        return nil, err
×
161
                }
×
162
                if !exists || isDir {
7✔
163
                        continue
3✔
164
                }
165
                content, err = ini.Load(f)
1✔
166
                if err != nil {
1✔
167
                        return nil, err
×
168
                }
×
169
                found = true
1✔
170
                break
1✔
171
        }
172

173
        url := "https://github.com/your/package"
2✔
174
        ok := false
2✔
175
        if found {
3✔
176
                section := content.Section(`remote "origin"`)
1✔
177
                if section != nil {
2✔
178
                        value := section.Key("url")
1✔
179
                        if value != nil {
2✔
180
                                url = strings.TrimSuffix(value.String(), ".git")
1✔
181
                                ok = true
1✔
182
                        }
1✔
183
                }
184
        }
185
        if !ok {
3✔
186
                fmt.Printf("WARNING: No Git repository or no remote 'origin' found.\n         Using dummy %s\n", url)
1✔
187
        }
1✔
188
        title, pages := repoToTitleAndPages(url)
2✔
189
        module := strings.ReplaceAll(strings.ReplaceAll(url, "https://", ""), "http://", "")
2✔
190
        module = fmt.Sprintf("%s/%s", module, outDir)
2✔
191

2✔
192
        return &hugoConfig{
2✔
193
                Title:  title,
2✔
194
                Repo:   url,
2✔
195
                Pages:  pages,
2✔
196
                Module: module,
2✔
197
        }, nil
2✔
198
}
199

200
func repoToTitleAndPages(repo string) (string, string) {
4✔
201
        if !strings.HasPrefix(repo, "https://github.com/") {
5✔
202
                parts := strings.Split(repo, "/")
1✔
203
                title := parts[len(parts)-1]
1✔
204
                return title, fmt.Sprintf("https://%s.com", title)
1✔
205
        }
1✔
206
        repo = strings.TrimPrefix(repo, "https://github.com/")
3✔
207
        parts := strings.Split(repo, "/")
3✔
208
        return parts[1], fmt.Sprintf("https://%s.github.io/%s/", parts[0], parts[1])
3✔
209
}
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