func main() { flag.Parse() // parses the swagger spec file spec, err := spec.YAMLSpec(*input) if err != nil { log.Fatal(err) } swag := spec.Spec() // create output source for file. defaults to // stdout but may be file. var w io.WriteCloser = os.Stdout if *output != "" { w, err = os.Create(*output) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) return } defer w.Close() } // we wrap the swagger file in a map, otherwise it // won't work with our existing templates, which expect // a map as the root parameter. var data = map[string]interface{}{ "Swagger": normalize(swag), } t := amber.MustCompileFile(*templ, amber.DefaultOptions) err = t.Execute(w, data) if err != nil { log.Fatal(err) } }
func main() { flag.Parse() // read the markdown file into a document element. document, err := toDocument(*input) if err != nil { log.Fatalf("Error opening %s. %s", *input, err) } // we assume this file is the sitemap, so we'll look // for the first ul element, and assume that contains // the site hierarchy. sitemap := document.Find("ul").First() site := Site{} site.Name = *name site.base = filepath.Dir(*input) // for each link in the sitemap we should attempt to // read the markdown file and add to our list of pages // to generate. sitemap.Find("li > a").EachWithBreak(func(i int, s *goquery.Selection) bool { page, err := toPage(&site, s) if err != nil { log.Fatalf("Error following link %s. %s", s.Text(), err) } if page != nil { site.Pages = append(site.Pages, page) } return true }) site.Nav = &Nav{} site.Nav.elem = sitemap site.Nav.html, err = sitemap.Html() if err != nil { log.Fatal(err) } // compiles our template which is in amber/jade format templ := amber.MustCompileFile(*html, amber.DefaultOptions) // for each page in the sitemap we generate a // corresponding html file using the above template. for _, page := range site.Pages { path := filepath.Join(*output, page.Href) f, err := os.Create(path) if err != nil { log.Fatalf("Error creating file %s. %s", path, err) } defer f.Close() // correctly make the active page in the // navigation html snippet site.Nav.elem.Find("li > a").EachWithBreak(func(i int, s *goquery.Selection) bool { href, _ := s.Attr("href") if href == page.Href { s.Parent().AddClass("active") } else { s.Parent().RemoveClass("active") } return true }) site.Nav.html, _ = site.Nav.elem.Html() data := map[string]interface{}{ "Site": site, "Page": page, } err = templ.Execute(f, &data) if err != nil { log.Fatalf("Error generating template %s. %s", path, err) } } }