|
|
|
/*
|
|
|
|
* Wiki - A wiki with editor
|
|
|
|
* Copyright (c) 2021 Peter Stuifzand
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"p83.nl/go/ekster/pkg/util"
|
|
|
|
"p83.nl/go/indieauth"
|
|
|
|
"p83.nl/go/wiki/link"
|
|
|
|
|
|
|
|
"github.com/blevesearch/bleve/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.SetFlags(log.Lshortfile)
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
|
|
|
type authorizedKey string
|
|
|
|
|
|
|
|
var (
|
|
|
|
authKey = authorizedKey("authorizedKey")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mp PagesRepository
|
|
|
|
|
|
|
|
port = flag.Int("port", 8080, "listen port")
|
|
|
|
baseurl = flag.String("baseurl", "", "baseurl")
|
|
|
|
redirectURI = ""
|
|
|
|
authToken = os.Getenv("API_TOKEN")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Day struct {
|
|
|
|
Class string
|
|
|
|
Text string
|
|
|
|
URL string
|
|
|
|
Count string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Backref struct {
|
|
|
|
Name string
|
|
|
|
Title string
|
|
|
|
LineHTML template.HTML
|
|
|
|
LineEditHTML template.HTML
|
|
|
|
Line string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Page
|
|
|
|
type Page struct {
|
|
|
|
// Name is the filename of the page
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Title is the human-readable title of the page
|
|
|
|
Title string
|
|
|
|
|
|
|
|
Content string
|
|
|
|
|
|
|
|
Refs map[string][]Backref
|
|
|
|
Blocks BlockResponse
|
|
|
|
Parent string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DiffPage struct {
|
|
|
|
Content string
|
|
|
|
Diff template.HTML
|
|
|
|
}
|
|
|
|
|
|
|
|
type Revision struct {
|
|
|
|
Version string
|
|
|
|
Page DiffPage
|
|
|
|
Summary string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Change struct {
|
|
|
|
Page string
|
|
|
|
Date time.Time
|
|
|
|
EndDate time.Time
|
|
|
|
Body string
|
|
|
|
Count int
|
|
|
|
|
|
|
|
DateSummary string
|
|
|
|
TimeSummary string
|
|
|
|
}
|
|
|
|
|
|
|
|
type PagesRepository interface {
|
|
|
|
Get(p string) Page
|
|
|
|
Save(p string, page Page, summary, author string) error
|
|
|
|
Exist(p string) bool
|
|
|
|
PageHistory(p string) ([]Revision, error)
|
|
|
|
RecentChanges() ([]Change, error)
|
|
|
|
AllPages() ([]Page, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type pageBaseInfo struct {
|
|
|
|
BaseURL string
|
|
|
|
RedirectURI string
|
|
|
|
Days []Day
|
|
|
|
Month string
|
|
|
|
Year int
|
|
|
|
PrevMonth string
|
|
|
|
NextMonth string
|
|
|
|
}
|
|
|
|
|
|
|
|
type indexPage struct {
|
|
|
|
pageBaseInfo
|
|
|
|
Session *Session
|
|
|
|
Title string
|
|
|
|
Name string
|
|
|
|
Content template.HTML
|
|
|
|
Backrefs map[string][]Backref
|
|
|
|
ShowGraph bool
|
|
|
|
TodayPage string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
Label string `json:"label"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Color *string `json:"color"`
|
|
|
|
Opacity float64 `json:"opacity"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Edge struct {
|
|
|
|
From int `json:"from"`
|
|
|
|
To int `json:"to"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type graphPage struct {
|
|
|
|
pageBaseInfo
|
|
|
|
Session *Session
|
|
|
|
Title string
|
|
|
|
Name string
|
|
|
|
Nodes template.JS
|
|
|
|
Edges template.JS
|
|
|
|
}
|
|
|
|
|
|
|
|
type Parent struct {
|
|
|
|
Text string
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type editPage struct {
|
|
|
|
pageBaseInfo
|
|
|
|
Session *Session
|
|
|
|
Title string
|
|
|
|
TitleHTML template.HTML
|
|
|
|
Content string
|
|
|
|
Name string
|
|
|
|
Editor template.HTML
|
|
|
|
Backrefs map[string][]Backref
|
|
|
|
ShowGraph bool
|
|
|
|
TodayPage string
|
|
|
|
Parent Parent
|
|
|
|
Parents []Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
type historyPage struct {
|
|
|
|
pageBaseInfo
|
|
|
|
Session *Session
|
|
|
|
Title string
|
|
|
|
Name string
|
|
|
|
History []Revision
|
|
|
|
}
|
|
|
|
|
|
|
|
type recentPage struct {
|
|
|
|
pageBaseInfo
|
|
|
|
Session *Session
|
|
|
|
Title string
|
|
|
|
Name string
|
|
|
|
Recent []Change
|
|
|
|
}
|
|
|
|
|
|
|
|
var baseTemplate = []string{"templates/layout.html", "templates/sidebar.html", "templates/sidebar-right.html", "templates/calendar.html"}
|
|
|
|
|
|
|
|
type indexHandler struct{}
|
|
|
|
type graphHandler struct{}
|
|
|
|
type saveHandler struct {
|
|
|
|
SearchIndex bleve.Index
|
|
|
|
}
|
|
|
|
type editHandler struct{}
|
|
|
|
type historyHandler struct{}
|
|
|
|
type recentHandler struct{}
|
|
|
|
type authHandler struct{}
|
|
|
|
|
|
|
|
func (*authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
if r.URL.Path == "/auth/login" {
|
|
|
|
templates := []string{"templates/layout_no_sidebar.html"}
|
|
|
|
templates = append(templates, "templates/login.html")
|
|
|
|
t, err := template.ParseFiles(templates...)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = t.Execute(w, nil)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if r.URL.Path == "/auth/callback" {
|
|
|
|
code := r.URL.Query().Get("code")
|
|
|
|
state := r.URL.Query().Get("state")
|
|
|
|
|
|
|
|
if state != sess.State {
|
|
|
|
log.Printf("mismatched state: %s != %s", state, sess.State)
|
|
|
|
http.Error(w, "mismatched state", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
authURL := sess.AuthorizationEndpoint
|
|
|
|
|
|
|
|
verified, response, err := indieauth.VerifyAuthCode(*baseurl, code, redirectURI, authURL)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !verified {
|
|
|
|
http.Redirect(w, r, "/auth/login", 302)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sess.Me = response.Me
|
|
|
|
sess.LoggedIn = true
|
|
|
|
sess.State = ""
|
|
|
|
|
|
|
|
http.Redirect(w, r, sess.NextURI, 302)
|
|
|
|
return
|
|
|
|
} else if r.URL.Path == "/auth/logout" {
|
|
|
|
templates := baseTemplate
|
|
|
|
templates = append(templates, "templates/logout.html")
|
|
|
|
t, err := template.ParseFiles(templates...)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = t.Execute(w, nil)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if r.Method == http.MethodPost {
|
|
|
|
if r.URL.Path == "/auth/login" {
|
|
|
|
r.ParseForm()
|
|
|
|
urlString := r.PostFormValue("url")
|
|
|
|
|
|
|
|
u, err := url.Parse(urlString)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints, err := indieauth.GetEndpoints(u)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
authURL, err := url.Parse(endpoints.AuthorizationEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
state := util.RandStringBytes(32)
|
|
|
|
|
|
|
|
sess.AuthorizationEndpoint = authURL.String()
|
|
|
|
sess.Me = urlString
|
|
|
|
sess.LoggedIn = false
|
|
|
|
sess.RedirectURI = redirectURI
|
|
|
|
sess.NextURI = "/"
|
|
|
|
sess.State = state
|
|
|
|
|
|
|
|
newURL := indieauth.CreateAuthenticationURL(*authURL, urlString, *baseurl, redirectURI, state)
|
|
|
|
|
|
|
|
http.Redirect(w, r, newURL, 302)
|
|
|
|
return
|
|
|
|
} else if r.URL.Path == "/auth/logout" {
|
|
|
|
sess.LoggedIn = false
|
|
|
|
sess.Me = ""
|
|
|
|
sess.AuthorizationEndpoint = ""
|
|
|
|
http.Redirect(w, r, "/", 302)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *historyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if !sess.LoggedIn {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.ParseForm()
|
|
|
|
page := r.URL.Path[9:]
|
|
|
|
page = resolvePageName(page)
|
|
|
|
|
|
|
|
history, err := mp.PageHistory(page)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
funcs := template.FuncMap{
|
|
|
|
"historyIndex": func(x int, history []Revision) int { return len(history) - x },
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := template.New("layout.html").Funcs(funcs).ParseFiles("templates/layout.html", "templates/history.html")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pageBase := getPageBase(mp, time.Now())
|
|
|
|
pageData := historyPage{
|
|
|
|
pageBaseInfo: pageBase,
|
|
|
|
Session: sess,
|
|
|
|
Title: "History of " + cleanTitle(page),
|
|
|
|
Name: page,
|
|
|
|
History: history,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.Execute(w, pageData)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func daysInMonth(year int, month time.Month) int {
|
|
|
|
return time.Date(year, month, 1, 0, 0, 0, 0, time.UTC).AddDate(0, 1, -1).Day()
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareDays(repo PagesRepository, t time.Time) []Day {
|
|
|
|
today := time.Now()
|
|
|
|
|
|
|
|
var days []Day
|
|
|
|
|
|
|
|
curDate := t.AddDate(0, 0, -(t.Day() - 1))
|
|
|
|
preDays := int(curDate.Weekday())
|
|
|
|
if preDays == 0 {
|
|
|
|
preDays = 7
|
|
|
|
}
|
|
|
|
preDays -= 1
|
|
|
|
|
|
|
|
maxDays := daysInMonth(curDate.Year(), curDate.Month())
|
|
|
|
|
|
|
|
endOfMonth := false
|
|
|
|
|
|
|
|
for i := 0; i < 6; i++ {
|
|
|
|
year, week := curDate.ISOWeek()
|
|
|
|
days = append(days, Day{
|
|
|
|
Class: "week day",
|
|
|
|
Text: fmt.Sprintf("%02d", week),
|
|
|
|
URL: fmt.Sprintf("/edit/%s", formatWeekPageName(year, week)),
|
|
|
|
Count: "",
|
|
|
|
})
|
|
|
|
|
|
|
|
for d := 0; d < 7; d++ {
|
|
|
|
day := -preDays + (i * 7) + 1 + d
|
|
|
|
fday := fmt.Sprintf("%d", day)
|
|
|
|
if day <= 0 {
|
|
|
|
fday = ""
|
|
|
|
}
|
|
|
|
if day > maxDays {
|
|
|
|
fday = ""
|
|
|
|
endOfMonth = true
|
|
|
|
}
|
|
|
|
class := "day"
|
|
|
|
if timeEqual(curDate, today) {
|
|
|
|
class += " today"
|
|
|
|
}
|
|
|
|
if timeEqual(curDate, t) {
|
|
|
|
class += " current"
|
|
|
|
}
|
|
|
|
pageName := formatDatePageName(curDate)
|
|
|
|
if fday != "" && pageHasContent(mp, pageName) {
|
|
|
|
class += " has-content"
|
|
|
|
}
|
|
|
|
days = append(days, Day{
|
|
|
|
Class: class,
|
|
|
|
Text: fday,
|
|
|
|
URL: fmt.Sprintf("/edit/%s", pageName),
|
|
|
|
Count: "",
|
|
|
|
})
|
|
|
|
if fday != "" {
|
|
|
|
curDate = curDate.AddDate(0, 0, 1)
|
|
|
|
}
|
|
|
|
if day == maxDays {
|
|
|
|
endOfMonth = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if endOfMonth {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return days
|
|
|
|
}
|
|
|
|
|
|
|
|
func pageHasContent(repo PagesRepository, day string) bool {
|
|
|
|
return repo.Exist(day)
|
|
|
|
// page := repo.Get(day)
|
|
|
|
// return page.Blocks.Children != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatWeekPageName(year int, week int) string {
|
|
|
|
return fmt.Sprintf("%04dW%02d", year, week)
|
|
|
|
}
|
|
|
|
|
|
|
|
func timeEqual(date time.Time, today time.Time) bool {
|
|
|
|
if date.Year() != today.Year() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if date.Month() != today.Month() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if date.Day() != today.Day() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPageBase(repo PagesRepository, t time.Time) pageBaseInfo {
|
|
|
|
clientID := *baseurl
|
|
|
|
|
|
|
|
pageBase := pageBaseInfo{
|
|
|
|
BaseURL: clientID,
|
|
|
|
RedirectURI: redirectURI,
|
|
|
|
Days: prepareDays(repo, t),
|
|
|
|
Month: t.Month().String(),
|
|
|
|
Year: t.Year(),
|
|
|
|
}
|
|
|
|
|
|
|
|
pageBase.PrevMonth = t.AddDate(0, -1, -t.Day()+1).Format("2006-01-02")
|
|
|
|
pageBase.NextMonth = t.AddDate(0, 1, -t.Day()+1).Format("2006-01-02")
|
|
|
|
|
|
|
|
return pageBase
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *saveHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if !sess.LoggedIn {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isJson := r.PostForm.Get("json") == "1"
|
|
|
|
page := r.PostForm.Get("p")
|
|
|
|
summary := r.PostForm.Get("summary")
|
|
|
|
|
|
|
|
pageData := Page{Content: r.PostForm.Get("content")}
|
|
|
|
err = mp.Save(page, pageData, summary, sess.Me)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if isJson {
|
|
|
|
fmt.Print(w, "{}")
|
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, "/"+page, http.StatusFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if !sess.LoggedIn {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
page := r.URL.Path[6:]
|
|
|
|
page = resolvePageName(page)
|
|
|
|
|
|
|
|
mpPage := mp.Get(page)
|
|
|
|
pageText := mpPage.Content
|
|
|
|
|
|
|
|
if pageText == "" {
|
|
|
|
pageText = "[]"
|
|
|
|
}
|
|
|
|
|
|
|
|
var rawMsg json.RawMessage
|
|
|
|
err = json.NewDecoder(strings.NewReader(pageText)).Decode(&rawMsg)
|
|
|
|
jsonEditor := pageText != "" && err == nil
|
|
|
|
|
|
|
|
var editor template.HTML
|
|
|
|
if jsonEditor {
|
|
|
|
editor, err = renderEditor(page, pageText, "json", *baseurl)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
editor = template.HTML(fmt.Sprintf(`
|
|
|
|
<textarea name="content" rows="24" style="width:100%%">%s</textarea>
|
|
|
|
<br>
|
|
|
|
|
|
|
|
<div class="field">
|
|
|
|
<label class="label">Summary</label>
|
|
|
|
<div class="control">
|
|
|
|
<input type="text" name="summary" class="input" placeholder="Summary"/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<button class="button is-primary" type="submit">Save</button>
|
|
|
|
`, html.EscapeString(pageText)))
|
|
|
|
}
|
|
|
|
|
|
|
|
curDate, err := ParseDatePageName(page)
|
|
|
|
if err != nil {
|
|
|
|
curDate = time.Now()
|
|
|
|
}
|
|
|
|
pageBase := getPageBase(mp, curDate)
|
|
|
|
title := cleanTitle(mpPage.Title)
|
|
|
|
if newTitle, err := PageTitle(pageText); err == nil {
|
|
|
|
title = newTitle
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent Parent
|
|
|
|
parent.ID = mpPage.Parent
|
|
|
|
parent.Text = mpPage.Blocks.Texts[parent.ID]
|
|
|
|
|
|
|
|
var parents []Parent
|
|
|
|
for _, p := range mpPage.Blocks.Parents {
|
|
|
|
var parent Parent
|
|
|
|
parent.ID = p
|
|
|
|
parent.Text = mpPage.Blocks.Texts[p]
|
|
|
|
parents = append(parents, parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, j := 0, len(parents)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
parents[i], parents[j] = parents[j], parents[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlTitle := link.FormatHtmlTitle(title)
|
|
|
|
|
|
|
|
data := editPage{
|
|
|
|
pageBaseInfo: pageBase,
|
|
|
|
Session: sess,
|
|
|
|
Title: title,
|
|
|
|
TitleHTML: htmlTitle,
|
|
|
|
Content: pageText,
|
|
|
|
Editor: editor,
|
|
|
|
Name: page,
|
|
|
|
Backrefs: mpPage.Refs,
|
|
|
|
TodayPage: "Today",
|
|
|
|
ShowGraph: page != "Daily_Notes",
|
|
|
|
Parent: parent,
|
|
|
|
Parents: parents,
|
|
|
|
}
|
|
|
|
templates := baseTemplate
|
|
|
|
templates = append(templates, "templates/edit.html")
|
|
|
|
t, err := template.ParseFiles(templates...)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=600")
|
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *graphHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
sess, err := NewSession(w, r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if !sess.LoggedIn {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gb, err := NewGraphBuilder(mp)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gb.prepareNodeMap()
|
|
|
|
|
|
|
|
gb.RemoveNode("DONE")
|
|
|
|
gb.RemoveNode("TODO")
|
|
|
|
gb.RemoveNode("Home")
|
|
|
|
gb.RemoveNode("Projects")
|
|
|
|
gb.RemoveNode("Notes")
|
|
|
|
gb.RemoveNode("Books")
|
|
|
|
gb.RemoveNode("Daily_Notes")
|
|
|
|
gb.RemoveNode("NewJsonTest")
|
|
|
|
gb.RemoveNodeWithSuffix("_2020")
|
|
|
|
|
|
|
|
err = gb.prepareGraph()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodesBuf bytes.Buffer
|
|
|
|
var edgesBuf bytes.Buffer
|
|
|
|
|
|
|
|
err = json.NewEncoder(&nodesBuf).Encode(&gb.nodes)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(&edgesBuf).Encode(&gb.edges)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pageBase := getPageBase(mp, time.Now())
|
|
|
|
data := graphPage{
|
|
|
|
pageBaseInfo: pageBase,
|
|
|
|
Session: sess,
|
|
|
|
Title: "Graph",
|
|
|
|
Name: "Graph",
|
|
|
|
Nodes: template.JS(nodesBuf.String()),
|
|
|
|
Edges: template.JS(edgesBuf.String()),
|
|
|
|
}
|
|
|
|
templates := baseTemplate
|
|
|
|
templates = append(templates, "templates/graph.html")
|
|
|
|
t, err := template.ParseFiles(templates...)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := sess.Flush(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if !sess.LoggedIn {
|
|
|
|
http.Redirect(w, r, "/auth/login", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
format := r.URL.Query().Get("format")
|
|
|
|
if format == "" {
|
|
|
|
format = "html"
|
|
|
|
}
|
|
|
|
|
|
|
|
if !(format == "html" || format == "markdown" || format == "hmarkdown" || format == "json" || format == "metakv") {
|
|
|
|
http.Error(w, "unknown format", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
page := r.URL.Path[1:]
|
|
|
|
if page == "favicon.ico" {
|
|
|
|
http.Error(w, "Not Found", 404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
page = resolvePageName(page)
|
|
|
|
|
|
|
|
mpPage := mp.Get(page)
|
|
|
|
pageText := mpPage.Content
|
|
|
|
if (format == "" || format == "html") && pageText == "" {
|
|
|
|
http.Redirect(w, r, "/edit/"+page, 302)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pageText == "" {
|
|
|
|
pageText = "[]"
|
|
|
|
}
|
|
|
|
|
|
|
|
var rawMsg json.RawMessage
|
|
|
|
err = json.NewDecoder(strings.NewReader(pageText)).Decode(&rawMsg)
|
|
|
|
|
|
|
|
title := cleanTitle(mpPage.Title)
|
|
|
|
|
|
|
|
jsonPage := pageText != "" && err == nil
|
|
|
|
if jsonPage {
|
|
|
|
if format == "json" {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
|
|
// 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 := createStructuredFormat(mpPage)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(strings.NewReader(pageText)).Decode(&listItems)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
builder := strings.Builder{}
|
|
|
|
if format == "markdown" || format == "html" {
|
|
|
|
for _, item := range listItems {
|
|
|
|
lines := strings.Split(item.Text, "\n")
|
|
|
|
if len(lines) > 1 {
|
|
|
|
first := true
|
|
|
|
for _, line := range lines {
|
|
|
|
if first {
|
|
|
|
builder.WriteString(strings.Repeat(" ", item.Indented))
|
|
|
|
builder.WriteString("* ")
|
|
|
|
builder.WriteString(line)
|
|
|
|
builder.WriteByte('\n')
|
|
|
|
first = false
|
|
|
|
} else {
|
|
|
|
builder.WriteString(strings.Repeat(" ", item.Indented+1))
|
|
|
|
builder.WriteString(line)
|
|
|
|
builder.WriteByte('\n')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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)
|
|
|
|
builder.WriteByte('\n')
|
|
|
|
}
|
|