Replace title from the contents of the page
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-07-19 17:10:59 +02:00
parent 934d67baae
commit 0197cee9a2
2 changed files with 51 additions and 14 deletions

32
main.go
View File

@ -443,10 +443,15 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
pageBase := getPageBase() pageBase := getPageBase()
title := cleanTitle(page)
if newTitle, err := PageTitle(pageText); err == nil {
title = newTitle
}
data := editPage{ data := editPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
Title: cleanTitle(page), Title: title,
Content: pageText, Content: pageText,
Editor: editor, Editor: editor,
Name: page, 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) { func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
metaKV, err := regexp.Compile(`(\w+)::\s+(.*)`)
if err != nil {
log.Fatal(err)
}
sess, err := NewSession(w, r) sess, err := NewSession(w, r)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
@ -595,6 +605,8 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var rawMsg json.RawMessage var rawMsg json.RawMessage
err = json.NewDecoder(strings.NewReader(pageText)).Decode(&rawMsg) err = json.NewDecoder(strings.NewReader(pageText)).Decode(&rawMsg)
title := cleanTitle(page)
jsonPage := pageText != "" && err == nil jsonPage := pageText != "" && err == nil
if jsonPage { if jsonPage {
var listItems []struct { var listItems []struct {
@ -627,6 +639,11 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
} else { } else {
if matches := metaKV.FindStringSubmatch(item.Text); matches != nil {
if matches[1] == "Title" {
title = matches[2]
}
}
builder.WriteString(strings.Repeat(" ", item.Indented)) builder.WriteString(strings.Repeat(" ", item.Indented))
builder.WriteString("* ") builder.WriteString("* ")
builder.WriteString(item.Text) builder.WriteString(item.Text)
@ -637,10 +654,6 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
if format == "html" { if format == "html" {
metaKV, err := regexp.Compile(`(\w+)::\s+(.*)`)
if err != nil {
log.Fatal(err)
}
pageText = metaKV.ReplaceAllString(pageText, "**[[$1]]**: $2") pageText = metaKV.ReplaceAllString(pageText, "**[[$1]]**: $2")
pageText = renderLinks(pageText, false) pageText = renderLinks(pageText, false)
@ -650,7 +663,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data := indexPage{ data := indexPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
Title: cleanTitle(page), Title: title,
Content: template.HTML(pageText), Content: template.HTML(pageText),
Name: page, Name: page,
Backrefs: mpPage.Refs, Backrefs: mpPage.Refs,
@ -671,7 +684,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} else if format == "markdown" { } else if format == "markdown" {
_, err = io.WriteString(w, "# ") _, 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, "\n\n")
_, err = io.WriteString(w, pageText) _, err = io.WriteString(w, pageText)
} }
@ -811,20 +824,19 @@ func main() {
http.ServeFile(w, r, filepath.Join(dataDir, LinksFile)) http.ServeFile(w, r, filepath.Join(dataDir, LinksFile))
}) })
http.HandleFunc("/api/graph", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/api/graph", func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
gb, err := NewGraphBuilder(mp) gb, err := NewGraphBuilder(mp)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
gb.RemoveNode(name)
for _, name := range r.URL.Query()["name"] {
err = gb.buildFromCenter(name) err = gb.buildFromCenter(name)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
}
// Keep a copy of the nodes, buildFromCenter appends to the nodeMap // Keep a copy of the nodes, buildFromCenter appends to the nodeMap
var nodes []string var nodes []string

25
util.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"encoding/json"
"fmt" "fmt"
"log" "log"
"math/rand" "math/rand"
@ -10,6 +11,10 @@ import (
"time" "time"
) )
var (
MetaKV = regexp.MustCompile(`(\w+)::\s+(.*)`)
)
type ParsedLink struct { type ParsedLink struct {
ID string `json:"ID"` ID string `json:"ID"`
Name string Name string
@ -116,3 +121,23 @@ func todayPage() string {
} }
return fmt.Sprintf("%d_%s_%d", now.Day(), months[now.Month()], now.Year()) 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")
}