diff --git a/main.go b/main.go index 86f54be69207ed072e9f03055ed8f64e2ffea3d1..cfbb759698cb96a3105f20dc766becf62275a200 100644 --- a/main.go +++ b/main.go @@ -17,11 +17,12 @@ import ( ) var ( - flagInput *string = flag.StringP("input", "i", "", "Input file") - flagOutput *string = flag.StringP("output", "o", "", "Output file") - flagTemplate *string = flag.StringP("template", "t", "", "Template file") - flagIgnore *string = flag.StringP("ignore", "g", "", "Comma-separated list of sections to ignore") - flagHelp *bool = flag.BoolP("help", "h", false, "Show help and exit") + flagInput *string = flag.StringP("input", "i", "", "Path to input OPML") + flagOutput *string = flag.StringP("output", "o", "", "Path to output MD") + flagXMLOutput *string = flag.StringP("xmloutput", "x", "", "Path to output XML") + flagTemplate *string = flag.StringP("template", "t", "", "Path to template MD file") + flagIgnore *string = flag.StringP("ignore", "g", "", "Comma-separated list of sections to ignore") + flagHelp *bool = flag.BoolP("help", "h", false, "Show help and exit") ) type OPML struct { @@ -78,19 +79,31 @@ func main() { feedsList := opmlToList(opml.Body.Outlines) - str, err := feedsToFile(feedsList) + listString, err := feedsToFile(feedsList) if err != nil { fmt.Println(err) os.Exit(1) } - err = os.WriteFile(*flagOutput, []byte(str), 0o644) + err = os.WriteFile(*flagOutput, []byte(listString), 0o644) if err != nil { fmt.Println(err) os.Exit(1) } - fmt.Printf("Wrote %s\n", *flagOutput) + + if *flagXMLOutput != "" { + newOPML := newOPML(opml) + marshalledOPML, err := xml.MarshalIndent(newOPML, "", "\t") + xmlBytes := []byte(xml.Header + string(marshalledOPML)) + + err = os.WriteFile(*flagXMLOutput, xmlBytes, 0o644) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + fmt.Printf("Wrote %s\n", *flagXMLOutput) + } } func parseOPML(filename string) (*OPML, error) { @@ -162,6 +175,44 @@ func feedsToFile(feedsList string) (string, error) { return buf.String(), nil } +func newOPML(opml *OPML) *OPML { + ignoreMap := make(map[string]bool) + if *flagIgnore != "" { + ignore := strings.Split(*flagIgnore, ",") + for _, i := range ignore { + ignoreMap[i] = true + } + } + + newOPML := *opml + newOPML.Body.Outlines = ignoreOutlines(newOPML.Body.Outlines, ignoreMap) + return &newOPML +} + +func ignoreOutlines(outline []Outline, ignoreMap map[string]bool) []Outline { + var newOutlines []Outline + for _, o := range outline { + if o.Outlines != nil { + if ignoreMap[o.Text] { + continue + } + o.Outlines = ignoreOutlines(o.Outlines, ignoreMap) + newOutlines = append(newOutlines, o) + } else { + if o.HTMLURL == "" { + parsedXMLURL, err := url.Parse(o.XMLURL) + if err != nil { + fmt.Println(err) + continue + } + o.HTMLURL = fmt.Sprintf("%s://%s", parsedXMLURL.Scheme, parsedXMLURL.Host) + } + newOutlines = append(newOutlines, o) + } + } + return newOutlines +} + func flags() { flag.Parse() if *flagHelp {