1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-06-19 19:25:24 +00:00

Support multiple bookmarks with the same name

This commit is contained in:
makeworld 2021-05-14 18:45:41 -04:00
parent d4aec14f63
commit 9fd5deb649
4 changed files with 37 additions and 27 deletions

View File

@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Plaintext documents are escaped properly (regression from v1.8.0) - Plaintext documents are escaped properly (regression from v1.8.0)
- Help page scrollbar color matches what's in the theme config - Help page scrollbar color matches what's in the theme config
- Regression where lists would not appear if `bullets = false` (#234, #235) - Regression where lists would not appear if `bullets = false` (#234, #235)
- Support multiple bookmarks with the same name
## [1.8.0] - 2021-02-17 ## [1.8.0] - 2021-02-17

View File

@ -159,27 +159,35 @@ func Remove(url string) {
} }
} }
// All returns all the bookmarks in a map of URLs to names. // bkmkNameSlice is used for sorting bookmarks alphabetically.
// It also returns a slice of map keys, sorted so that the map *values* // It implements sort.Interface.
// are in alphabetical order, with case ignored. type bkmkNameSlice struct {
func All() (map[string]string, []string) { names []string
bkmksMap := make(map[string]string) urls []string
}
inverted := make(map[string]string) // Holds inverted map, name->URL
names := make([]string, len(data.Bookmarks)) // Holds bookmark names, for sorting func (b *bkmkNameSlice) Len() int {
keys := make([]string, len(data.Bookmarks)) // Final sorted keys (URLs), for returning at the end return len(b.names)
}
for i, bkmk := range data.Bookmarks { func (b *bkmkNameSlice) Less(i, j int) bool {
bkmksMap[bkmk.URL] = bkmk.Name return b.names[i] < b.names[j]
inverted[bkmk.Name] = bkmk.URL }
names[i] = bkmk.Name func (b *bkmkNameSlice) Swap(i, j int) {
} b.names[i], b.names[j] = b.names[j], b.names[i]
b.urls[i], b.urls[j] = b.urls[j], b.urls[i]
// Sort, then turn back into URL keys }
sort.Strings(names)
for i, name := range names { // All returns all the bookmarks, as two arrays, one for names and one for URLs.
keys[i] = inverted[name] // They are sorted alphabetically.
} func All() ([]string, []string) {
b := bkmkNameSlice{
return bkmksMap, keys make([]string, len(data.Bookmarks)),
make([]string, len(data.Bookmarks)),
}
for i, bkmk := range data.Bookmarks {
b.names[i] = bkmk.Name
b.urls[i] = bkmk.URL
}
sort.Sort(&b)
return b.names, b.urls
} }

View File

@ -2,6 +2,7 @@ package bookmarks
// Structs and code for the XBEL XML bookmark format. // Structs and code for the XBEL XML bookmark format.
// https://github.com/makeworld-the-better-one/amfora/issues/68 // https://github.com/makeworld-the-better-one/amfora/issues/68
// http://xbel.sourceforge.net/
import ( import (
"encoding/xml" "encoding/xml"
@ -38,7 +39,7 @@ type xbel struct {
XMLName xml.Name `xml:"xbel"` XMLName xml.Name `xml:"xbel"`
Version string `xml:"version,attr"` Version string `xml:"version,attr"`
Bookmarks []*xbelBookmark `xml:"bookmark"` Bookmarks []*xbelBookmark `xml:"bookmark"`
// Later: Folders []*xbelFolder // Folders []*xbelFolder // Use later for #56
} }
// Instance of xbel - loaded from bookmarks file // Instance of xbel - loaded from bookmarks file

View File

@ -127,9 +127,9 @@ func Bookmarks(t *tab) {
bkmkPageRaw := "# Bookmarks\r\n\r\n" bkmkPageRaw := "# Bookmarks\r\n\r\n"
// Gather bookmarks // Gather bookmarks
m, keys := bookmarks.All() names, urls := bookmarks.All()
for i := range keys { for i := range names {
bkmkPageRaw += fmt.Sprintf("=> %s %s\r\n", keys[i], m[keys[i]]) bkmkPageRaw += fmt.Sprintf("=> %s %s\r\n", urls[i], names[i])
} }
// Render and display // Render and display
content, links := renderer.RenderGemini(bkmkPageRaw, textWidth(), false) content, links := renderer.RenderGemini(bkmkPageRaw, textWidth(), false)