-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
295 lines (271 loc) · 7.98 KB
/
Copy pathapp.go
File metadata and controls
295 lines (271 loc) · 7.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"context"
"embed"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
"github.com/phpdave11/gofpdf"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
//go:embed report-templates/*.json
var bundledTemplates embed.FS
var nonSlug = regexp.MustCompile(`[^a-z0-9]+`)
type App struct {
ctx context.Context
templateDir string
mu sync.Mutex
}
func NewApp() *App {
return &App{templateDir: filepath.Join(appDir(), "report-templates")}
}
func (a *App) startup(ctx context.Context) { a.ctx = ctx }
func appDir() string {
executable, err := os.Executable()
if err != nil {
return "."
}
return filepath.Dir(executable)
}
func slugify(name string) string {
slug := strings.Trim(nonSlug.ReplaceAllString(strings.ToLower(name), "-"), "-")
if slug == "" {
return "template"
}
return slug
}
func templateName(template map[string]any) string {
name, _ := template["name"].(string)
name = strings.TrimSpace(name)
if name == "" {
return "Untitled Template"
}
return name
}
func templateSections(template map[string]any) []any {
sections, _ := template["sections"].([]any)
return sections
}
func normalizeTemplates(templates map[string]any) map[string]map[string]any {
ids := make([]string, 0, len(templates))
for id := range templates {
ids = append(ids, id)
}
sort.Strings(ids)
used := map[string]bool{}
normalized := make(map[string]map[string]any, len(templates))
for _, id := range ids {
template, ok := templates[id].(map[string]any)
if !ok || template == nil {
continue
}
name := templateName(template)
filename := slugify(name) + ".json"
for index := 2; used[filename]; index++ {
filename = fmt.Sprintf("%s-%d.json", slugify(name), index)
}
used[filename] = true
normalized[id] = map[string]any{
"name": name,
"narrative": stringValue(template["narrative"]),
"sections": templateSections(template),
"fileName": filename,
}
}
return normalized
}
func stringValue(value any) string {
text, _ := value.(string)
return text
}
func (a *App) ensureTemplateDir() error {
if err := os.MkdirAll(a.templateDir, 0o755); err != nil {
return err
}
entries, err := os.ReadDir(a.templateDir)
if err != nil {
return err
}
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), ".json") {
return nil
}
}
seedFiles, err := bundledTemplates.ReadDir("report-templates")
if err != nil {
return err
}
for _, entry := range seedFiles {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".json") {
continue
}
contents, err := bundledTemplates.ReadFile("report-templates/" + entry.Name())
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(a.templateDir, entry.Name()), contents, 0o644); err != nil {
return err
}
}
return nil
}
func writeJSONAtomic(path string, value any) error {
contents, err := json.MarshalIndent(value, "", " ")
if err != nil {
return err
}
temp := path + ".tmp"
if err := os.WriteFile(temp, contents, 0o644); err != nil {
return err
}
return os.Rename(temp, path)
}
func state(templates map[string]map[string]any, activeID any, warnings []string, templateDir string) map[string]any {
active, _ := activeID.(string)
if _, ok := templates[active]; !ok {
active = ""
for id := range templates {
active = id
break
}
}
return map[string]any{"templates": templates, "activeId": active, "templateDir": templateDir, "warnings": warnings}
}
func (a *App) GetState() (map[string]any, error) {
a.mu.Lock()
defer a.mu.Unlock()
if err := a.ensureTemplateDir(); err != nil {
return nil, err
}
templates := map[string]any{}
warnings := []string{}
entries, err := os.ReadDir(a.templateDir)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".json") {
continue
}
path := filepath.Join(a.templateDir, entry.Name())
contents, err := os.ReadFile(path)
if err != nil {
warnings = append(warnings, entry.Name()+": unreadable")
continue
}
var template map[string]any
if err := json.Unmarshal(contents, &template); err != nil {
warnings = append(warnings, entry.Name()+": invalid JSON")
continue
}
id := stringValue(template["id"])
if id == "" {
if _, hasName := template["name"].(string); hasName {
id = strings.TrimSuffix(entry.Name(), ".json")
template["id"] = id
if err := writeJSONAtomic(path, template); err != nil {
return nil, err
}
} else {
warnings = append(warnings, entry.Name()+": missing required template fields")
continue
}
}
if _, duplicate := templates[id]; duplicate {
warnings = append(warnings, entry.Name()+`: duplicate template id "`+id+`" ignored`)
continue
}
template["fileName"] = entry.Name()
templates[id] = template
}
return state(normalizeTemplates(templates), nil, warnings, filepath.Base(a.templateDir)), nil
}
func (a *App) SaveState(payload map[string]any) (map[string]any, error) {
a.mu.Lock()
defer a.mu.Unlock()
if err := a.ensureTemplateDir(); err != nil {
return nil, err
}
templates, ok := payload["templates"].(map[string]any)
if !ok {
return nil, fmt.Errorf("templates payload must be an object")
}
normalized := normalizeTemplates(templates)
for id, template := range normalized {
body := map[string]any{"app": "template-workspace", "version": 1, "id": id, "name": template["name"], "narrative": template["narrative"], "sections": template["sections"]}
if err := writeJSONAtomic(filepath.Join(a.templateDir, stringValue(template["fileName"])), body); err != nil {
return nil, err
}
}
entries, err := os.ReadDir(a.templateDir)
if err != nil {
return nil, err
}
keep := map[string]bool{}
for _, template := range normalized {
keep[stringValue(template["fileName"])] = true
}
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".json") && !keep[entry.Name()] {
if err := os.Remove(filepath.Join(a.templateDir, entry.Name())); err != nil {
return nil, err
}
}
}
return state(normalized, payload["activeId"], nil, filepath.Base(a.templateDir)), nil
}
func (a *App) ExportPDF(payload map[string]any) (map[string]any, error) {
path, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{DefaultFilename: slugify(stringValue(payload["name"])) + ".pdf", Filters: []runtime.FileFilter{{DisplayName: "PDF files", Pattern: "*.pdf"}}})
if err != nil || path == "" {
return map[string]any{"cancelled": true}, nil
}
if err := buildPDF(path, stringValue(payload["name"]), stringValue(payload["markdown"])); err != nil {
return nil, err
}
return map[string]any{"savedPath": path}, nil
}
func buildPDF(path, title, markdown string) error {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.SetMargins(17, 17, 17)
pdf.SetAutoPageBreak(true, 17)
pdf.AddPage()
pdf.SetFont("Helvetica", "B", 20)
pdf.MultiCell(176, 9, fallback(title, "Template Workspace Report"), "", "L", false)
pdf.SetFont("Helvetica", "", 8)
pdf.SetTextColor(123, 116, 109)
pdf.CellFormat(176, 6, "Generated from Template Workspace", "", 1, "L", false, 0, "")
pdf.SetTextColor(43, 41, 39)
for _, block := range strings.Split(strings.ReplaceAll(markdown, "\r\n", "\n"), "\n\n") {
block = strings.TrimSpace(block)
if block == "" || block == "---" {
continue
}
size, style := float64(11), ""
switch {
case strings.HasPrefix(block, "### "):
size, style, block = 13, "B", strings.TrimPrefix(block, "### ")
case strings.HasPrefix(block, "## "):
size, style, block = 16, "B", strings.TrimPrefix(block, "## ")
case strings.HasPrefix(block, "# "):
size, style, block = 20, "B", strings.TrimPrefix(block, "# ")
case strings.HasPrefix(block, "> "):
block = strings.ReplaceAll(block, "\n> ", "\n")
block = strings.TrimPrefix(block, "> ")
}
pdf.SetFont("Helvetica", style, size)
pdf.MultiCell(176, size*0.46, strings.ReplaceAll(block, "**", ""), "", "L", false)
pdf.Ln(2)
}
return pdf.OutputFileAndClose(path)
}
func fallback(value, defaultValue string) string {
if value == "" {
return defaultValue
}
return value
}