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

40
main.go
View File

@ -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

25
util.go
View File

@ -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")
}