From 0197cee9a2145be5867e22c4d7841a113464e165 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 19 Jul 2020 17:10:59 +0200 Subject: [PATCH] Replace title from the contents of the page --- main.go | 40 ++++++++++++++++++++++++++-------------- util.go | 25 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 0e6c447..892f8cb 100644 --- a/main.go +++ b/main.go @@ -443,10 +443,15 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } pageBase := getPageBase() + title := cleanTitle(page) + if newTitle, err := PageTitle(pageText); err == nil { + title = newTitle + } + data := editPage{ pageBaseInfo: pageBase, Session: sess, - Title: cleanTitle(page), + Title: title, Content: pageText, Editor: editor, Name: page, @@ -551,6 +556,11 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() + metaKV, err := regexp.Compile(`(\w+)::\s+(.*)`) + if err != nil { + log.Fatal(err) + } + sess, err := NewSession(w, r) if err != nil { http.Error(w, err.Error(), 500) @@ -595,6 +605,8 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { var rawMsg json.RawMessage err = json.NewDecoder(strings.NewReader(pageText)).Decode(&rawMsg) + title := cleanTitle(page) + jsonPage := pageText != "" && err == nil if jsonPage { var listItems []struct { @@ -627,6 +639,11 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } } else { + if matches := metaKV.FindStringSubmatch(item.Text); matches != nil { + if matches[1] == "Title" { + title = matches[2] + } + } builder.WriteString(strings.Repeat(" ", item.Indented)) builder.WriteString("* ") builder.WriteString(item.Text) @@ -637,10 +654,6 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if format == "html" { - metaKV, err := regexp.Compile(`(\w+)::\s+(.*)`) - if err != nil { - log.Fatal(err) - } pageText = metaKV.ReplaceAllString(pageText, "**[[$1]]**: $2") pageText = renderLinks(pageText, false) @@ -650,7 +663,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { data := indexPage{ pageBaseInfo: pageBase, Session: sess, - Title: cleanTitle(page), + Title: title, Content: template.HTML(pageText), Name: page, Backrefs: mpPage.Refs, @@ -671,7 +684,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } else if format == "markdown" { _, err = io.WriteString(w, "# ") - _, err = io.WriteString(w, cleanTitle(page)) + _, err = io.WriteString(w, title) _, err = io.WriteString(w, "\n\n") _, err = io.WriteString(w, pageText) } @@ -811,19 +824,18 @@ func main() { http.ServeFile(w, r, filepath.Join(dataDir, LinksFile)) }) http.HandleFunc("/api/graph", func(w http.ResponseWriter, r *http.Request) { - name := r.URL.Query().Get("name") - gb, err := NewGraphBuilder(mp) if err != nil { http.Error(w, err.Error(), 500) return } - gb.RemoveNode(name) - err = gb.buildFromCenter(name) - if err != nil { - http.Error(w, err.Error(), 500) - return + for _, name := range r.URL.Query()["name"] { + err = gb.buildFromCenter(name) + if err != nil { + http.Error(w, err.Error(), 500) + return + } } // Keep a copy of the nodes, buildFromCenter appends to the nodeMap diff --git a/util.go b/util.go index 306aad5..702ab4f 100644 --- a/util.go +++ b/util.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "encoding/json" "fmt" "log" "math/rand" @@ -10,6 +11,10 @@ import ( "time" ) +var ( + MetaKV = regexp.MustCompile(`(\w+)::\s+(.*)`) +) + type ParsedLink struct { ID string `json:"ID"` Name string @@ -116,3 +121,23 @@ func todayPage() string { } return fmt.Sprintf("%d_%s_%d", now.Day(), months[now.Month()], now.Year()) } + +func PageTitle(pageText string) (string, error) { + var listItems []struct { + Indented int + Text string + } + err := json.NewDecoder(strings.NewReader(pageText)).Decode(&listItems) + if err != nil { + return "", fmt.Errorf("while decoding page text: %w", err) + } + + for _, li := range listItems { + if matches := MetaKV.FindStringSubmatch(li.Text); matches != nil { + if matches[1] == "Title" { + return matches[2], nil + } + } + } + return "", fmt.Errorf("no meta title found in page text") +}