Improve saving of pages for blocks
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Peter Stuifzand 2021-08-13 00:41:26 +02:00
parent 7199e0c1da
commit 71838ee0af
3 changed files with 30 additions and 12 deletions

29
file.go
View File

@ -34,6 +34,7 @@ import (
"time" "time"
"github.com/blevesearch/bleve" "github.com/blevesearch/bleve"
"github.com/davecgh/go-spew/spew"
"github.com/sergi/go-diff/diffmatchpatch" "github.com/sergi/go-diff/diffmatchpatch"
) )
@ -281,8 +282,14 @@ func (fp *FilePages) save(msg saveMessage) error {
summary := msg.summary summary := msg.summary
author := msg.author author := msg.author
if p[0] != '_' {
page.Name = strings.Replace(p, " ", "_", -1) page.Name = strings.Replace(p, " ", "_", -1)
page.Title = strings.Replace(p, "_", " ", -1) page.Title = strings.Replace(p, "_", " ", -1)
} else {
page.Name = p
page.Title = p
}
spew.Dump(page)
sw.Start("create blocks") sw.Start("create blocks")
err := saveBlocksFromPage(fp.dirname, page) err := saveBlocksFromPage(fp.dirname, page)
@ -365,7 +372,7 @@ func saveWithNewIDs(dirname string, listItems []ListItemV2, pageName string) ([]
} }
func saveBlocksFromPage(dirname string, page Page) error { func saveBlocksFromPage(dirname string, page Page) error {
log.Println("Processing: ", page.Name) log.Printf("Processing: %q\n", page.Name)
var listItems []ListItem var listItems []ListItem
err := json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems) err := json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems)
if err != nil { if err != nil {
@ -394,6 +401,7 @@ func saveBlocksFromPage(dirname string, page Page) error {
title = parentBlock.Text title = parentBlock.Text
} }
rootParentID := page.Name
var parent = ListItem{ var parent = ListItem{
Text: title, Text: title,
Indented: -1, Indented: -1,
@ -427,7 +435,11 @@ func saveBlocksFromPage(dirname string, page Page) error {
} }
} }
blocks[item.ID] = Block{item.Text, []string{}, parent.ID} blocks[item.ID] = Block{
Text: item.Text,
Children: []string{},
Parent: parent.ID,
}
if block, e := blocks[parent.ID]; e { if block, e := blocks[parent.ID]; e {
block.Children = append(block.Children, item.ID) block.Children = append(block.Children, item.ID)
blocks[parent.ID] = block blocks[parent.ID] = block
@ -437,17 +449,18 @@ func saveBlocksFromPage(dirname string, page Page) error {
prev = &listItems[i] prev = &listItems[i]
} }
log.Printf("Loading parent block: %s", parent.ID) // TODO: found out if this is still necessary
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, parent.ID)) log.Printf("Loading parent block: %q", rootParentID)
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, rootParentID))
if err == nil { if err == nil {
var parentBlock Block var parentBlock Block
err = json.NewDecoder(f).Decode(&parentBlock) err = json.NewDecoder(f).Decode(&parentBlock)
if err == nil { if err == nil {
if pb, e := blocks[parent.ID]; e { if pb, e := blocks[rootParentID]; e {
pb.Text = parentBlock.Text pb.Text = parentBlock.Text
pb.Parent = parentBlock.Parent pb.Parent = parentBlock.Parent
blocks[parent.ID] = pb blocks[rootParentID] = pb
log.Printf("Text=%s, Parent=%s", parentBlock.Text, parentBlock.Parent) log.Printf("Text=%q, Parent=%q", parentBlock.Text, parentBlock.Parent)
} }
} }
f.Close() f.Close()
@ -457,7 +470,7 @@ func saveBlocksFromPage(dirname string, page Page) error {
} }
for id, block := range blocks { for id, block := range blocks {
log.Println("Writing to ", id) log.Printf("Writing to %q\n", id)
f, err := os.OpenFile(filepath.Join(dirname, BlocksDirectory, id), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) f, err := os.OpenFile(filepath.Join(dirname, BlocksDirectory, id), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil { if err != nil {
log.Println(err) log.Println(err)

View File

@ -39,6 +39,7 @@ import (
"github.com/blevesearch/bleve" "github.com/blevesearch/bleve"
"p83.nl/go/ekster/pkg/util" "p83.nl/go/ekster/pkg/util"
"p83.nl/go/indieauth" "p83.nl/go/indieauth"
"p83.nl/go/wiki/link"
) )
func init() { func init() {
@ -169,6 +170,7 @@ type editPage struct {
pageBaseInfo pageBaseInfo
Session *Session Session *Session
Title string Title string
TitleHTML template.HTML
Content string Content string
Name string Name string
Editor template.HTML Editor template.HTML
@ -388,7 +390,7 @@ func prepareDays(repo PagesRepository, t time.Time) []Day {
var days []Day var days []Day
curDate := t.AddDate(0, 0, -(t.Day()-1)) curDate := t.AddDate(0, 0, -(t.Day() - 1))
preDays := int(curDate.Weekday()) preDays := int(curDate.Weekday())
if preDays == 0 { if preDays == 0 {
preDays = 7 preDays = 7
@ -609,10 +611,13 @@ func (h *editHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
parents[i], parents[j] = parents[j], parents[i] parents[i], parents[j] = parents[j], parents[i]
} }
htmlTitle := link.FormatHtmlTitle(title)
data := editPage{ data := editPage{
pageBaseInfo: pageBase, pageBaseInfo: pageBase,
Session: sess, Session: sess,
Title: title, Title: title,
TitleHTML: htmlTitle,
Content: pageText, Content: pageText,
Editor: editor, Editor: editor,
Name: page, Name: page,

View File

@ -13,7 +13,7 @@
{{ end }} {{ end }}
</ol> </ol>
</nav> </nav>
<h1 class="title">{{ .Title }}</h1> <h1 class="title">{{ .TitleHTML }}</h1>
<form action="/save/" method="post"> <form action="/save/" method="post">
<input type="hidden" name="p" value="{{ .Name }}"/> <input type="hidden" name="p" value="{{ .Name }}"/>
{{ .Editor }} {{ .Editor }}