diff --git a/bookmarks/bookmarks.go b/bookmarks/bookmarks.go index 4a6f347..1a65c33 100644 --- a/bookmarks/bookmarks.go +++ b/bookmarks/bookmarks.go @@ -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 } diff --git a/go.mod b/go.mod index 1257f07..891cf6a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 236a96b..28d56cf 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/skipper/bookmarks b/skipper/bookmarks new file mode 100644 index 0000000..0f90fc0 --- /dev/null +++ b/skipper/bookmarks @@ -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/