Update output
This commit is contained in:
parent
a93f029d43
commit
8924b11aa8
@ -1,25 +1,90 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
zig "github.com/FooSoft/zero-epwing-go"
|
||||
)
|
||||
|
||||
type Entry struct {
|
||||
Heading string `json:"heading"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type Subbook struct {
|
||||
Title string `json:"title"`
|
||||
Copyright string `json:"copyrignt"`
|
||||
Entries []Entry `json:"entries"`
|
||||
}
|
||||
|
||||
type Book struct {
|
||||
DiscCode string `json:"discCode"`
|
||||
CharCode string `json:"charCode"`
|
||||
Subbooks []Subbook `json:"subbooks"`
|
||||
}
|
||||
|
||||
func outputBook(bookSrc *zig.Book, path string, pretty bool) error {
|
||||
bookDst := Book{DiscCode: bookSrc.DiscCode, CharCode: bookSrc.CharCode}
|
||||
for _, subbookSrc := range bookSrc.Subbooks {
|
||||
subbookDst := Subbook{Title: subbookSrc.Title, Copyright: subbookSrc.Copyright}
|
||||
for _, entrySrc := range subbookSrc.Entries {
|
||||
entryDst := Entry{entrySrc.Heading, entrySrc.Text}
|
||||
subbookDst.Entries = append(subbookDst.Entries, entryDst)
|
||||
}
|
||||
|
||||
bookDst.Subbooks = append(bookDst.Subbooks, subbookDst)
|
||||
}
|
||||
|
||||
var (
|
||||
data []byte
|
||||
err error
|
||||
)
|
||||
|
||||
if pretty {
|
||||
data, err = json.MarshalIndent(bookDst, "", "\t")
|
||||
} else {
|
||||
data, err = json.Marshal(bookDst)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(path, data, 0644)
|
||||
}
|
||||
|
||||
func main() {
|
||||
book, err := zig.Load(os.Args[1])
|
||||
var (
|
||||
entriesPath = flag.String("entries-path", "", "output path for dictionary entries")
|
||||
entriesPretty = flag.Bool("entries-pretty", false, "pretty-print dictionary entries")
|
||||
)
|
||||
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [options] path\n", path.Base(os.Args[0]))
|
||||
fmt.Fprintf(os.Stderr, "Parameters:\n")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
flag.Parse()
|
||||
|
||||
args := flag.Args()
|
||||
if len(args) != 1 {
|
||||
flag.Usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
book, err := zig.Load(args[0])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fp, err := os.Create(os.Args[2])
|
||||
defer fp.Close()
|
||||
|
||||
for _, subbook := range book.Subbooks {
|
||||
for _, entry := range subbook.Entries {
|
||||
fp.WriteString(entry.Heading)
|
||||
fp.WriteString("\n")
|
||||
}
|
||||
if err := outputBook(book, *entriesPath, *entriesPretty); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user