From 89fbb17ab7d9aa4d93c67ae09587f1a7418dbdb3 Mon Sep 17 00:00:00 2001 From: ChaoticByte Date: Fri, 11 Jul 2025 21:32:16 +0200 Subject: [PATCH] Require .txt filename extension for entries, allow defining entry title via Title: ... in first line of the file, update examples --- README.md | 17 +++++++- database.go | 40 +++++++++++++++---- entries/{Another Entry => another-entry.txt} | 2 + entries/{Example Entry => example-entry.txt} | 2 + entries/only-title.txt | 1 + ...et another entry => yet-another-entry.txt} | 2 +- 6 files changed, 53 insertions(+), 11 deletions(-) rename entries/{Another Entry => another-entry.txt} (97%) rename entries/{Example Entry => example-entry.txt} (54%) create mode 100644 entries/only-title.txt rename entries/{Yet another entry => yet-another-entry.txt} (99%) diff --git a/README.md b/README.md index 202e97d..7f6cd6c 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,25 @@ A very simple and clean reader/browser? for plaintext... documentation? wiki? ch ## How to use 1. Set your settings in `settings.go` -2. Modify (if you want) +2. Modify the application (if you want) 3. Compile `go get && go build -o server` -4. Place your content into `entries/` +4. Place your `.txt` files into `entries/` (see section "Formatting" below) 5. Run `./server` 6. put behind a proxy (or don't). +### Formatting + +The only formatting option you have: to define a title. + +``` +Title: A title + +here comes the entry body ... +``` + +It has to be defined in the first line of the text file. +If you don't define a title, the filename without the `.txt` part gets used as the title. + ## Requirements Developed and tested on Linux. Should work on other Unixoids. diff --git a/database.go b/database.go index c153c30..a8dcdd0 100644 --- a/database.go +++ b/database.go @@ -44,20 +44,44 @@ func (db *Database) search(query string) []string { // returns keys (entry names func BuildDB(directory string) Database { logger := log.Default() logger.Println("Building database") - // keys, entries - var keys []string + // files, entries + var files []string entries := map[string]string{} // get files in directory and read them directory = strings.TrimRight(directory, "/") // we don't need that last /, don't use the root directory / entriesDirFs := os.DirFS(directory) - keys, err := fs.Glob(entriesDirFs, "*") + files, err := fs.Glob(entriesDirFs, "*.txt") if err != nil { logger.Panicln(err) } - for _, k := range keys { - contentB, err := os.ReadFile(directory + "/" + k) + titles := []string{} + for _, f := range files { + fileData, err := os.ReadFile(directory + "/" + f) if err != nil { logger.Panicln(err) } - content := string(contentB) - entries[k] = content + content := string(fileData) + content = strings.Trim(content, "\n\r ") + if strings.HasPrefix(content, "Title:") { + len_content := len(content) + title_start_idx := 6 + var title_stop_idx int + if strings.Contains(content, "\n") { + title_stop_idx = strings.Index(content, "\n") + } else { + title_stop_idx = len_content + } + title := content[title_start_idx:title_stop_idx] + title = strings.Trim(title, " ") + body := content[title_stop_idx:len_content] + body = strings.TrimLeft(body, "\n\r") + if len(body) < 1 { + body = " " + } + titles = append(titles, title) + entries[title] = body + } else { + title := f[:len(f)-4] // remove ".txt" + titles = append(titles, title) + entries[title] = content + } } matcher := search.New(ContentLanguage, search.IgnoreCase, search.IgnoreDiacritics) - return Database{Keys: keys, Entries: entries, matcher: matcher} + return Database{Keys: titles, Entries: entries, matcher: matcher} } diff --git a/entries/Another Entry b/entries/another-entry.txt similarity index 97% rename from entries/Another Entry rename to entries/another-entry.txt index 57e285c..2fdfbe6 100644 --- a/entries/Another Entry +++ b/entries/another-entry.txt @@ -1 +1,3 @@ +Title: Another Entry + Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. diff --git a/entries/Example Entry b/entries/example-entry.txt similarity index 54% rename from entries/Example Entry rename to entries/example-entry.txt index 1915099..ccefa32 100644 --- a/entries/Example Entry +++ b/entries/example-entry.txt @@ -1 +1,3 @@ +Title: Example Entry + This is an example entry. diff --git a/entries/only-title.txt b/entries/only-title.txt new file mode 100644 index 0000000..da8e10b --- /dev/null +++ b/entries/only-title.txt @@ -0,0 +1 @@ +Title: very interesting entry \ No newline at end of file diff --git a/entries/Yet another entry b/entries/yet-another-entry.txt similarity index 99% rename from entries/Yet another entry rename to entries/yet-another-entry.txt index 79f0842..de0e3e1 100644 --- a/entries/Yet another entry +++ b/entries/yet-another-entry.txt @@ -5,4 +5,4 @@ When Aunt Em came there to live she was a young, pretty wife. The sun and wind h Uncle Henry never laughed. He worked hard from morning till night and did not know what joy was. He was gray also, from his long beard to his rough boots, and he looked stern and solemn, and rarely spoke. -It was Toto that made Dorothy laugh, and saved her from growing as gray as her other surroundings. Toto was not gray; he was a little black dog, with long, silky hair and small black eyes that twinkled merrily on either side of his funny, wee nose. Toto played all day long, and Dorothy played with him, and loved him dearly. \ No newline at end of file +It was Toto that made Dorothy laugh, and saved her from growing as gray as her other surroundings. Toto was not gray; he was a little black dog, with long, silky hair and small black eyes that twinkled merrily on either side of his funny, wee nose. Toto played all day long, and Dorothy played with him, and loved him dearly.