2021-04-14 18:14:01 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2021-04-18 14:37:54 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-04-14 18:14:01 +00:00
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NoteFormatter formats notes to be printed on the screen.
|
|
|
|
type NoteFormatter func(note ContextualNote) (string, error)
|
|
|
|
|
2021-06-20 19:08:18 +00:00
|
|
|
func newNoteFormatter(basePath string, template Template, linkFormatter LinkFormatter, env map[string]string, fs FileStorage) (NoteFormatter, error) {
|
2021-04-14 18:14:01 +00:00
|
|
|
termRepl, err := template.Styler().Style("$1", StyleTerm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(note ContextualNote) (string, error) {
|
2022-01-10 13:28:14 +00:00
|
|
|
path := NotebookPath{
|
|
|
|
Path: note.Path,
|
|
|
|
BasePath: basePath,
|
|
|
|
WorkingDir: fs.WorkingDir(),
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
2022-01-10 13:28:14 +00:00
|
|
|
relPath, err := path.PathRelToWorkingDir()
|
2021-07-14 14:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
snippets := make([]string, 0)
|
|
|
|
for _, snippet := range note.Snippets {
|
|
|
|
snippets = append(snippets, noteTermRegex.ReplaceAllString(snippet, termRepl))
|
|
|
|
}
|
|
|
|
|
|
|
|
return template.Render(noteFormatRenderContext{
|
2021-10-23 17:24:46 +00:00
|
|
|
Filename: note.Filename(),
|
|
|
|
FilenameStem: note.FilenameStem(),
|
2022-01-10 13:28:14 +00:00
|
|
|
Path: relPath,
|
|
|
|
AbsPath: path.AbsPath(),
|
2021-10-23 17:24:46 +00:00
|
|
|
Title: note.Title,
|
2021-04-18 14:37:54 +00:00
|
|
|
Link: newLazyStringer(func() string {
|
2022-01-10 13:28:14 +00:00
|
|
|
context, err := NewLinkFormatterContext(path, note.Title, note.Metadata)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
link, _ := linkFormatter(context)
|
2021-04-18 14:37:54 +00:00
|
|
|
return link
|
|
|
|
}),
|
2021-04-14 18:14:01 +00:00
|
|
|
Lead: note.Lead,
|
|
|
|
Body: note.Body,
|
|
|
|
Snippets: snippets,
|
|
|
|
Tags: note.Tags,
|
|
|
|
RawContent: note.RawContent,
|
|
|
|
WordCount: note.WordCount,
|
|
|
|
Metadata: note.Metadata,
|
|
|
|
Created: note.Created,
|
|
|
|
Modified: note.Modified,
|
|
|
|
Checksum: note.Checksum,
|
2021-06-20 19:08:18 +00:00
|
|
|
Env: env,
|
2021-04-14 18:14:01 +00:00
|
|
|
})
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var noteTermRegex = regexp.MustCompile(`<zk:match>(.*?)</zk:match>`)
|
|
|
|
|
|
|
|
// noteFormatRenderContext holds the variables available to the note formatting
|
|
|
|
// templates.
|
|
|
|
type noteFormatRenderContext struct {
|
2021-10-23 17:24:46 +00:00
|
|
|
Filename string `json:"filename"`
|
|
|
|
FilenameStem string `json:"filenameStem" handlebars:"filename-stem"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
AbsPath string `json:"absPath" handlebars:"abs-path"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Link fmt.Stringer `json:"link"`
|
|
|
|
Lead string `json:"lead"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
Snippets []string `json:"snippets"`
|
|
|
|
RawContent string `json:"rawContent" handlebars:"raw-content"`
|
|
|
|
WordCount int `json:"wordCount" handlebars:"word-count"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
Metadata map[string]interface{} `json:"metadata"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Modified time.Time `json:"modified"`
|
|
|
|
Checksum string `json:"checksum"`
|
|
|
|
Env map[string]string `json:"-"`
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
2021-04-18 14:37:54 +00:00
|
|
|
|
|
|
|
func (c noteFormatRenderContext) Equal(other noteFormatRenderContext) bool {
|
|
|
|
json1, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
json2, err := json.Marshal(other)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return string(json1) == string(json2)
|
|
|
|
}
|