Require .txt filename extension for entries, allow defining entry title via Title: ... in first line of the file, update examples

This commit is contained in:
ChaoticByte 2025-07-11 21:32:16 +02:00
parent 1db5336f35
commit 89fbb17ab7
No known key found for this signature in database
6 changed files with 53 additions and 11 deletions

View file

@ -8,12 +8,25 @@ A very simple and clean reader/browser? for plaintext... documentation? wiki? ch
## How to use ## How to use
1. Set your settings in `settings.go` 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` 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` 5. Run `./server`
6. put behind a proxy (or don't). 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 ## Requirements
Developed and tested on Linux. Should work on other Unixoids. Developed and tested on Linux. Should work on other Unixoids.

View file

@ -44,20 +44,44 @@ func (db *Database) search(query string) []string { // returns keys (entry names
func BuildDB(directory string) Database { func BuildDB(directory string) Database {
logger := log.Default() logger := log.Default()
logger.Println("Building database") logger.Println("Building database")
// keys, entries // files, entries
var keys []string var files []string
entries := map[string]string{} entries := map[string]string{}
// get files in directory and read them // get files in directory and read them
directory = strings.TrimRight(directory, "/") // we don't need that last /, don't use the root directory / directory = strings.TrimRight(directory, "/") // we don't need that last /, don't use the root directory /
entriesDirFs := os.DirFS(directory) entriesDirFs := os.DirFS(directory)
keys, err := fs.Glob(entriesDirFs, "*") files, err := fs.Glob(entriesDirFs, "*.txt")
if err != nil { logger.Panicln(err) } if err != nil { logger.Panicln(err) }
for _, k := range keys { titles := []string{}
contentB, err := os.ReadFile(directory + "/" + k) for _, f := range files {
fileData, err := os.ReadFile(directory + "/" + f)
if err != nil { logger.Panicln(err) } if err != nil { logger.Panicln(err) }
content := string(contentB) content := string(fileData)
entries[k] = content 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) matcher := search.New(ContentLanguage, search.IgnoreCase, search.IgnoreDiacritics)
return Database{Keys: keys, Entries: entries, matcher: matcher} return Database{Keys: titles, Entries: entries, matcher: matcher}
} }

View file

@ -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. 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.

View file

@ -1 +1,3 @@
Title: Example Entry
This is an example entry. This is an example entry.

1
entries/only-title.txt Normal file
View file

@ -0,0 +1 @@
Title: very interesting entry

View file

@ -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. 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. 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.