/
sitemap.go
83 lines (65 loc) · 2.48 KB
/
sitemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2015 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package handlers
import (
"fmt"
"net/http"
"github.com/andreaskoch/allmark/common/route"
"github.com/andreaskoch/allmark/web/header"
"github.com/andreaskoch/allmark/web/orchestrator"
"github.com/andreaskoch/allmark/web/view/templates"
"github.com/andreaskoch/allmark/web/view/viewmodel"
"strings"
"text/template"
)
func Sitemap(headerWriter header.HeaderWriter,
navigationOrchestrator *orchestrator.NavigationOrchestrator,
sitemapOrchestrator *orchestrator.SitemapOrchestrator,
templateProvider templates.Provider) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// set headers
headerWriter.Write(w, header.CONTENTTYPE_HTML)
hostname := getBaseURLFromRequest(r)
// get the sitemap template
sitemapTemplate, err := templateProvider.GetSitemapTemplate(hostname)
if err != nil {
fmt.Fprintf(w, "Template not found. Error: %s", err)
return
}
// get the sitemap-entry template
sitemapEntryTemplate, childPlaceholder, err := templateProvider.GetSitemapEntryTemplate(hostname)
if err != nil {
fmt.Fprintf(w, "Template not found. Error: %s", err)
return
}
// Page parameters
pageTitle := "Sitemap"
pageType := "sitemap"
descriptionText := "A list of all items in this repository."
// Page model
viewModel := viewmodel.Model{}
viewModel.Type = pageType
viewModel.Title = pageTitle
viewModel.PageTitle = sitemapOrchestrator.GetPageTitle(pageTitle)
viewModel.Description = descriptionText
viewModel.ToplevelNavigation = navigationOrchestrator.GetToplevelNavigation()
viewModel.BreadcrumbNavigation = navigationOrchestrator.GetBreadcrumbNavigation(route.New())
sitemapPageModel := viewmodel.Sitemap{}
sitemapPageModel.Model = viewModel
sitemapPageModel.Tree = renderSitemapEntryTemplate(sitemapEntryTemplate, sitemapOrchestrator.GetSitemap(), childPlaceholder)
renderTemplate(sitemapTemplate, sitemapPageModel, w)
})
}
func renderSitemapEntryTemplate(template *template.Template, entry viewmodel.SitemapEntry, childPlaceholder string) string {
content, err := getRenderedCode(template, entry)
if err != nil {
return err.Error()
}
childCode := ""
for _, child := range entry.Children {
childCode += renderSitemapEntryTemplate(template, child, childPlaceholder)
}
content = strings.Replace(content, childPlaceholder, childCode, -1)
return content
}