Skip to content

Commit 1150ee6

Browse files
committed
Use -- for horizontal rule and adjust pages
1 parent 8c286bc commit 1150ee6

File tree

7 files changed

+114
-129
lines changed

7 files changed

+114
-129
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ It's a basic file format. Every command must be on it's own line empty lines are
4141
| `<Link>` | Will create a link to `<Link>` with the display name `<Link>` |
4242
| `<Link> <Name>` | Will create a link to `<Link>` with the display name `<Name>` |
4343
| `Column` | Will create a column |
44-
| `Page` or `--` | Creates a new page |
45-
46-
The older delimiter `--` is still supported for compatibility and will create a full-screen page break.
44+
| `Page` | Creates a new page |
45+
| `--` | Inserts a horizontal rule and resets columns |
4746

4847
![img.png](media/img.png)
4948

bookmarkProcessor.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,53 @@ type BookmarkColumn struct {
1818
Categories []*BookmarkCategory
1919
}
2020

21-
type BookmarkPage struct {
21+
type BookmarkBlock struct {
2222
Columns []*BookmarkColumn
23+
HR bool
24+
}
25+
26+
type BookmarkPage struct {
27+
Blocks []*BookmarkBlock
2328
}
2429

2530
func PreprocessBookmarks(bookmarks string) []*BookmarkPage {
2631
lines := strings.Split(bookmarks, "\n")
27-
var result = []*BookmarkPage{{Columns: []*BookmarkColumn{{}}}}
32+
var result = []*BookmarkPage{{Blocks: []*BookmarkBlock{{Columns: []*BookmarkColumn{{}}}}}}
2833
var currentCategory *BookmarkCategory
2934

3035
for _, line := range lines {
3136
line = strings.TrimSpace(line)
32-
if strings.EqualFold(line, "Page") || line == "--" {
37+
if strings.EqualFold(line, "Page") {
38+
if currentCategory != nil {
39+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
40+
lastColumn := lastBlock.Columns[len(lastBlock.Columns)-1]
41+
lastColumn.Categories = append(lastColumn.Categories, currentCategory)
42+
currentCategory = nil
43+
}
44+
result = append(result, &BookmarkPage{Blocks: []*BookmarkBlock{{Columns: []*BookmarkColumn{{}}}}})
45+
continue
46+
}
47+
if line == "--" {
3348
if currentCategory != nil {
34-
lastColumn := result[len(result)-1].Columns[len(result[len(result)-1].Columns)-1]
49+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
50+
lastColumn := lastBlock.Columns[len(lastBlock.Columns)-1]
3551
lastColumn.Categories = append(lastColumn.Categories, currentCategory)
3652
currentCategory = nil
3753
}
38-
result = append(result, &BookmarkPage{Columns: []*BookmarkColumn{{}}})
54+
// add hr block then start a new column block
55+
result[len(result)-1].Blocks = append(result[len(result)-1].Blocks, &BookmarkBlock{HR: true})
56+
result[len(result)-1].Blocks = append(result[len(result)-1].Blocks, &BookmarkBlock{Columns: []*BookmarkColumn{{}}})
3957
continue
4058
}
4159
if strings.EqualFold(line, "column") {
4260
if currentCategory != nil {
43-
lastColumn := result[len(result)-1].Columns[len(result[len(result)-1].Columns)-1]
61+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
62+
lastColumn := lastBlock.Columns[len(lastBlock.Columns)-1]
4463
lastColumn.Categories = append(lastColumn.Categories, currentCategory)
4564
currentCategory = nil
4665
}
47-
result[len(result)-1].Columns = append(result[len(result)-1].Columns, &BookmarkColumn{})
66+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
67+
lastBlock.Columns = append(lastBlock.Columns, &BookmarkColumn{})
4868
continue
4969
}
5070
parts := strings.Fields(line)
@@ -56,7 +76,8 @@ func PreprocessBookmarks(bookmarks string) []*BookmarkPage {
5676
if currentCategory == nil {
5777
currentCategory = &BookmarkCategory{Name: categoryName}
5878
} else if currentCategory.Name != "" {
59-
lastColumn := result[len(result)-1].Columns[len(result[len(result)-1].Columns)-1]
79+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
80+
lastColumn := lastBlock.Columns[len(lastBlock.Columns)-1]
6081
lastColumn.Categories = append(lastColumn.Categories, currentCategory)
6182
currentCategory = &BookmarkCategory{Name: categoryName}
6283
} else {
@@ -74,7 +95,8 @@ func PreprocessBookmarks(bookmarks string) []*BookmarkPage {
7495
}
7596

7697
if currentCategory != nil && currentCategory.Name != "" {
77-
lastColumn := result[len(result)-1].Columns[len(result[len(result)-1].Columns)-1]
98+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
99+
lastColumn := lastBlock.Columns[len(lastBlock.Columns)-1]
78100
lastColumn.Categories = append(lastColumn.Categories, currentCategory)
79101
}
80102

bookmarkProcessor_test.go

Lines changed: 61 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -5,141 +5,87 @@ import (
55
"testing"
66
)
77

8+
type (
9+
Pg = BookmarkPage
10+
Blk = BookmarkBlock
11+
Col = BookmarkColumn
12+
Cat = BookmarkCategory
13+
Ent = BookmarkEntry
14+
)
15+
16+
func e(u, n string) *Ent { return &Ent{Url: u, Name: n} }
17+
func cat(name string, es ...*Ent) *Cat { return &Cat{Name: name, Entries: es} }
18+
func col(cs ...*Cat) *Col { return &Col{Categories: cs} }
19+
func colsBlock(cs ...*Col) *Blk { return &Blk{Columns: cs} }
20+
func hrBlock() *Blk { return &Blk{HR: true} }
21+
func page(bs ...*Blk) *Pg { return &Pg{Blocks: bs} }
22+
823
func Test_preprocessBookmarks(t *testing.T) {
924
tests := []struct {
10-
name string
11-
bookmarks string
12-
want []*BookmarkPage
25+
name string
26+
input string
27+
want []*Pg
1328
}{
1429
{
15-
name: "basic",
16-
bookmarks: "Category: Search\nhttp://www.google.com.au Google\nCategory: Wikies\nhttp://en.wikipedia.org/wiki/Main_Page Wikipedia\nhttp://mathworld.wolfram.com/ Math World\nhttp://gentoo-wiki.com/Main_Page Gentoo-wiki\n",
17-
want: []*BookmarkPage{{
18-
Columns: []*BookmarkColumn{{
19-
Categories: []*BookmarkCategory{
20-
{
21-
Name: "Search",
22-
Entries: []*BookmarkEntry{{Url: "http://www.google.com.au", Name: "Google"}},
23-
},
24-
{
25-
Name: "Wikies",
26-
Entries: []*BookmarkEntry{
27-
{Url: "http://en.wikipedia.org/wiki/Main_Page", Name: "Wikipedia"},
28-
{Url: "http://mathworld.wolfram.com/", Name: "Math World"},
29-
{Url: "http://gentoo-wiki.com/Main_Page", Name: "Gentoo-wiki"},
30-
},
31-
},
32-
},
33-
}},
34-
}},
30+
name: "basic",
31+
input: "Category: Search\nhttp://g.com G\nCategory: Wikies\nhttp://w.com W\n",
32+
want: []*Pg{
33+
page(colsBlock(
34+
col(cat("Search", e("http://g.com", "G")),
35+
cat("Wikies", e("http://w.com", "W"))),
36+
)),
37+
},
3538
},
3639
{
37-
name: "columns",
38-
bookmarks: "Category: Search\nhttp://www.google.com.au Google\nColumn\nCategory: Wikies\nhttp://en.wikipedia.org/wiki/Main_Page Wikipedia\n",
39-
want: []*BookmarkPage{{
40-
Columns: []*BookmarkColumn{
41-
{
42-
Categories: []*BookmarkCategory{
43-
{
44-
Name: "Search",
45-
Entries: []*BookmarkEntry{{Url: "http://www.google.com.au", Name: "Google"}},
46-
},
47-
},
48-
},
49-
{
50-
Categories: []*BookmarkCategory{
51-
{
52-
Name: "Wikies",
53-
Entries: []*BookmarkEntry{{Url: "http://en.wikipedia.org/wiki/Main_Page", Name: "Wikipedia"}},
54-
},
55-
},
56-
},
57-
},
58-
}},
40+
name: "columns",
41+
input: "Category: Search\nhttp://g.com G\nColumn\nCategory: Wikies\nhttp://w.com W\n",
42+
want: []*Pg{
43+
page(colsBlock(
44+
col(cat("Search", e("http://g.com", "G"))),
45+
col(cat("Wikies", e("http://w.com", "W"))),
46+
)),
47+
},
5948
},
6049
{
61-
name: "pages",
62-
bookmarks: "Category: First\nhttp://example.com A\nPage\nCategory: Second\nhttp://example.org B\n",
63-
want: []*BookmarkPage{
64-
{
65-
Columns: []*BookmarkColumn{{
66-
Categories: []*BookmarkCategory{
67-
{
68-
Name: "First",
69-
Entries: []*BookmarkEntry{{Url: "http://example.com", Name: "A"}},
70-
},
71-
},
72-
}},
73-
},
74-
{
75-
Columns: []*BookmarkColumn{{
76-
Categories: []*BookmarkCategory{
77-
{
78-
Name: "Second",
79-
Entries: []*BookmarkEntry{{Url: "http://example.org", Name: "B"}},
80-
},
81-
},
82-
}},
83-
},
50+
name: "pages",
51+
input: "Category: A\nhttp://a.com a\nPage\nCategory: B\nhttp://b.com b\n",
52+
want: []*Pg{
53+
page(colsBlock(col(cat("A", e("http://a.com", "a"))))),
54+
page(colsBlock(col(cat("B", e("http://b.com", "b"))))),
8455
},
8556
},
8657
{
87-
name: "pages and columns",
88-
bookmarks: "Category: A\nhttp://a.com\nColumn\nCategory: B\nhttp://b.com\nPage\nCategory: C\nhttp://c.com\nColumn\nCategory: D\nhttp://d.com\n",
89-
want: []*BookmarkPage{
90-
{
91-
Columns: []*BookmarkColumn{
92-
{
93-
Categories: []*BookmarkCategory{
94-
{Name: "A", Entries: []*BookmarkEntry{{Url: "http://a.com", Name: "http://a.com"}}},
95-
},
96-
},
97-
{
98-
Categories: []*BookmarkCategory{
99-
{Name: "B", Entries: []*BookmarkEntry{{Url: "http://b.com", Name: "http://b.com"}}},
100-
},
101-
},
102-
},
103-
},
104-
{
105-
Columns: []*BookmarkColumn{
106-
{
107-
Categories: []*BookmarkCategory{
108-
{Name: "C", Entries: []*BookmarkEntry{{Url: "http://c.com", Name: "http://c.com"}}},
109-
},
110-
},
111-
{
112-
Categories: []*BookmarkCategory{
113-
{Name: "D", Entries: []*BookmarkEntry{{Url: "http://d.com", Name: "http://d.com"}}},
114-
},
115-
},
116-
},
117-
},
58+
name: "pages and columns",
59+
input: "Category: A\nhttp://a.com\nColumn\nCategory: B\nhttp://b.com\nPage\nCategory: C\nhttp://c.com\nColumn\nCategory: D\nhttp://d.com\n",
60+
want: []*Pg{
61+
page(colsBlock(
62+
col(cat("A", e("http://a.com", "http://a.com"))),
63+
col(cat("B", e("http://b.com", "http://b.com"))),
64+
)),
65+
page(colsBlock(
66+
col(cat("C", e("http://c.com", "http://c.com"))),
67+
col(cat("D", e("http://d.com", "http://d.com"))),
68+
)),
11869
},
11970
},
12071
{
121-
name: "double dash compat",
122-
bookmarks: "Category: One\nhttp://one.com\n--\nCategory: Two\nhttp://two.com\n",
123-
want: []*BookmarkPage{
124-
{
125-
Columns: []*BookmarkColumn{{
126-
Categories: []*BookmarkCategory{{Name: "One", Entries: []*BookmarkEntry{{Url: "http://one.com", Name: "http://one.com"}}}},
127-
}},
128-
},
129-
{
130-
Columns: []*BookmarkColumn{{
131-
Categories: []*BookmarkCategory{{Name: "Two", Entries: []*BookmarkEntry{{Url: "http://two.com", Name: "http://two.com"}}}},
132-
}},
133-
},
72+
name: "horizontal rule",
73+
input: "Category: One\nhttp://one.com\n--\nCategory: Two\nhttp://two.com\n",
74+
want: []*Pg{
75+
page(
76+
colsBlock(col(cat("One", e("http://one.com", "http://one.com")))),
77+
hrBlock(),
78+
colsBlock(col(cat("Two", e("http://two.com", "http://two.com")))),
79+
),
13480
},
13581
},
13682
}
13783

13884
for _, tt := range tests {
13985
t.Run(tt.name, func(t *testing.T) {
140-
got := PreprocessBookmarks(tt.bookmarks)
141-
if diff := cmp.Diff(got, tt.want); diff != "" {
142-
t.Errorf("PreprocessBookmarks() = diff\n%s", diff)
86+
got := PreprocessBookmarks(tt.input)
87+
if diff := cmp.Diff(tt.want, got); diff != "" {
88+
t.Errorf("diff:\n%s", diff)
14389
}
14490
})
14591
}

funcs.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ func NewFuncs(r *http.Request) template.FuncMap {
115115
pages := PreprocessBookmarks(bookmark)
116116
var columns []*BookmarkColumn
117117
for _, p := range pages {
118-
columns = append(columns, p.Columns...)
118+
for _, b := range p.Blocks {
119+
if b.HR {
120+
continue
121+
}
122+
columns = append(columns, b.Columns...)
123+
}
119124
}
120125
return columns, nil
121126
},

main.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,9 @@ div.title {
9292
.bookmarkPage {
9393
height: 100vh;
9494
}
95+
96+
.bookmarkHr {
97+
margin: 1em 0;
98+
border: none;
99+
border-top: 1px solid #800000;
100+
}

templates/edit.gohtml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
"&lt;URL&gt; &lt;Name&gt; &lt;Newline&gt;" - Will create a link to URL with name, if you need to use a space make it %20 and use that.<br>
1818
"Category: &lt;name&gt; &lt;Newline&gt;" - Creates a category named &lt;name&gt;.<br />
1919
"Column &lt;Newline&gt;" - Creates a new column.<br/>
20-
"Page &lt;Newline&gt;" or "--" - Creates a new page.
20+
"Page &lt;Newline&gt;" - Creates a new page.<br/>
21+
"--" - Inserts a horizontal rule and resets the columns.
2122
{{ template "tail" $ }}

templates/indexPage.gohtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
{{else}}
66
{{- range bookmarkPages }}
77
<div class="bookmarkPage">
8+
{{- range .Blocks }}
9+
{{- if .HR }}
10+
<hr class="bookmarkHr" />
11+
{{- else }}
812
<table>
913
<tr valign="top">
1014
{{- range .Columns }}
@@ -24,6 +28,8 @@
2428
{{- end }}
2529
</tr>
2630
</table>
31+
{{- end }}
32+
{{- end }}
2733
</div>
2834
{{- end }}
2935
{{end}}

0 commit comments

Comments
 (0)