function chains splitted into lines

This commit is contained in:
Kirill Zhuravlev 2023-02-15 00:21:17 +01:00 committed by Avelino
parent 776c1fee45
commit 2a6e7d9086
No known key found for this signature in database
GPG Key ID: B345B4D52E98180A

80
main.go
View File

@ -79,33 +79,39 @@ func renderAll() error {
return fmt.Errorf("unable to read converted html: %w", err) return fmt.Errorf("unable to read converted html: %w", err)
} }
query, err := goquery.NewDocumentFromReader(bytes.NewReader(input)) doc, err := goquery.NewDocumentFromReader(bytes.NewReader(input))
if err != nil { if err != nil {
return fmt.Errorf("unable to create goquery instance: %w", err) return fmt.Errorf("unable to create goquery instance: %w", err)
} }
objs := make(map[string]Object) objs := make(map[string]Object)
query.Find("body #contents").NextFiltered("ul").Find("ul").Each(func(_ int, s *goquery.Selection) { doc.
s.Find("li a").Each(func(_ int, s *goquery.Selection) { Find("body #contents").
selector, exists := s.Attr("href") NextFiltered("ul").
if !exists { Find("ul").
return Each(func(_ int, selUl *goquery.Selection) {
} selUl.
Find("li a").
Each(func(_ int, s *goquery.Selection) {
selector, exists := s.Attr("href")
if !exists {
return
}
obj, err := makeObjByID(selector, query.Find("body")) obj, err := makeObjByID(selector, doc)
if err != nil { if err != nil {
return return
} }
objs[selector] = *obj objs[selector] = *obj
})
}) })
})
if err := renderCategories(objs); err != nil { if err := renderCategories(objs); err != nil {
return fmt.Errorf("unable to render categories: %w", err) return fmt.Errorf("unable to render categories: %w", err)
} }
if err := rewriteLinksInIndex(query, objs); err != nil { if err := rewriteLinksInIndex(doc, objs); err != nil {
return fmt.Errorf("unable to rewrite links in index: %w", err) return fmt.Errorf("unable to rewrite links in index: %w", err)
} }
@ -200,11 +206,11 @@ func renderSitemap(objs map[string]Object) error {
return nil return nil
} }
func makeObjByID(selector string, s *goquery.Selection) (*Object, error) { func makeObjByID(selector string, doc *goquery.Document) (*Object, error) {
var obj Object var obj Object
var err error var err error
s.Find(selector).Each(func(_ int, selCatHeader *goquery.Selection) { doc.Find(selector).Each(func(_ int, selCatHeader *goquery.Selection) {
selDescr := selCatHeader.NextFiltered("p") selDescr := selCatHeader.NextFiltered("p")
// FIXME: bug. this would select links from all neighboring // FIXME: bug. this would select links from all neighboring
// sub-categories until the next category. To prevent this we should // sub-categories until the next category. To prevent this we should
@ -243,30 +249,32 @@ func makeObjByID(selector string, s *goquery.Selection) (*Object, error) {
return &obj, nil return &obj, nil
} }
func rewriteLinksInIndex(query *goquery.Document, objs map[string]Object) error { func rewriteLinksInIndex(doc *goquery.Document, objs map[string]Object) error {
query.Find("body #content ul li ul li a").Each(func(_ int, s *goquery.Selection) { doc.
href, hrefExists := s.Attr("href") Find("body #content ul li ul li a").
if !hrefExists { Each(func(_ int, s *goquery.Selection) {
// FIXME: looks like is an error. Tag `a` in our case always href, hrefExists := s.Attr("href")
// should have `href` attr. if !hrefExists {
return // FIXME: looks like is an error. Tag `a` in our case always
} // should have `href` attr.
return
}
// do not replace links if no page has been created for it // do not replace links if no page has been created for it
_, objExists := objs[href] _, objExists := objs[href]
if !objExists { if !objExists {
return return
} }
// FIXME: parse url // FIXME: parse url
uri := strings.SplitAfter(href, "#") uri := strings.SplitAfter(href, "#")
if len(uri) >= 2 && uri[1] != "contents" { if len(uri) >= 2 && uri[1] != "contents" {
s.SetAttr("href", uri[1]) s.SetAttr("href", uri[1])
} }
}) })
fmt.Printf("Rewrite links in Index file: %s\n", outIndexFile) fmt.Printf("Rewrite links in Index file: %s\n", outIndexFile)
resultHtml, err := query.Html() resultHtml, err := doc.Html()
if err != nil { if err != nil {
return fmt.Errorf("unable to render html: %w", err) return fmt.Errorf("unable to render html: %w", err)
} }