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

28
main.go
View File

@ -79,20 +79,26 @@ func renderAll() error {
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 {
return fmt.Errorf("unable to create goquery instance: %w", err)
}
objs := make(map[string]Object)
query.Find("body #contents").NextFiltered("ul").Find("ul").Each(func(_ int, s *goquery.Selection) {
s.Find("li a").Each(func(_ int, s *goquery.Selection) {
doc.
Find("body #contents").
NextFiltered("ul").
Find("ul").
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 {
return
}
@ -105,7 +111,7 @@ func renderAll() error {
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)
}
@ -200,11 +206,11 @@ func renderSitemap(objs map[string]Object) error {
return nil
}
func makeObjByID(selector string, s *goquery.Selection) (*Object, error) {
func makeObjByID(selector string, doc *goquery.Document) (*Object, error) {
var obj Object
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")
// FIXME: bug. this would select links from all neighboring
// sub-categories until the next category. To prevent this we should
@ -243,8 +249,10 @@ func makeObjByID(selector string, s *goquery.Selection) (*Object, error) {
return &obj, nil
}
func rewriteLinksInIndex(query *goquery.Document, objs map[string]Object) error {
query.Find("body #content ul li ul li a").Each(func(_ int, s *goquery.Selection) {
func rewriteLinksInIndex(doc *goquery.Document, objs map[string]Object) error {
doc.
Find("body #content ul li ul li a").
Each(func(_ int, s *goquery.Selection) {
href, hrefExists := s.Attr("href")
if !hrefExists {
// FIXME: looks like is an error. Tag `a` in our case always
@ -266,7 +274,7 @@ func rewriteLinksInIndex(query *goquery.Document, objs map[string]Object) error
})
fmt.Printf("Rewrite links in Index file: %s\n", outIndexFile)
resultHtml, err := query.Html()
resultHtml, err := doc.Html()
if err != nil {
return fmt.Errorf("unable to render html: %w", err)
}