<commit_before>// Copyright 2013 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package renderer

import (
	"bytes"
	"fmt"
	"github.com/andreaskoch/allmark/mapper"
	"github.com/andreaskoch/allmark/templates"
	"github.com/andreaskoch/allmark/view"
	"io"
	"strings"
	"text/template"
)

func (renderer *Renderer) Sitemap(writer io.Writer, host string) {

	if renderer.root == nil {
		fmt.Fprintf(writer, "The root is not ready yet.")
		return
	}

	// get the sitemap content template
	sitemapContentTemplate, err := renderer.templateProvider.GetSubTemplate(templates.SitemapContentTemplateName)
	if err != nil {
		fmt.Fprintf(writer, "Content template not found. Error: %s", err)
		return
	}

	// get the sitemap template
	sitemapTemplate, err := renderer.templateProvider.GetFullTemplate(templates.SitemapTemplateName)
	if err != nil {
		fmt.Fprintf(writer, "Template not found. Error: %s", err)
		return
	}

	// render the sitemap content
	sitemapContentModel := mapper.MapSitemap(renderer.root)
	sitemapContent := renderer.renderSitemapEntry(sitemapContentTemplate, sitemapContentModel)

	sitemapPageModel := view.Model{
		Title:                "Sitemap",
		Description:          "A list of all items in this repository.",
		Content:              sitemapContent,
		ToplevelNavigation:   renderer.root.ToplevelNavigation,
		BreadcrumbNavigation: renderer.root.BreadcrumbNavigation,
		Type:                 "sitemap",
	}

	writeTemplate(sitemapPageModel, sitemapTemplate, writer)
}

func (renderer *Renderer) renderSitemapEntry(templ *template.Template, sitemapModel *view.Sitemap) string {

	// render
	buffer := new(bytes.Buffer)
	writeTemplate(sitemapModel, templ, buffer)

	// get the produced html code
	rootCode := buffer.String()

	if len(sitemapModel.Childs) > 0 {

		// render all childs

		childCode := ""
		for _, child := range sitemapModel.Childs {
			childCode += "\n" + renderer.renderSitemapEntry(templ, child)<commit_msg>Response caching: Applied the same response caching technique to the HTML Sitemap response writer as I have applied to the RSS feed response writer<commit_after>// Copyright 2013 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package renderer

import (
	"bytes"
	"fmt"
	"github.com/andreaskoch/allmark/mapper"
	"github.com/andreaskoch/allmark/repository"
	"github.com/andreaskoch/allmark/templates"
	"github.com/andreaskoch/allmark/view"
	"io"
	"strings"
	"text/template"
)

func (renderer *Renderer) Sitemap(writer io.Writer, host string) {

	targetFile := "sitemap.html"
	pathProvider := renderer.pathProvider
	rssRenderer := func(writer io.Writer, host string) {
		sitemap(writer, renderer.root, renderer.templateProvider)
	}

	cacheReponse(targetFile, pathProvider, rssRenderer, host, writer)
}

func sitemap(writer io.Writer, rootItem *repository.Item, templateProvider *templates.Provider) {

	if rootItem == nil {
		fmt.Fprintf(writer, "The root is not ready yet.")
		return
	}

	// get the sitemap content template
	sitemapContentTemplate, err := templateProvider.GetSubTemplate(templates.SitemapContentTemplateName)
	if err != nil {
		fmt.Fprintf(writer, "Content template not found. Error: %s", err)
		return
	}

	// get the sitemap template
	sitemapTemplate, err := templateProvider.GetFullTemplate(templates.SitemapTemplateName)
	if err != nil {
		fmt.Fprintf(writer, "Template not found. Error: %s", err)
		return
	}

	// render the sitemap content
	sitemapContentModel := mapper.MapSitemap(rootItem)
	sitemapContent := renderSitemapEntry(sitemapContentTemplate, sitemapContentModel)

	sitemapPageModel := view.Model{
		Title:                "Sitemap",
		Description:          "A list of all items in this repository.",
		Content:              sitemapContent,
		ToplevelNavigation:   rootItem.ToplevelNavigation,
		BreadcrumbNavigation: rootItem.BreadcrumbNavigation,
		Type:                 "sitemap",
	}

	writeTemplate(sitemapPageModel, sitemapTemplate, writer)
}

func renderSitemapEntry(templ *template.Template, sitemapModel *view.Sitemap) string {

	// render
	buffer := new(bytes.Buffer)
	writeTemplate(sitemapModel, templ, buffer)

	// get the produced html code
	rootCode := buffer.String()

	if len(sitemapModel.Childs) > 0 {

		// render all childs

		childCode := ""
		for _, child := range sitemapModel.Childs {
			childCode += "\n" + renderSitemapEntry(templ, child)
{'fraction_non_alphanumeric': 0.10368561278863232, 'fraction_numerical': 0.002220248667850799, 'mean_word_length': 4.196078431372549, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3994807', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1437, 'n_words': 369}