Skip to content

Commit 2723a13

Browse files
authored
Merge pull request #16 from arran4/codex/add-double-dash-for-pagination-in-markup
Add double dash markup for page breaks
2 parents 2bbddab + 1150ee6 commit 2723a13

File tree

8 files changed

+198
-85
lines changed

8 files changed

+198
-85
lines changed

.github/workflows/test.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-go@v5
15+
with:
16+
go-version-file: go.mod
17+
- run: go test ./...

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +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` | Creates a new page |
45+
| `--` | Inserts a horizontal rule and resets columns |
4446

4547
![img.png](media/img.png)
4648

bookmarkProcessor.go

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

21-
func PreprocessBookmarks(bookmarks string) []*BookmarkColumn {
21+
type BookmarkBlock struct {
22+
Columns []*BookmarkColumn
23+
HR bool
24+
}
25+
26+
type BookmarkPage struct {
27+
Blocks []*BookmarkBlock
28+
}
29+
30+
func PreprocessBookmarks(bookmarks string) []*BookmarkPage {
2231
lines := strings.Split(bookmarks, "\n")
23-
var result = []*BookmarkColumn{{}}
32+
var result = []*BookmarkPage{{Blocks: []*BookmarkBlock{{Columns: []*BookmarkColumn{{}}}}}}
2433
var currentCategory *BookmarkCategory
2534

2635
for _, line := range lines {
2736
line = strings.TrimSpace(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 == "--" {
48+
if currentCategory != nil {
49+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
50+
lastColumn := lastBlock.Columns[len(lastBlock.Columns)-1]
51+
lastColumn.Categories = append(lastColumn.Categories, currentCategory)
52+
currentCategory = nil
53+
}
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{{}}})
57+
continue
58+
}
2859
if strings.EqualFold(line, "column") {
2960
if currentCategory != nil {
30-
result[len(result)-1].Categories = append(result[len(result)-1].Categories, currentCategory)
61+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
62+
lastColumn := lastBlock.Columns[len(lastBlock.Columns)-1]
63+
lastColumn.Categories = append(lastColumn.Categories, currentCategory)
3164
currentCategory = nil
3265
}
33-
result = append(result, &BookmarkColumn{})
66+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
67+
lastBlock.Columns = append(lastBlock.Columns, &BookmarkColumn{})
3468
continue
3569
}
3670
parts := strings.Fields(line)
@@ -42,7 +76,9 @@ func PreprocessBookmarks(bookmarks string) []*BookmarkColumn {
4276
if currentCategory == nil {
4377
currentCategory = &BookmarkCategory{Name: categoryName}
4478
} else if currentCategory.Name != "" {
45-
result[len(result)-1].Categories = append(result[len(result)-1].Categories, currentCategory)
79+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
80+
lastColumn := lastBlock.Columns[len(lastBlock.Columns)-1]
81+
lastColumn.Categories = append(lastColumn.Categories, currentCategory)
4682
currentCategory = &BookmarkCategory{Name: categoryName}
4783
} else {
4884
currentCategory.Name = categoryName
@@ -59,7 +95,9 @@ func PreprocessBookmarks(bookmarks string) []*BookmarkColumn {
5995
}
6096

6197
if currentCategory != nil && currentCategory.Name != "" {
62-
result[len(result)-1].Categories = append(result[len(result)-1].Categories, currentCategory)
98+
lastBlock := result[len(result)-1].Blocks[len(result[len(result)-1].Blocks)-1]
99+
lastColumn := lastBlock.Columns[len(lastBlock.Columns)-1]
100+
lastColumn.Categories = append(lastColumn.Categories, currentCategory)
63101
}
64102

65103
return result

bookmarkProcessor_test.go

Lines changed: 70 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +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 []*BookmarkColumn
25+
name string
26+
input string
27+
want []*Pg
1328
}{
1429
{
15-
name: "Test",
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: []*BookmarkColumn{{
18-
Categories: []*BookmarkCategory{
19-
{
20-
Name: "Search",
21-
Entries: []*BookmarkEntry{
22-
{
23-
Url: "http://www.google.com.au",
24-
Name: "Google",
25-
},
26-
},
27-
},
28-
{
29-
Name: "Wikies",
30-
Entries: []*BookmarkEntry{
31-
{
32-
Url: "http://en.wikipedia.org/wiki/Main_Page",
33-
Name: "Wikipedia",
34-
},
35-
{
36-
Url: "http://mathworld.wolfram.com/",
37-
Name: "Math World",
38-
},
39-
{
40-
Url: "http://gentoo-wiki.com/Main_Page",
41-
Name: "Gentoo-wiki",
42-
},
43-
},
44-
},
45-
}}},
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+
},
38+
},
39+
{
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+
},
48+
},
49+
{
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"))))),
55+
},
4656
},
4757
{
48-
name: "Test",
49-
bookmarks: "Category: Search\nhttp://www.google.com.au Google\nColumn\nCategory: Wikies\nhttp://en.wikipedia.org/wiki/Main_Page Wikipedia\n",
50-
want: []*BookmarkColumn{
51-
{
52-
Categories: []*BookmarkCategory{
53-
{
54-
Name: "Search",
55-
Entries: []*BookmarkEntry{
56-
{
57-
Url: "http://www.google.com.au",
58-
Name: "Google",
59-
},
60-
},
61-
},
62-
},
63-
},
64-
{
65-
Categories: []*BookmarkCategory{
66-
{
67-
Name: "Wikies",
68-
Entries: []*BookmarkEntry{
69-
{
70-
Url: "http://en.wikipedia.org/wiki/Main_Page",
71-
Name: "Wikipedia",
72-
},
73-
},
74-
},
75-
},
76-
},
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+
)),
69+
},
70+
},
71+
{
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+
),
7780
},
7881
},
7982
}
83+
8084
for _, tt := range tests {
8185
t.Run(tt.name, func(t *testing.T) {
82-
got := PreprocessBookmarks(tt.bookmarks)
83-
if diff := cmp.Diff(got, tt.want); diff != "" {
84-
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)
8589
}
8690
})
8791
}

funcs.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ func NewFuncs(r *http.Request) template.FuncMap {
7474
}
7575
return "main", nil
7676
},
77+
"bookmarkPages": func() ([]*BookmarkPage, error) {
78+
session := r.Context().Value(ContextValues("session")).(*sessions.Session)
79+
githubUser, _ := session.Values["GithubUser"].(*github.User)
80+
token, _ := session.Values["Token"].(*oauth2.Token)
81+
82+
login := ""
83+
if githubUser != nil && githubUser.Login != nil {
84+
login = *githubUser.Login
85+
}
86+
87+
bookmarks, err := GetBookmarks(r.Context(), login, r.URL.Query().Get("ref"), token)
88+
var bookmark = defaultBookmarks
89+
if err != nil {
90+
// TODO check for error type and if it's not exist, fall through
91+
return nil, fmt.Errorf("bookmarkPages: %w", err)
92+
} else {
93+
bookmark = bookmarks
94+
}
95+
return PreprocessBookmarks(bookmark), nil
96+
},
7797
"bookmarkColumns": func() ([]*BookmarkColumn, error) {
7898
session := r.Context().Value(ContextValues("session")).(*sessions.Session)
7999
githubUser, _ := session.Values["GithubUser"].(*github.User)
@@ -92,7 +112,17 @@ func NewFuncs(r *http.Request) template.FuncMap {
92112
} else {
93113
bookmark = bookmarks
94114
}
95-
return PreprocessBookmarks(bookmark), nil
115+
pages := PreprocessBookmarks(bookmark)
116+
var columns []*BookmarkColumn
117+
for _, p := range pages {
118+
for _, b := range p.Blocks {
119+
if b.HR {
120+
continue
121+
}
122+
columns = append(columns, b.Columns...)
123+
}
124+
}
125+
return columns, nil
96126
},
97127
"tags": func() ([]*github.RepositoryTag, error) {
98128
session := r.Context().Value(ContextValues("session")).(*sessions.Session)

main.css

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,19 @@ div.title {
8282
background-color: transparent;
8383
}
8484
.textcenter {
85-
text-align: center;
86-
font-family: tahoma, arial, helvetica, sans-serif;
87-
font-size: 0.8em;
88-
color: #800000;
89-
background-color: transparent;
85+
text-align: center;
86+
font-family: tahoma, arial, helvetica, sans-serif;
87+
font-size: 0.8em;
88+
color: #800000;
89+
background-color: transparent;
90+
}
91+
92+
.bookmarkPage {
93+
height: 100vh;
94+
}
95+
96+
.bookmarkHr {
97+
margin: 1em 0;
98+
border: none;
99+
border-top: 1px solid #800000;
90100
}

templates/edit.gohtml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
Simply. First edit your page using the keywords below then set it as your start page.<br>
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 />
19-
"Column &lt;Newline&gt;" - Creates a new column.
19+
"Column &lt;Newline&gt;" - Creates a new column.<br/>
20+
"Page &lt;Newline&gt;" - Creates a new page.<br/>
21+
"--" - Inserts a horizontal rule and resets the columns.
2022
{{ template "tail" $ }}

templates/indexPage.gohtml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
You will need to login to see this page:
44
<a href="{{ OAuth2URL }}">Login</a><br>
55
{{else}}
6-
<table>
7-
<tr valign="top">
8-
{{- range bookmarkColumns }}
6+
{{- range bookmarkPages }}
7+
<div class="bookmarkPage">
8+
{{- range .Blocks }}
9+
{{- if .HR }}
10+
<hr class="bookmarkHr" />
11+
{{- else }}
12+
<table>
13+
<tr valign="top">
14+
{{- range .Columns }}
915
<td>
1016
{{- range .Categories }}
1117
<ul style="list-style-type: none;">
@@ -19,8 +25,12 @@
1925
</ul>
2026
{{- end }}
2127
</td>
22-
{{- end }}
23-
</tr>
24-
</table>
28+
{{- end }}
29+
</tr>
30+
</table>
31+
{{- end }}
32+
{{- end }}
33+
</div>
34+
{{- end }}
2535
{{end}}
2636
{{ template "tail" $ }}

0 commit comments

Comments
 (0)