This commit is contained in:
Alex Yatskov 2024-03-30 22:00:58 -07:00
parent 73aca287d3
commit 778ea957f0

36
main.go
View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"html"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -36,6 +37,10 @@ type (
Table = []TableRow Table = []TableRow
) )
func wrapSpan(content, class string) string {
return fmt.Sprintf("<span class='%s'>%s</span>", class, html.EscapeString(content))
}
func injectStories(table Table, path string, heisigIndex int) error { func injectStories(table Table, path string, heisigIndex int) error {
fp, err := os.Open(path) fp, err := os.Open(path)
if err != nil { if err != nil {
@ -58,7 +63,8 @@ func injectStories(table Table, path string, heisigIndex int) error {
var stories strings.Builder var stories strings.Builder
for _, story := range character.Stories { for _, story := range character.Stories {
stories.WriteString(fmt.Sprintf("<span>%s</span>", story.Content)) content := strings.ReplaceAll(story.Content, "\n", " ")
stories.WriteString(wrapSpan(content, "rtk-story"))
} }
row = append(row, stories.String()) row = append(row, stories.String())
@ -89,30 +95,29 @@ func injectKanjidic(table Table, path string, heisigIndex int) error {
} }
var ( var (
meanings []string meanings strings.Builder
kunyomi []string kunyomi strings.Builder
onyomi []string onyomi strings.Builder
) )
for _, reading := range character.ReadingMeaning.Readings { for _, reading := range character.ReadingMeaning.Readings {
switch reading.Type { switch reading.Type {
case "ja_on": case "ja_on":
onyomi = append(onyomi, reading.Value) onyomi.WriteString(wrapSpan(reading.Value, "rtk-onyomi"))
case "ja_kun": case "ja_kun":
kunyomi = append(kunyomi, reading.Value) kunyomi.WriteString(wrapSpan(reading.Value, "rtk-kunyomi"))
} }
} }
for _, meaning := range character.ReadingMeaning.Meanings { for _, meaning := range character.ReadingMeaning.Meanings {
if meaning.Language == nil { if meaning.Language == nil {
meanings = append(meanings, meaning.Meaning) meanings.WriteString(wrapSpan(meaning.Meaning, "rtk-meaning"))
} }
} }
const seperator = " " row = append(row, meanings.String())
row = append(row, strings.Join(meanings, seperator)) row = append(row, kunyomi.String())
row = append(row, strings.Join(kunyomi, seperator)) row = append(row, onyomi.String())
row = append(row, strings.Join(onyomi, seperator))
table[i] = row table[i] = row
found = true found = true
@ -160,6 +165,7 @@ func saveTable(path string, table Table) error {
defer fp.Close() defer fp.Close()
writer := csv.NewWriter(fp) writer := csv.NewWriter(fp)
writer.Comma = '\t'
if err := writer.WriteAll(table); err != nil { if err := writer.WriteAll(table); err != nil {
return err return err
} }
@ -194,14 +200,14 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
if len(*storiesPath) > 0 { if len(*kanjidicPath) > 0 {
if err := injectStories(table, *storiesPath, *heisigIndex); err != nil { if err := injectKanjidic(table, *kanjidicPath, *heisigIndex); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
if len(*kanjidicPath) > 0 { if len(*storiesPath) > 0 {
if err := injectKanjidic(table, *kanjidicPath, *heisigIndex); err != nil { if err := injectStories(table, *storiesPath, *heisigIndex); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }