|
| 1 | +package bundler |
| 2 | + |
| 3 | +import ( |
| 4 | + "ModCreator/file" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// BundleXML converts <Include ... >'s into full xml |
| 11 | +func BundleXML(rawxml string, xr file.TextReader) (string, error) { |
| 12 | + lines := strings.Split(rawxml, "\n") |
| 13 | + final := []string{} |
| 14 | + inctag := regexp.MustCompile(`(?m)^(.*)<Include src="(.*)"/>`) |
| 15 | + |
| 16 | + for _, v := range lines { |
| 17 | + if !inctag.Match([]byte(v)) { |
| 18 | + final = append(final, v) |
| 19 | + continue |
| 20 | + } |
| 21 | + subs := inctag.FindSubmatch([]byte(v)) |
| 22 | + indent := string(subs[1]) |
| 23 | + name := string(subs[2]) |
| 24 | + |
| 25 | + replacement := fmt.Sprintf("%s<!-- include %s -->", indent, name) |
| 26 | + final = append(final, replacement) |
| 27 | + |
| 28 | + fname := name |
| 29 | + if !strings.HasSuffix(name, ".xml") { |
| 30 | + fname = name + ".xml" |
| 31 | + } |
| 32 | + incXMLRaw, err := xr.EncodeFromFile(fname) |
| 33 | + if err != nil { |
| 34 | + return "", fmt.Errorf("EncodeFromFile(%s): %v", fname, err) |
| 35 | + } |
| 36 | + incXMLBundled, err := BundleXML(incXMLRaw, xr) |
| 37 | + if err != nil { |
| 38 | + return "", fmt.Errorf("BundleXML(<%s>): %v", fname, err) |
| 39 | + } |
| 40 | + final = append(final, indentString(incXMLBundled, indent)) |
| 41 | + |
| 42 | + final = append(final, replacement) |
| 43 | + |
| 44 | + } |
| 45 | + return strings.Join(final, "\n"), nil |
| 46 | +} |
| 47 | + |
| 48 | +func indentString(s string, indent string) string { |
| 49 | + lines := strings.Split(s, "\n") |
| 50 | + final := []string{} |
| 51 | + for _, v := range lines { |
| 52 | + if v == "" { |
| 53 | + final = append(final, "") |
| 54 | + continue |
| 55 | + } |
| 56 | + final = append(final, fmt.Sprintf("%s%s", indent, v)) |
| 57 | + } |
| 58 | + |
| 59 | + return strings.Join(final, "\n") |
| 60 | +} |
| 61 | + |
| 62 | +// UnbundleAllXML converts a bundled xml file to mapping of filenames to |
| 63 | +// contents |
| 64 | +func UnbundleAllXML(rawxml string) (map[string]string, error) { |
| 65 | + type inc struct { |
| 66 | + name string |
| 67 | + start int |
| 68 | + } |
| 69 | + store := map[string]string{} |
| 70 | + inctag := regexp.MustCompile(`(?m)^(.*?)<!-- include (.*) -->`) |
| 71 | + stack := []inc{} |
| 72 | + xmlarray := strings.Split(rawxml, "\n") |
| 73 | + |
| 74 | + for ln := 0; ln < len(xmlarray); ln++ { |
| 75 | + val := xmlarray[ln] |
| 76 | + if !inctag.Match([]byte(val)) { |
| 77 | + continue |
| 78 | + } |
| 79 | + submatches := inctag.FindSubmatch([]byte(val)) |
| 80 | + key := string(submatches[2]) |
| 81 | + if len(stack) > 0 && stack[0].name == key { |
| 82 | + // found end of include, process it |
| 83 | + indent := string(submatches[1]) |
| 84 | + indentedvals := xmlarray[stack[0].start+1 : ln] |
| 85 | + store[stack[0].name] = unindentAndJoin(indentedvals, indent) |
| 86 | + |
| 87 | + insertLine := fmt.Sprintf("%s<Include src=\"%s\"/>", indent, stack[0].name) |
| 88 | + // remove the include from xmlarray |
| 89 | + tmp := append(xmlarray[:stack[0].start], insertLine) |
| 90 | + xmlarray = append(tmp, xmlarray[ln+1:]...) |
| 91 | + ln = stack[0].start |
| 92 | + stack = stack[1:] |
| 93 | + } else { |
| 94 | + stack = append([]inc{inc{name: key, start: ln}}, stack...) |
| 95 | + } |
| 96 | + } |
| 97 | + if len(stack) != 0 { |
| 98 | + return nil, fmt.Errorf("Bundled xml left after finished reading file: %v", stack) |
| 99 | + } |
| 100 | + store[Rootname] = unindentAndJoin(xmlarray, "") |
| 101 | + return store, nil |
| 102 | +} |
| 103 | + |
| 104 | +func unindentAndJoin(raw []string, indent string) string { |
| 105 | + ret := []string{} |
| 106 | + for _, v := range raw { |
| 107 | + ret = append(ret, strings.Replace(v, indent, "", 1)) |
| 108 | + } |
| 109 | + return strings.Join(ret, "\n") |
| 110 | +} |
0 commit comments