diff --git a/cmd/note-cli/main.go b/cmd/note-cli/main.go new file mode 100644 index 0000000..296d521 --- /dev/null +++ b/cmd/note-cli/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "path/filepath" + "strconv" + + "git.sr.ht/~ewintr/shitty-ssg/cmd/note-cli/note" +) + +func main() { + notesPath := os.Getenv("NOTES_PATH") + if notesPath == "" { + log.Fatal("no notes directory to parse") + } + + if len(os.Args) != 2 { + log.Fatal("exactly one search term is required as parameter") + } + searchTerm := os.Args[1] + + var notes note.Notes + if err := filepath.Walk(notesPath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if !info.IsDir() && filepath.Ext(path) == ".adoc" { + if err := notes.AddFileNote(path); err != nil { + return nil + } + } + + return nil + }); err != nil { + log.Fatal(err) + } + + notes = notes.FilterByTerm(searchTerm) + if len(notes) == 0 { + fmt.Println("Found nothing.") + + return + } + + for i, n := range notes { + fmt.Printf("%d) %s\n", i, n.Title) + } + reader := bufio.NewReader(os.Stdin) + r, _, err := reader.ReadRune() + if err != nil { + log.Fatal(err) + } + c := string(r) + switch c { + case "q": + return + default: + i, err := strconv.Atoi(c) + if err != nil { + log.Fatal(err) + } + if i < 0 || i >= len(notes) { + fmt.Println("number out of range") + return + } + fmt.Printf("\n\n%s\n\n%s\n\n", notes[i].Title, notes[i].Content) + } +} diff --git a/cmd/note-cli/note/note.go b/cmd/note-cli/note/note.go new file mode 100644 index 0000000..19b7cdd --- /dev/null +++ b/cmd/note-cli/note/note.go @@ -0,0 +1,76 @@ +package note + +import ( + "strings" + + "git.sr.ht/~ewintr/shitty-ssg/pkg/adoc" +) + +type Kind string + +type Tag string + +const ( + KIND_NOTE = Kind("note") + KIND_PRIVATE_NOTE = Kind("private_note") + KIND_WORK_NOTE = Kind("work_note") + KIND_INVALID = Kind("") +) + +func mapKind(akind adoc.Kind) Kind { + nkind, ok := map[adoc.Kind]Kind{ + adoc.KIND_NOTE: KIND_NOTE, + adoc.KIND_PRIVATE_NOTE: KIND_PRIVATE_NOTE, + adoc.KIND_WORK_NOTE: KIND_WORK_NOTE, + }[akind] + if !ok { + return KIND_INVALID + } + + return nkind +} + +type Note struct { + doc *adoc.ADoc + Title string + Kind Kind + Tags []Tag + Content string +} + +func NewNote(doc *adoc.ADoc) *Note { + var paragraphs []string + for _, be := range doc.Content { + paragraphs = append(paragraphs, be.Text()) + } + content := strings.Join(paragraphs, "\n\n") + + var tags []Tag + for _, t := range doc.Tags { + tags = append(tags, Tag(t)) + } + + return &Note{ + doc: doc, + Kind: mapKind(doc.Kind), + Title: doc.Title, + Tags: tags, + Content: content, + } +} + +func (n *Note) Contains(term string) bool { + for _, t := range n.Tags { + if strings.ToLower(string(t)) == strings.ToLower(term) { + return true + } + } + + for _, w := range strings.Split(n.Content, " ") { + if strings.ToLower(w) == strings.ToLower(term) { + return true + } + } + + return false +} diff --git a/cmd/note-cli/note/notes.go b/cmd/note-cli/note/notes.go new file mode 100644 index 0000000..fc0d018 --- /dev/null +++ b/cmd/note-cli/note/notes.go @@ -0,0 +1,33 @@ +package note + +import ( + "io/ioutil" + + "git.sr.ht/~ewintr/shitty-ssg/pkg/adoc" +) + +type Notes []*Note + +func (n *Notes) AddFileNote(fPath string) error { + content, err := ioutil.ReadFile(fPath) + if err != nil { + return err + } + note := NewNote(adoc.New(string(content))) + if note.Kind != KIND_INVALID { + *n = append(*n, note) + } + + return nil +} + +func (n *Notes) FilterByTerm(term string) Notes { + found := Notes{} + for _, note := range *n { + if note.Contains(term) { + found = append(found, note) + } + } + + return found +} diff --git a/resources/ewnl/font/Amiko-Bold.ttf b/cmd/ssg/resources/ewnl/font/Amiko-Bold.ttf similarity index 100% rename from resources/ewnl/font/Amiko-Bold.ttf rename to cmd/ssg/resources/ewnl/font/Amiko-Bold.ttf diff --git a/resources/ewnl/font/Amiko-Regular.ttf b/cmd/ssg/resources/ewnl/font/Amiko-Regular.ttf similarity index 100% rename from resources/ewnl/font/Amiko-Regular.ttf rename to cmd/ssg/resources/ewnl/font/Amiko-Regular.ttf diff --git a/resources/ewnl/font/Amiko-SemiBold.ttf b/cmd/ssg/resources/ewnl/font/Amiko-SemiBold.ttf similarity index 100% rename from resources/ewnl/font/Amiko-SemiBold.ttf rename to cmd/ssg/resources/ewnl/font/Amiko-SemiBold.ttf diff --git a/resources/ewnl/font/Merriweather-Regular.otf b/cmd/ssg/resources/ewnl/font/Merriweather-Regular.otf similarity index 100% rename from resources/ewnl/font/Merriweather-Regular.otf rename to cmd/ssg/resources/ewnl/font/Merriweather-Regular.otf diff --git a/resources/ewnl/font/merriweather.regular.ttf b/cmd/ssg/resources/ewnl/font/merriweather.regular.ttf similarity index 100% rename from resources/ewnl/font/merriweather.regular.ttf rename to cmd/ssg/resources/ewnl/font/merriweather.regular.ttf diff --git a/resources/ewnl/template/archive.gohtml b/cmd/ssg/resources/ewnl/template/archive.gohtml similarity index 100% rename from resources/ewnl/template/archive.gohtml rename to cmd/ssg/resources/ewnl/template/archive.gohtml diff --git a/resources/ewnl/template/head.gohtml b/cmd/ssg/resources/ewnl/template/head.gohtml similarity index 100% rename from resources/ewnl/template/head.gohtml rename to cmd/ssg/resources/ewnl/template/head.gohtml diff --git a/resources/ewnl/template/list.gohtml b/cmd/ssg/resources/ewnl/template/list.gohtml similarity index 100% rename from resources/ewnl/template/list.gohtml rename to cmd/ssg/resources/ewnl/template/list.gohtml diff --git a/resources/ewnl/template/menu.gohtml b/cmd/ssg/resources/ewnl/template/menu.gohtml similarity index 100% rename from resources/ewnl/template/menu.gohtml rename to cmd/ssg/resources/ewnl/template/menu.gohtml diff --git a/resources/ewnl/template/post.gohtml b/cmd/ssg/resources/ewnl/template/post.gohtml similarity index 100% rename from resources/ewnl/template/post.gohtml rename to cmd/ssg/resources/ewnl/template/post.gohtml diff --git a/resources/ewnl/template/rss.goxml b/cmd/ssg/resources/ewnl/template/rss.goxml similarity index 100% rename from resources/ewnl/template/rss.goxml rename to cmd/ssg/resources/ewnl/template/rss.goxml diff --git a/resources/ewnl/template/static.gohtml b/cmd/ssg/resources/ewnl/template/static.gohtml similarity index 100% rename from resources/ewnl/template/static.gohtml rename to cmd/ssg/resources/ewnl/template/static.gohtml diff --git a/resources/vkvnl/template/post.gohtml b/cmd/ssg/resources/vkvnl/template/post.gohtml similarity index 100% rename from resources/vkvnl/template/post.gohtml rename to cmd/ssg/resources/vkvnl/template/post.gohtml diff --git a/resources/vkvnl/template/rss.goxml b/cmd/ssg/resources/vkvnl/template/rss.goxml similarity index 100% rename from resources/vkvnl/template/rss.goxml rename to cmd/ssg/resources/vkvnl/template/rss.goxml diff --git a/pkg/adoc/adoc.go b/pkg/adoc/adoc.go index e562123..7d41625 100644 --- a/pkg/adoc/adoc.go +++ b/pkg/adoc/adoc.go @@ -6,15 +6,16 @@ import ( ) const ( - KIND_NOTE = Kind("note") - KIND_VKV = Kind("vkv") - KIND_STORY = Kind("story") - KIND_SNIPPET = Kind("snippet") - KIND_ESSAY = Kind("essay") - KIND_WORK = Kind("work") - KIND_ARTICLE = Kind("article") - KIND_TUTORIAL = Kind("tutorial") - KIND_UNKNOWN = Kind("unknown") + KIND_NOTE = Kind("note") + KIND_PRIVATE_NOTE = Kind("private_note") + KIND_WORK_NOTE = Kind("work_note") + KIND_VKV = Kind("vkv") + KIND_STORY = Kind("story") + KIND_SNIPPET = Kind("snippet") + KIND_ESSAY = Kind("essay") + KIND_ARTICLE = Kind("article") + KIND_TUTORIAL = Kind("tutorial") + KIND_UNKNOWN = Kind("unknown") ) type Kind string @@ -28,7 +29,8 @@ func NewKind(text string) Kind { } for _, k := range []string{ - "note", "vkv", "story", "snippet", + "note", "private_note", "work_note", "vkv", + "story", "snippet", "essay", "tutorial", "work", "article", } { if k == text {