diff --git a/main.go b/main.go index 8e86154..d5dfd44 100644 --- a/main.go +++ b/main.go @@ -76,7 +76,7 @@ func main() { ignoreBlobs := getIgnoredFiles(*root) l, i := walk(*in, ".md", *index, ignoreBlobs) f := filter(l) - err := write(f, i, *index, *out) + err := write(f, i, *index, *out, *root) if err != nil { panic(err) } diff --git a/walk.go b/walk.go index a1c1c6e..769208a 100644 --- a/walk.go +++ b/walk.go @@ -42,7 +42,6 @@ func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res [] fmt.Printf("[Ignored] %s\n", d.Name()) nPrivate++ } else if filepath.Ext(d.Name()) == ext { - res = append(res, parse(s, root)...) if index { text := getText(s) @@ -61,12 +60,13 @@ func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res [] info, _ := os.Stat(s) source := processSource(trim(s, root, ".md")) - // adjustedPath := UnicodeSanitize(strings.Replace(hugoPathTrim(trim(s, root, ".md")), " ", "-", -1)) + // add to content and link index i[source] = Content{ LastModified: info.ModTime(), Title: matter.Title, Content: body, } + res = append(res, parse(s, root)...) } else { fmt.Printf("[Ignored] %s\n", d.Name()) nPrivate++ diff --git a/write.go b/write.go index e199e0a..f258c6f 100644 --- a/write.go +++ b/write.go @@ -1,16 +1,18 @@ package main import ( + "bufio" "encoding/json" "io/ioutil" + "os" "path" ) -func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) error { +func write(links []Link, contentIndex ContentIndex, toIndex bool, out string, root string) error { index := index(links) resStruct := struct { - Index Index `json:"index"` - Links []Link `json:"links"` + Index Index `json:"index"` + Links []Link `json:"links"` }{ Index: index, Links: links, @@ -25,6 +27,7 @@ func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) er return writeErr } + // check whether to index content if toIndex { marshalledContentIndex, mcErr := json.MarshalIndent(&contentIndex, "", " ") if mcErr != nil { @@ -35,11 +38,38 @@ func write(links []Link, contentIndex ContentIndex, toIndex bool, out string) er if writeErr != nil { return writeErr } + + // write linkmap + writeErr = writeLinkMap(&contentIndex, root) + if writeErr != nil { + return writeErr + } } return nil } +func writeLinkMap(contentIndex *ContentIndex, root string) error { + fp := path.Join(root, "linkmap") + file, err := os.OpenFile(fp, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return err + } + + datawriter := bufio.NewWriter(file) + for path := range *contentIndex { + if path == "/" { + _, _ = datawriter.WriteString("/index.html /\n") + } else { + _, _ = datawriter.WriteString(path + "/$1.{html} " + path + "/$1\n") + } + } + datawriter.Flush() + file.Close() + + return nil +} + // constructs index from links func index(links []Link) (index Index) { linkMap := make(map[string][]Link) @@ -63,6 +93,3 @@ func index(links []Link) (index Index) { index.Backlinks = backlinkMap return index } - - -