-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.go
More file actions
74 lines (56 loc) · 1.84 KB
/
Copy pathdocs.go
File metadata and controls
74 lines (56 loc) · 1.84 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
package cmd
import (
"os"
"path/filepath"
_ "embed"
"github.com/Bananenpro/cli"
"github.com/code-game-project/go-utils/cggenevents"
"github.com/code-game-project/go-utils/exec"
"github.com/code-game-project/go-utils/server"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/spf13/cobra"
)
//go:embed templates/css/docs.css
var docsStyle string
var docsCmd = &cobra.Command{
Use: "docs",
Short: "View the documention of CodeGame or a specific game in your webbrowser.",
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cli.Print("Opening documentation...")
err := exec.OpenBrowser("https://docs.code-game.org")
abort(err)
return
}
api, err := server.NewAPI(args[0])
abort(err)
url := api.BaseURL()
cge, err := api.GetCGEFile()
abort(err)
cgeVersion, err := cggenevents.ParseCGEVersion(cge)
abort(err)
err = cggenevents.CGGenEvents(cgeVersion, os.TempDir(), url, "markdown")
abort(err)
md, err := os.ReadFile(filepath.Join(os.TempDir(), "event_docs.md"))
abort(err)
md = markdown.NormalizeNewlines(md)
text := markdown.ToHTML(md, parser.NewWithExtensions(parser.CommonExtensions|parser.AutoHeadingIDs), html.NewRenderer(html.RendererOptions{
CSS: filepath.Join(os.TempDir(), "event_docs.css"),
Flags: html.CommonFlags | html.CompletePage,
}))
os.Remove(filepath.Join(os.TempDir(), "event_docs.md"))
err = os.WriteFile(filepath.Join(os.TempDir(), "event_docs.html"), text, 0o644)
abort(err)
err = os.WriteFile(filepath.Join(os.TempDir(), "event_docs.css"), []byte(docsStyle), 0o644)
abort(err)
cli.Print("Opening documentation...")
err = exec.OpenBrowser(filepath.Join(os.TempDir(), "event_docs.html"))
abort(err)
},
}
func init() {
rootCmd.AddCommand(docsCmd)
}