From 5e4be6f2a2a39b05e8e05050735ed824c64e74e3 Mon Sep 17 00:00:00 2001 From: Gwyn Date: Thu, 11 Jul 2024 20:45:36 -0600 Subject: [PATCH 1/7] Passing simple case of grouping pages by directory. --- andrew_server_test.go | 8 ++- linksbuilder.go | 101 +++++++++++++++++++++++++++++++++++-- linksbuilder_test.go | 115 +++++++++++++++++++++--------------------- page.html | 14 +++++ 4 files changed, 171 insertions(+), 67 deletions(-) create mode 100644 page.html diff --git a/andrew_server_test.go b/andrew_server_test.go index adbd4eb..460f45a 100644 --- a/andrew_server_test.go +++ b/andrew_server_test.go @@ -422,13 +422,11 @@ func TestMainCalledWithInvalidAddressPanics(t *testing.T) { // what file attribute andrew is actually sorting on. func TestArticlesInAndrewTableOfContentsAreDefaultSortedByModTime(t *testing.T) { - expected, err := regexp.Compile(`.*b_newer.*a_older.*`) + expectedOrder, err := regexp.Compile(`(?s).*b_newer.*a_older.*`) if err != nil { t.Fatal(err) } - // expected := `b_newer.html` + - // `a_older.html` contentRoot := t.TempDir() @@ -465,8 +463,8 @@ func TestArticlesInAndrewTableOfContentsAreDefaultSortedByModTime(t *testing.T) received := page.Content - if expected.FindString(received) == "" { - t.Fatalf(cmp.Diff(expected, received)) + if expectedOrder.FindString(received) == "" { + t.Fatalf(cmp.Diff(expectedOrder, received)) } } diff --git a/linksbuilder.go b/linksbuilder.go index f6b7ac7..6c84156 100644 --- a/linksbuilder.go +++ b/linksbuilder.go @@ -3,6 +3,10 @@ package andrew import ( "bytes" "fmt" + "path" + "regexp" + "sort" + "strings" "text/template" "time" ) @@ -13,20 +17,107 @@ import ( // as a list of html links to those pages. func RenderTemplates(siblings []Page, startingPage Page) ([]byte, error) { + tableOfContents, err := regexp.Compile(`.*{{\s*\.AndrewTableOfContents\s*}}.*`) + if err != nil { + return nil, err + } + + if tableOfContents.FindString(startingPage.Content) != "" { + return renderAndrewTableOfContents(siblings, startingPage) + } + + tableOfContentsWithDirs, err := regexp.Compile(`.*{{\s*\.AndrewTableOfContentsWithDirectories\s*}}.*`) + if err != nil { + return nil, err + } + + if tableOfContentsWithDirs.FindString(startingPage.Content) != "" { + return renderAndrewTableOfContentsWithDirectories(siblings, startingPage) + } + + return []byte(startingPage.Content), nil +} + +func countSlashes(s string) int { + return strings.Count(s, "/") +} + +func renderAndrewTableOfContentsWithDirectories(siblings []Page, startingPage Page) ([]byte, error) { + var links bytes.Buffer + var templateBuffer bytes.Buffer + directoriesAndContents := make(map[string][]Page) + + for _, sibling := range siblings { + path, _ := path.Split(sibling.UrlPath) + directoriesAndContents[path] = append(directoriesAndContents[path], sibling) + } + + keys := make([]string, 0, len(directoriesAndContents)) + for k := range directoriesAndContents { + keys = append(keys, k) + } + + sort.Slice(keys, func(i, j int) bool { + slashesI := countSlashes(keys[i]) + slashesJ := countSlashes(keys[j]) + if slashesI == slashesJ { + return keys[i] < keys[j] // Lexicographical order if the number of slashes is the same + } + return slashesI < slashesJ // Primary order by number of slashes + }) + + linkCount := 0 + dirCount := 1 + + links.Write([]byte(" +// ` // contentRoot := fstest.MapFS{ -// "groupedContents.html": &fstest.MapFile{Data: []byte(` -// {{ .AndrewTableOfContentsGrouped}}`)}, - -// "parentDir/index.html": &fstest.MapFile{Data: []byte(` -// -// -// -// -// `)}, -// "parentDir/displayme.html": &fstest.MapFile{Data: []byte(` -// -// -// -// -// `)}, -// "parentDir/childDir/1-2-3.html": &fstest.MapFile{Data: []byte(` -// -// -// 1-2-3 Page -// -// `)}, +// "groupedContents.html": &fstest.MapFile{Data: []byte(`{{.AndrewTableOfContentsWithDirectories}}`)}, +// "parentDir/index.html": &fstest.MapFile{}, +// "parentDir/displayme.html": &fstest.MapFile{}, +// "parentDir/childDir/1-2-3.html": &fstest.MapFile{}, // } // s := newTestAndrewServer(t, contentRoot) diff --git a/page.html b/page.html new file mode 100644 index 0000000..3983a30 --- /dev/null +++ b/page.html @@ -0,0 +1,14 @@ + + +directory layout page + + + + + \ No newline at end of file From f3aa4be12aa68be839b517d39d4e669f97723920 Mon Sep 17 00:00:00 2001 From: Gwyn Date: Thu, 11 Jul 2024 20:53:07 -0600 Subject: [PATCH 2/7] Remove the starting page from the results being shown. We already know where we are, after all. --- linksbuilder.go | 6 ++++++ linksbuilder_test.go | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/linksbuilder.go b/linksbuilder.go index 6c84156..1a6d0cc 100644 --- a/linksbuilder.go +++ b/linksbuilder.go @@ -72,6 +72,7 @@ func renderAndrewTableOfContentsWithDirectories(siblings []Page, startingPage Pa links.Write([]byte("