Bookmarks now save

This commit is contained in:
Sasha Koshka 2022-11-27 00:26:21 -05:00
parent 18800d5b87
commit c9cbc2acb1
4 changed files with 78 additions and 7 deletions

View File

@ -1,6 +1,15 @@
package bookmarks
import "os"
import "fmt"
import "bufio"
import "strings"
import "net/url"
import "path/filepath"
import "git.tebibyte.media/sashakoshka/stone/config"
var bookmarksFile string
var dataDirectory string
type Bookmark struct {
Title string
@ -10,10 +19,7 @@ type Bookmark struct {
var HomePage = New("Home", "about:home")
var SearchEngine = New("Search", "gemini://geminispace.info:1965/search/")
var bookmarks = []Bookmark {
New("Project Gemini", "gemini://gemini.circumlunar.space:1965/"),
New("Search", "gemini://geminispace.info:1965/search/"),
}
var bookmarks []Bookmark
func Gemtext () (page string) {
page += "# Bookmarks\n"
@ -29,9 +35,71 @@ func Gemtext () (page string) {
func New (title, location string) (bookmark Bookmark) {
bookmark.Title = title
bookmark.Location, _ = url.Parse(location)
bookmark.Location.RawQuery = url.PathEscape(bookmark.Location.RawQuery)
return
}
func Add (bookmark Bookmark) {
bookmarks = append(bookmarks, bookmark)
save()
}
func init () {
dataDirectory = config.DataHome("skipper")
bookmarksFile = filepath.Join(dataDirectory, "bookmarks")
println(bookmarksFile)
load()
}
func load () {
defer func () {
if len(bookmarks) > 0 { return }
bookmarks = []Bookmark {
New("Project Gemini", "gemini://gemini.circumlunar.space:1965/"),
New("Search", "gemini://geminispace.info:1965/search/"),
}
} ()
file, err := os.Open(bookmarksFile)
if err != nil { return }
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
if line[0] == '#' {
continue
}
title, location, found := strings.Cut(scanner.Text(), ":")
if !found { continue }
title = strings.TrimSpace(title)
location = strings.TrimSpace(location)
bookmarks = append(bookmarks, New(title, location))
}
}
func save () (err error) {
err = os.MkdirAll(dataDirectory, 0755)
if err != nil { return }
file, err := os.OpenFile (
bookmarksFile,
os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0744)
if err != nil { return }
defer file.Close()
for _, bookmark := range bookmarks {
fmt.Fprintf (
file, "%s: %s\n",
bookmark.Title,
bookmark.Location.String())
}
return
}

4
go.mod
View File

@ -4,11 +4,9 @@ go 1.19
require (
git.sr.ht/~yotam/go-gemini v0.0.0-20191116204306-8ebb75240eef
git.tebibyte.media/sashakoshka/stone v0.2.1
git.tebibyte.media/sashakoshka/stone v0.4.1
)
replace git.tebibyte.media/sashakoshka/stone => /home/sashakoshka/repos/tebibyte/stone
require (
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 // indirect
github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 // indirect

2
go.sum
View File

@ -1,5 +1,7 @@
git.sr.ht/~yotam/go-gemini v0.0.0-20191116204306-8ebb75240eef h1:rHPrfUoN0cIwxf5PaSDQgDd9r1kBab0OiEu2akI12dU=
git.sr.ht/~yotam/go-gemini v0.0.0-20191116204306-8ebb75240eef/go.mod h1:KxQlipD0Ti7MfV3itYJfuvgcvd+SOlRTtbOK+A0DCCE=
git.tebibyte.media/sashakoshka/stone v0.4.1 h1:cKV5cX+y3DHfOBXXLzZ+TFV+fs3Jx+k7XevmDaINu0Q=
git.tebibyte.media/sashakoshka/stone v0.4.1/go.mod h1:ISnqmX6xvItOot3eW3YWLcNFeJrGpKetQGQniAjnU2A=
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 h1:1qlsVAQJXZHsaM8b6OLVo6muQUQd4CwkH/D3fnnbHXA=
github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298/go.mod h1:D+QujdIlUNfa0igpNMk6UIvlb6C252URs4yupRUV4lQ=
github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 h1:lTG4HQym5oPKjL7nGs+csTgiDna685ZXjxijkne828g=

3
skipper/bookmarks Normal file
View File

@ -0,0 +1,3 @@
Project Gemini: gemini://gemini.circumlunar.space:1965/
Search: gemini://geminispace.info:1965/search/
Gemini protocol documentation: gemini://gemini.circumlunar.space:1965/docs/