Add format=json, format=metakv

This commit is contained in:
Peter Stuifzand 2020-08-31 11:50:03 +02:00
parent 077d76462c
commit 91d59c5b0f
3 changed files with 49 additions and 12 deletions

32
main.go
View File

@ -462,7 +462,7 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ShowGraph: page != "Daily_Notes",
}
templates := baseTemplate
templates = append(templates, "templates/edit.html")
templates = append(templates, "templates/edit.html")
t, err := template.ParseFiles(templates...)
if err != nil {
http.Error(w, err.Error(), 500)
@ -544,7 +544,7 @@ func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Edges: template.JS(edgesBuf.String()),
}
templates := baseTemplate
templates = append(templates, "templates/graph.html")
templates = append(templates, "templates/graph.html")
t, err := template.ParseFiles(templates...)
if err != nil {
http.Error(w, err.Error(), 500)
@ -586,7 +586,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
format = "html"
}
if !(format == "html" || format == "markdown") {
if !(format == "html" || format == "markdown" || format == "json" || format == "metakv") {
http.Error(w, "unknown format", http.StatusBadRequest)
}
@ -611,6 +611,28 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
jsonPage := pageText != "" && err == nil
if jsonPage {
if format == "json" {
// Shortcut for json output
_, err := io.WriteString(w, pageText)
if err != nil {
http.Error(w, err.Error(), 500)
}
return
} else if format == "metakv" {
so, err := createSearchObject(mpPage)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
err = enc.Encode(so)
if err != nil {
http.Error(w, err.Error(), 500)
}
return
}
var listItems []struct {
Indented int
Text string
@ -672,8 +694,8 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ShowGraph: page != "Daily_Notes",
TodayPage: "Today",
}
templates := baseTemplate
templates = append(templates, "templates/view.html")
templates := baseTemplate
templates = append(templates, "templates/view.html")
t, err := template.ParseFiles(templates...)
if err != nil {
http.Error(w, err.Error(), 500)

View File

@ -33,6 +33,7 @@ type searchObject struct {
Blocks []string `json:"blocks"`
Refs []nameLine `json:"refs"`
Meta map[string]string `json:"meta"`
Links []ParsedLink `json:"links"`
}
func NewSearchHandler(searchIndex bleve.Index) (http.Handler, error) {
@ -188,6 +189,7 @@ func createSearchObject(page Page) (searchObject, error) {
type simpleListItem struct {
Text string
ID string
}
var listItems []simpleListItem
@ -198,13 +200,25 @@ func createSearchObject(page Page) (searchObject, error) {
meta := strings.SplitN(li.Text, "::", 2)
if len(meta) == 2 {
key := strings.ToLower(strings.TrimSpace(meta[0]))
value := strings.ToLower(strings.TrimSpace(meta[1]))
if (key == "title") {
value := strings.TrimSpace(meta[1])
if key == "title" {
so.Title = value
}
so.Meta[key] = value
}
so.Blocks = append(so.Blocks, li.Text)
links, err := ParseLinks(li.ID, li.Text)
if err != nil {
continue
}
for i, link := range links {
links[i].Href = fmt.Sprintf("%s%s", *baseurl, link.PageName)
}
so.Links = append(so.Links, links...)
}
}
@ -213,7 +227,7 @@ func createSearchObject(page Page) (searchObject, error) {
so.Refs = append(so.Refs, nameLine{
ref.Name,
ref.Title,
ref.Line,
strings.TrimSpace(ref.Line),
})
}
}

View File

@ -17,9 +17,10 @@ var (
type ParsedLink struct {
ID string `json:"ID"`
Name string
PageName string
Line string
Name string `json:"title"`
PageName string `json:"name"`
Line string `json:"line"`
Href string `json:"href"`
}
var random *rand.Rand
@ -64,7 +65,7 @@ func ParseLinks(blockId string, content string) ([]ParsedLink, error) {
link = strings.TrimSuffix(link, "]]")
link = strings.TrimSpace(link)
l := cleanNameURL(link)
result = append(result, ParsedLink{blockId, link, l, line})
result = append(result, ParsedLink{blockId, link, l, line, ""})
}
// keywords := keywordsRE.FindAllStringSubmatch(line, -1)