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