-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaudeformatter.go
110 lines (98 loc) · 2.28 KB
/
claudeformatter.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
package main
import (
"errors"
"fmt"
"io"
"strings"
)
// Format writes the stsh struct as a markdown file to the provided writer
func Format(w io.Writer, s *stsh) error {
// Write header
if s.header != "" {
if _, err := fmt.Fprintf(w, "# %s\n\n", s.header); err != nil {
return err
}
} else {
return errors.New("empty header line")
}
// Write comment if exists
if s.comment != "" {
if _, err := fmt.Fprintf(w, "> %s\n\n", s.comment); err != nil {
return err
}
} else {
return errors.New("empty comment field")
}
if len(s.sols) == 0 {
return errors.New("no h2 found")
}
// Write each solution
for _, sol := range s.sols {
if sol.header != "" {
if _, err := fmt.Fprintf(w, "## %s\n\n", sol.header); err != nil {
return err
}
} else {
return errors.New("empty solution header line")
}
if len(sol.feats) == 0 {
return errors.New("no h3 found")
}
// Write features
for _, feat := range sol.feats {
if feat.header != "" {
if _, err := fmt.Fprintf(w, "### %s\n\n", feat.header); err != nil {
return err
}
} else {
return errors.New("empty feature header line")
}
if len(feat.cmds) == 0 {
return errors.New("no h4 found")
}
// Write commands
for _, cmd := range feat.cmds {
// Command header
if cmd.header != "" {
if _, err := fmt.Fprintf(w, "#### %s\n\n", cmd.header); err != nil {
return err
}
} else {
return errors.New("empty command header line")
}
// Command description
if cmd.desc != "" {
if _, err := fmt.Fprintf(w, "%s\n\n", cmd.desc); err != nil {
return err
}
} else {
return errors.New("empty command description")
}
// Command code block
if cmd.cmd != "" {
if _, err := fmt.Fprintf(w, "```sh\n%s\n```\n\n", cmd.cmd); err != nil {
return err
}
} else {
return errors.New("empty command code block")
}
// Command tags
if len(cmd.tags) > 0 {
if _, err := fmt.Fprintf(w, "Tags: %s\n\n", strings.Join(cmd.tags, ", ")); err != nil {
return err
}
}
}
}
}
return nil
}
// FormatToString returns the markdown as a string
func FormatToString(s *stsh) (string, error) {
var b strings.Builder
err := Format(&b, s)
if err != nil {
return "", err
}
return b.String(), nil
}