mirror of
https://github.com/avelino/awesome-go.git
synced 2024-11-28 16:56:26 +00:00
parse url with url.Parse
This commit is contained in:
parent
0215981b89
commit
a868ca431b
27
main.go
27
main.go
@ -5,9 +5,9 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
cp "github.com/otiai10/copy"
|
cp "github.com/otiai10/copy"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
@ -280,29 +280,40 @@ func extractCategory(doc *goquery.Document, selector string) (*Category, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func rewriteLinksInIndex(doc *goquery.Document, categories map[string]Category) error {
|
func rewriteLinksInIndex(doc *goquery.Document, categories map[string]Category) error {
|
||||||
|
var iterErr error
|
||||||
doc.
|
doc.
|
||||||
Find("body #content ul li ul li a").
|
Find("body #content ul li ul li a").
|
||||||
Each(func(_ int, s *goquery.Selection) {
|
EachWithBreak(func(_ int, s *goquery.Selection) bool {
|
||||||
href, hrefExists := s.Attr("href")
|
href, hrefExists := s.Attr("href")
|
||||||
if !hrefExists {
|
if !hrefExists {
|
||||||
// FIXME: looks like is an error. Tag `a` in our case always
|
// FIXME: looks like is an error. Tag `a` in our case always
|
||||||
// should have `href` attr.
|
// should have `href` attr.
|
||||||
return
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// do not replace links if no page has been created for it
|
// do not replace links if no page has been created for it
|
||||||
_, catExists := categories[href]
|
_, catExists := categories[href]
|
||||||
if !catExists {
|
if !catExists {
|
||||||
return
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: parse url
|
linkUrl, err := url.Parse(href)
|
||||||
uri := strings.SplitAfter(href, "#")
|
if err != nil {
|
||||||
if len(uri) >= 2 && uri[1] != "contents" {
|
iterErr = err
|
||||||
s.SetAttr("href", uri[1])
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if linkUrl.Fragment != "" && linkUrl.Fragment != "contents" {
|
||||||
|
s.SetAttr("href", linkUrl.Fragment)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if iterErr != nil {
|
||||||
|
return iterErr
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Printf("Rewrite links in Index file: %s\n", outIndexFile)
|
fmt.Printf("Rewrite links in Index file: %s\n", outIndexFile)
|
||||||
resultHtml, err := doc.Html()
|
resultHtml, err := doc.Html()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user