| 1 |
package main |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"encoding/xml" |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
func parseXMLtoTOML(input []byte) { |
| 14 |
parser := xml.NewDecoder(bytes.NewReader(input)) |
| 15 |
keywords := []string{} |
| 16 |
name := "" |
| 17 |
fmt.Println("% # Quick 'n dirty translated by mmark") |
| 18 |
for { |
| 19 |
token, err := parser.Token() |
| 20 |
if err != nil { |
| 21 |
break |
| 22 |
} |
| 23 |
switch t := token.(type) { |
| 24 |
case xml.StartElement: |
| 25 |
elmt := xml.StartElement(t) |
| 26 |
name = elmt.Name.Local |
| 27 |
switch name { |
| 28 |
case "author": |
| 29 |
fmt.Println("%\n% [[author]]") |
| 30 |
outAttr(elmt.Attr) |
| 31 |
case "rfc": |
| 32 |
fallthrough |
| 33 |
case "title": |
| 34 |
outAttr(elmt.Attr) |
| 35 |
case "address": |
| 36 |
fmt.Println("% [author.address]") |
| 37 |
case "postal": |
| 38 |
fmt.Println("% [author.address.postal]") |
| 39 |
case "date": |
| 40 |
outDate(elmt.Attr) |
| 41 |
} |
| 42 |
case xml.CharData: |
| 43 |
if name == "" { |
| 44 |
continue |
| 45 |
} |
| 46 |
data := xml.CharData(t) |
| 47 |
data = bytes.TrimSpace(data) |
| 48 |
if len(data) == 0 { |
| 49 |
continue |
| 50 |
} |
| 51 |
if name == "keyword" { |
| 52 |
keywords = append(keywords, "\""+string(data)+"\"") |
| 53 |
continue |
| 54 |
} |
| 55 |
outString(name, string(data)) |
| 56 |
case xml.EndElement: |
| 57 |
name = "" |
| 58 |
case xml.Comment: |
| 59 |
|
| 60 |
case xml.ProcInst: |
| 61 |
|
| 62 |
case xml.Directive: |
| 63 |
|
| 64 |
default: |
| 65 |
} |
| 66 |
} |
| 67 |
outArray("keyword", keywords) |
| 68 |
} |
| 69 |
|
| 70 |
func outString(k, v string) { |
| 71 |
fmt.Printf("%% %s = \"%s\"\n", k, v) |
| 72 |
} |
| 73 |
|
| 74 |
func outDate(attr []xml.Attr) { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
fmt.Printf("%%\n%% # TODO date \n%%\n") |
| 89 |
} |
| 90 |
|
| 91 |
func outArray(k string, arr []string) { |
| 92 |
all := strings.Join(arr, ", ") |
| 93 |
fmt.Printf("%%\n%% keyword = [ %s ]\n%%", all) |
| 94 |
} |
| 95 |
|
| 96 |
func outAttr(attr []xml.Attr) { |
| 97 |
for _, a := range attr { |
| 98 |
outString(a.Name.Local, a.Value) |
| 99 |
} |
| 100 |
} |