From 483e0a7fe7af504b97c6c930afcedb7ceda1551f Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Thu, 6 Jul 2023 17:21:36 +0200 Subject: [PATCH] Add lss2md utility --- misc/lss2md/main.go | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 misc/lss2md/main.go diff --git a/misc/lss2md/main.go b/misc/lss2md/main.go new file mode 100644 index 0000000..dd28003 --- /dev/null +++ b/misc/lss2md/main.go @@ -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) + } + } +}