Skip to content

Commit 2313ed1

Browse files
committed
json output from cli. closes #126
1 parent abdc73f commit 2313ed1

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

quiki.go

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"flag"
56
"fmt"
67
"io"
@@ -20,6 +21,7 @@ var (
2021
wizard bool
2122
wikiPath string
2223
forceGen bool
24+
jsonOutput bool
2325
opts webserver.Options
2426
)
2527

@@ -29,6 +31,7 @@ func main() {
2931
flag.BoolVar(&wizard, "w", false, "run setup wizard")
3032
flag.StringVar(&wikiPath, "wiki", "", "path to a wiki for wiki operations")
3133
flag.BoolVar(&forceGen, "force-gen", false, "regenerate pages even if unchanged")
34+
flag.BoolVar(&jsonOutput, "json", false, "output JSON instead of HTML")
3235
flag.StringVar(&opts.Bind, "bind", "", "address to bind to")
3336
flag.StringVar(&opts.Port, "port", "", "port to listen on")
3437
flag.StringVar(&opts.Host, "host", "", "default HTTP host")
@@ -115,13 +118,21 @@ func runPageAndExit(page *wikifier.Page) {
115118
if err != nil {
116119
log.Fatal(err)
117120
}
121+
if jsonOutput {
122+
json.NewEncoder(os.Stdout).Encode(page)
123+
os.Exit(0)
124+
}
118125
fmt.Println(page.HTML())
119126
os.Exit(0)
120127
}
121128

122129
// runs a wiki page
123130
func runWikiPageAndExit(w *wiki.Wiki, pagePath string) {
124131
res := w.DisplayPage(pagePath)
132+
if jsonOutput {
133+
json.NewEncoder(os.Stdout).Encode(res)
134+
os.Exit(0)
135+
}
125136
switch r := res.(type) {
126137
case wiki.DisplayPage:
127138
fmt.Println(r.Content)

wikifier/page-json.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package wikifier
2+
3+
import (
4+
"encoding/json"
5+
)
6+
7+
func (p *Page) MarshalJSON() ([]byte, error) {
8+
return json.Marshal(struct {
9+
Title string `json:"title"`
10+
HTML HTML `json:"html"`
11+
CSS string `json:"css"`
12+
PageInfo
13+
}{
14+
HTML: p.HTML(),
15+
CSS: p.CSS(),
16+
PageInfo: p.Info(),
17+
})
18+
}

0 commit comments

Comments
 (0)