Add lss2md utility

This commit is contained in:
Andrea Fazzi 2023-07-06 17:21:36 +02:00
parent a5b24b7919
commit 483e0a7fe7

57
misc/lss2md/main.go Normal file
View file

@ -0,0 +1,57 @@
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)
}
}
}