Skip to content

Commit

Permalink
Merge pull request #16 from j3lamp/absolute-to-relative-paths
Browse files Browse the repository at this point in the history
Replace absolute href and src attributes with relative paths
  • Loading branch information
technosophos committed Dec 31, 2015
2 parents 1f30238 + b5a97de commit df508fa
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions dashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ func create(c *cli.Context) {
func build(c *cli.Context) {
var dashing Dashing

source_depth := 0
source := c.String("source")
if len(source) == 0 {
source = "."
} else {
source_depth = len(strings.Split(source, "/"))
}

cf := strings.TrimSpace(c.String("config"))
Expand Down Expand Up @@ -207,7 +210,7 @@ func build(c *cli.Context) {
return
}
defer db.Close()
texasRanger(source, name, dashing, db)
texasRanger(source, source_depth, name, dashing, db)
}

func decodeSelectField(d *Dashing) error {
Expand Down Expand Up @@ -306,7 +309,7 @@ func createDB(name string) (*sql.DB, error) {
}

// texasRanger is... wait for it... a WALKER!
func texasRanger(base, name string, dashing Dashing, db *sql.DB) error {
func texasRanger(base string, base_depth int, name string, dashing Dashing, db *sql.DB) error {
filepath.Walk(base, func(path string, info os.FileInfo, err error) error {
fmt.Printf("Reading %s\n", path)
if strings.HasPrefix(path, name+".docset") {
Expand All @@ -323,7 +326,7 @@ func texasRanger(base, name string, dashing Dashing, db *sql.DB) error {
//fmt.Printf("Failed to copy file %s: %s\n", path, err)
//return err
//}
found, err := parseHTML(path, dest, dashing)
found, err := parseHTML(path, base_depth, dest, dashing)
if err != nil {
fmt.Printf("Error parsing %s: %s\n", path, err)
return nil
Expand Down Expand Up @@ -408,7 +411,7 @@ type reference struct {
name, etype, href string
}

func parseHTML(path, dest string, dashing Dashing) ([]*reference, error) {
func parseHTML(path string, source_depth int, dest string, dashing Dashing) ([]*reference, error) {
refs := []*reference{}

r, err := os.Open(path)
Expand All @@ -418,6 +421,25 @@ func parseHTML(path, dest string, dashing Dashing) ([]*reference, error) {
defer r.Close()
top, err := html.Parse(r)

root := css.MustCompile("*[href],*[src]")
roots := root.MatchAll(top)
for _, node := range roots {
for i, attribute := range node.Attr {
if "href" == attribute.Key || "src" == attribute.Key {
if (strings.HasPrefix(attribute.Val, "/")) {
// parts of the path - the file name - the source depth
path_depth := len(strings.Split(attribute.Val[1 :], "/")) - 1 - source_depth
relative := ""
if path_depth > 0 {
strings.Repeat("../", path_depth)
}
node.Attr[i].Val = relative + attribute.Val[1 :];
}
break;
}
}
}

for pattern, sel := range dashing.selectors {
m := css.MustCompile(pattern)
found := m.MatchAll(top)
Expand Down

0 comments on commit df508fa

Please sign in to comment.