~emersion/gyosu

074d911d37de9143d9047a2dd16953aeac684184 — Simon Ser 9 days ago c0f5298
Add declaration list to index
4 files changed, 60 insertions(+), 24 deletions(-)

M main.go
M template.go
M template/index.html
M template/unit.html
M main.go => main.go +33 -17
@@ 523,18 523,22 @@ func main() {
	}

	declByFile := make(map[string][]declData)
	decls = nil
	for _, decl := range declMap {
		declByFile[decl.Filename] = append(declByFile[decl.Filename], decl)
		decls = append(decls, decl)
	}

	sort.Slice(decls, func(i, j int) bool {
		return decls[i].Name < decls[j].Name
	})

	tpl := template.New("gyosu")
	tpl.Funcs(template.FuncMap{
		"symhref": func(kind declKind, name string) string {
			panic("Unimplemented")
		},
		"unithref": func(name string) string {
			return filepath.ToSlash(name) + ".html"
			return symHref(declMap, kind, name)
		},
		"unithref": unitHref,
	})
	template.Must(tpl.ParseGlob("template/*.html"))



@@ 542,7 546,10 @@ func main() {
		log.Fatalf("failed to remove %q: %v", outputDir, err)
	}

	data := indexData{Title: "Documentation"}
	data := indexData{
		Title: "Documentation",
		Decls: decls,
	}
	for filename, _ := range declByFile {
		data.Filenames = append(data.Filenames, filename)
	}


@@ 567,23 574,16 @@ func main() {
		})

		symHref := func(kind declKind, name string) string {
			k := declKey{Kind: kind, Name: name}
			decl, ok := declMap[k]
			if !ok {
			target := symHref(declMap, kind, name)
			if target == "" {
				return ""
			}

			anchor := fmt.Sprintf("#%v-%v", kind, name)
			if decl.Filename == filename {
				return anchor
			}

			targetPath := filepath.ToSlash(decl.Filename + ".html")
			return relPath(basePath, targetPath) + anchor
			return relPath(basePath, target)
		}

		unitHref := func(name string) string {
			panic("Unimplemented")
			target := unitHref(name)
			return relPath(basePath, target)
		}

		tpl.Funcs(template.FuncMap{


@@ 595,3 595,19 @@ func main() {
		renderTemplate(tpl, outputFilename, "unit.html", &data)
	}
}

func unitHref(filename string) string {
	return filepath.ToSlash(filename + ".html")
}

func symHref(declMap map[declKey]declData, kind declKind, name string) string {
	k := declKey{Kind: kind, Name: name}
	decl, ok := declMap[k]
	if !ok {
		return ""
	}

	path := filepath.ToSlash(decl.Filename + ".html")
	anchor := fmt.Sprintf("#%v-%v", kind, name)
	return path + anchor
}

M template.go => template.go +12 -0
@@ 34,6 34,17 @@ func (decl *declData) key() declKey {
	return declKey{Name: decl.Name, Kind: decl.Kind}
}

func (decl *declData) Title() string {
	switch decl.Kind {
	case declFunc:
		return decl.Name + "()"
	case declVar:
		return decl.Name
	default:
		return string(decl.Kind) + " " + decl.Name
	}
}

type unitData struct {
	Title     string
	Decls     []declData


@@ 43,6 54,7 @@ type unitData struct {
type indexData struct {
	Title     string
	Filenames []string
	Decls     []declData
}

type declKey struct {

M template/index.html => template/index.html +14 -0
@@ 3,6 3,8 @@
<main>
	<h2>{{ .Title }}</h2>

	<h3>Files</h3>

	<ul>
		{{ range .Filenames }}
			<li>


@@ 12,6 14,18 @@
			</li>
		{{ end }}
	</ul>

	<h3>Declarations</h3>

	<ul>
		{{ range .Decls }}
			<li>
				<a href="{{ symhref .Kind .Name }}">
					{{ .Title }}
				</a>
			</li>
		{{ end }}
	</ul>
</main>

{{ template "foot.html" }}

M template/unit.html => template/unit.html +1 -7
@@ 8,13 8,7 @@
	{{ range .Decls }}
	<section class="{{ .Kind }}">
			<h3 id="{{ .Kind }}-{{ .Name }}">
				{{ if eq .Kind "func" }}
					{{ .Name }}()
				{{ else if eq .Kind "var" }}
					{{ .Name }}
				{{ else }}
					{{ .Kind }} {{ .Name }}
				{{ end }}
				{{ .Title }}
				<a href="#{{ .Kind }}-{{ .Name }}">¶</a>
			</h3>
			<pre class="prototype">