sundays/main.go

277 lines
5.5 KiB
Go

package main
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"os"
)
type node struct {
Type string `json:"type"`
Version string `json:"version,omitempty"`
Caller string `json:"caller,omitempty"`
Content json.RawMessage `json:"content"`
Code string `json:"code,omitempty"`
Marker string `json:"marker,omitempty"`
Number string `json:"number,omitempty"`
}
type chapter struct {
Number int
Verses []*verse
Titles *textGroup
}
type verse struct {
Number int
TextGroup *textGroup
}
type textGroup struct {
}
type Document struct {
USX *USX `xml:"usx"`
Book *Book `xml:"book"`
Meta []*Para `xml:"para"`
}
type USX struct {
Version string `xml:"version,attr"`
}
type Book struct {
Style string `xml:"style,attr"`
Code string `xml:"code,attr"`
Content string `xml:"chardata"`
}
type Chapter struct {
Style string `xml:"style,attr"`
Number int `xml:"number,attr"`
}
type Para struct {
Style string `xml:"style,attr"`
}
type usxMeta struct {
Version string
H string
TOC1 string
TOC2 string
TOC3 string
MT1 string
MT2 string
}
type usxDoc struct {
Book *usxBook
Meta *usxMeta
}
type usxBook struct {
Style string `xml:"style,attr"`
Code string `xml:"code,attr"`
Info string `xml:"chardata"`
Chapters []*usxChapter `xml:"-"`
}
type usxChapter struct {
XMLName xml.Name `xml:"chapter"`
Number int `xml:"number,attr"`
Style string `xml:"style,attr"`
Para []*usxPara `xml:"-"`
}
type usxPara struct {
XMLName xml.Name `xml:"para"`
Style string `xml:"style,attr"`
Content string `xml:",chardata"`
Refs []*usxRef `xml:"ref"`
Verses []*usxVerse `xml:"verse"`
}
type usxRef struct {
XMLName xml.Name `xml:"ref"`
Loc string `xml:"loc,attr"`
Content string `xml:",chardata"`
}
type usxVerse struct {
XMLName xml.Name `xml:"verse"`
Style string `xml:"style,attr"`
Number int `xml:"number,attr"`
Content string `xml:",chardata"`
Notes []*usxNote `xml:"note"`
}
type usxNote struct {
Style string `xml:"style,attr"`
Caller string `xml:"caller,attr"`
Chars []*usxChar `xml:"char"`
}
type usxChar struct {
Style string `xml:"style,attr"`
Content string `xml:",chardata"`
}
// Convert USX to S2F (Structured Scripture Format).
func usxToS2f() {
}
type sm struct {
version string
// Are we actually processing the XML document? Requires valid processing instructions (<?xml...).
inDoc bool
// Are we processing the book metadata? Until we're actually inside a chapter, this is true.
inMeta bool
// Are we processing book chapters? Once this is true, we start collecting book data and increment chapters accordingly.
chapter *usxChapter
}
func main() {
/*fp, err := os.Open("downloads/bsb_usj/GAL.usj")
if err != nil {
fmt.Errorf("%s", err)
}
_ = fp*/
fp, err := os.Open("downloads/bsb_usx/GAL.usx")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dec := xml.NewDecoder(fp)
tk, err := dec.Token()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if _, ok := tk.(xml.ProcInst); ok {
// Skip
}
state := &sm{}
doc := &usxDoc{
Book: &usxBook{
Chapters: make([]*usxChapter, 0),
},
Meta: &usxMeta{},
}
for {
tk, err = dec.Token()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
fmt.Println(err)
os.Exit(1)
}
REPROC:
switch t := tk.(type) {
default:
fmt.Printf("unhandled type: %T\n", t)
case xml.StartElement:
if !state.inMeta && !state.inDoc {
if t.Name.Local == "usx" {
for _, attr := range t.Attr {
if attr.Name.Local == "version" {
doc.Meta.Version = attr.Value
}
}
state.inDoc = true
continue
}
} else if state.inDoc && !state.inMeta {
if t.Name.Local == "book" {
err = dec.DecodeElement(doc.Book, &t)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
continue
}
if t.Name.Local == "para" {
p := &usxPara{}
err = dec.DecodeElement(p, &t)
fmt.Println(p, err)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch p.Style {
case "h":
doc.Meta.H = p.Content
case "toc1":
doc.Meta.TOC1 = p.Content
case "toc2":
doc.Meta.TOC2 = p.Content
case "toc3":
doc.Meta.TOC3 = p.Content
case "mt1":
doc.Meta.MT1 = p.Content
case "mt2":
doc.Meta.MT2 = p.Content
}
}
if t.Name.Local == "chapter" {
state.inMeta = true
goto REPROC
}
} else {
switch t.Name.Local {
case "chapter":
state.chapter = &usxChapter{}
err = dec.DecodeElement(state.chapter, &t)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
doc.Book.Chapters = append(doc.Book.Chapters, state.chapter)
case "para":
if state.chapter != nil {
para := &usxPara{}
err = dec.DecodeElement(para, &t)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
state.chapter.Para = append(state.chapter.Para, para)
}
default:
fmt.Println("unhandled tag:", t.Name.Local)
os.Exit(1)
}
}
case xml.EndElement:
//fmt.Println("end element:")
//fmt.Println(t)
case xml.Directive:
//fmt.Println("Directive:")
//fmt.Println(t)
case xml.CharData:
//fmt.Println("char data:")
//fmt.Println(t)
case xml.Comment:
//fmt.Println("comment:")
//fmt.Println(t)
continue
}
}
fmt.Println(doc.Book.Chapters[2].Para[6].Content)
}