probo/misc/lss2md/main.go
2023-07-06 17:21:36 +02:00

57 lines
1.1 KiB
Go

package main
import (
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"os"
)
type Questionnaire struct {
XMLName xml.Name `xml:"questionnaire"`
Section []struct {
Question []struct {
Text string `xml:"text"`
Response struct {
Fixed struct {
Category []struct {
Label string `xml:"label"`
} `xml:"category"`
} `xml:"fixed"`
} `xml:"response"`
} `xml:"question"`
} `xml:"section"`
}
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
panic("A filename should be provided")
}
xmlFile, err := os.Open(flag.Arg(0))
if err != nil {
fmt.Println(err)
return
}
defer xmlFile.Close()
byteValue, _ := ioutil.ReadAll(xmlFile)
var questionnaire Questionnaire
xml.Unmarshal(byteValue, &questionnaire)
for i, section := range questionnaire.Section {
for j, question := range section.Question {
mdContent := question.Text + "\n\n"
for _, answer := range question.Response.Fixed.Category {
mdContent += "* " + answer.Label + "\n"
}
mdFileName := fmt.Sprintf("question_%d_%d.md", i+1, j+1)
ioutil.WriteFile(mdFileName, []byte(mdContent), 0644)
}
}
}