mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Support category pages
This commit is contained in:
parent
c207077877
commit
917d32f8a5
File diff suppressed because one or more lines are too long
@ -369,6 +369,7 @@ func handlePageRequest(c *gin.Context) {
|
||||
"DiaryMode": diaryMode,
|
||||
"Date": time.Now().Format("2006-01-02"),
|
||||
"UnixTime": time.Now().Unix(),
|
||||
"ChildPageNames": p.ChildPageNames(),
|
||||
})
|
||||
}
|
||||
|
||||
|
21
page.go
21
page.go
@ -5,6 +5,7 @@ import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
@ -128,6 +129,26 @@ func (p *Page) Save() error {
|
||||
return ioutil.WriteFile(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0644)
|
||||
}
|
||||
|
||||
func (p *Page) ChildPageNames() []string {
|
||||
prefix := strings.ToLower(p.Name + ": ")
|
||||
files, err := filepath.Glob(path.Join(pathToData, "*"))
|
||||
if err != nil {
|
||||
panic("Filepath pattern cannot be malformed")
|
||||
}
|
||||
|
||||
result := []string{}
|
||||
for i := range files {
|
||||
basename := filepath.Base(files[i])
|
||||
if strings.HasSuffix(basename, ".json") {
|
||||
cname, err := decodeFromBase32(basename[:len(basename)-len(".json")])
|
||||
if err == nil && strings.HasPrefix(strings.ToLower(cname), prefix) {
|
||||
result = append(result, cname)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (p *Page) IsNew() bool {
|
||||
return !exists(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
|
||||
}
|
||||
|
14
page_test.go
14
page_test.go
@ -55,4 +55,18 @@ func TestGeneral(t *testing.T) {
|
||||
t.Errorf("Did not render: '%s'", p2.RenderedPage)
|
||||
}
|
||||
|
||||
p3 := Open("testpage: childpage")
|
||||
err = p3.Update("**child content**")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
children := p.ChildPageNames()
|
||||
if len(children) != 1 {
|
||||
t.Errorf("Expected 1 child page to be found by '%s', got %d", encodeToBase32FileGlob(p.Name+": ")+"*", len(children))
|
||||
return
|
||||
}
|
||||
if children[0] != "testpage: childpage" {
|
||||
t.Errorf("Expected child page %s to be found (got %s)", "testpage: childpage", children[0])
|
||||
}
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ body#pad.HasDotInName textarea {
|
||||
}
|
||||
|
||||
@media (min-width: 5em) {
|
||||
div#menu, div#rendered, body#pad textarea {
|
||||
div#menu, div#rendered, .ChildPageNames, body#pad textarea {
|
||||
padding-left: 2%;
|
||||
padding-right: 2%;
|
||||
}
|
||||
@ -75,26 +75,54 @@ body#pad.HasDotInName textarea {
|
||||
padding-left:1.2em;
|
||||
padding-right:em;
|
||||
}
|
||||
.ChildPageNames ul {
|
||||
grid-template-columns: repeat(1, 1fr);
|
||||
}
|
||||
}
|
||||
@media (min-width: 50em) {
|
||||
div#menu, div#rendered, body#pad textarea {
|
||||
div#menu, div#rendered, .ChildPageNames, body#pad textarea {
|
||||
padding-left: 10%;
|
||||
padding-right: 10%;
|
||||
}
|
||||
.pure-menu-disabled, .pure-menu-heading, .pure-menu-link {
|
||||
padding: .5em 1em;
|
||||
}
|
||||
.ChildPageNames ul {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
}
|
||||
}
|
||||
@media (min-width: 70em) {
|
||||
div#menu, div#rendered, body#pad textarea {
|
||||
div#menu, div#rendered, .ChildPageNames, body#pad textarea {
|
||||
padding-left: 15%;
|
||||
padding-right: 15%;
|
||||
}
|
||||
.ChildPageNames ul {
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
@media (min-width: 100em) {
|
||||
div#menu, div#rendered, body#pad textarea {
|
||||
div#menu, div#rendered, .ChildPageNames, body#pad textarea {
|
||||
padding-left: 20%;
|
||||
padding-right: 20%;
|
||||
}
|
||||
.ChildPageNames ul {
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
.ChildPageNames ul {
|
||||
margin: 0.3em 0 0 1.6em;
|
||||
padding: 0;
|
||||
display: grid;
|
||||
grid-gap: 0.5rem;
|
||||
}
|
||||
|
||||
.ChildPageNames li {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.ChildPageNames a {
|
||||
color: #0645ad;
|
||||
background: none;
|
||||
}
|
||||
|
@ -197,6 +197,16 @@
|
||||
</table>
|
||||
{{ end }}
|
||||
</div>
|
||||
{{ if .ChildPageNames }}
|
||||
<section class="ChildPageNames">
|
||||
<h2>See also</h2>
|
||||
<ul>
|
||||
{{ range .ChildPageNames }}
|
||||
<li><a href="/{{ . }}/view">{{ . }}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</section>
|
||||
{{ end }}
|
||||
</div>
|
||||
</article>
|
||||
</body>
|
||||
|
Loading…
Reference in New Issue
Block a user