1package main
  2
  3import (
  4	"fmt"
  5	"html/template"
  6	"net/http"
  7)
  8
  9func readmeHandler(w http.ResponseWriter, r *http.Request) {
 10	ctx, err := getRepoContext(w, r)
 11	if err != nil {
 12		http.Error(w, err.Error(), http.StatusInternalServerError)
 13		return
 14	}
 15
 16	if ctx.ReadmeName == "" {
 17		http.NotFound(w, r)
 18		return
 19	}
 20
 21	commit, err := ctx.GitRepo.CommitObject(ctx.Hash)
 22	if err != nil {
 23		http.Error(w, fmt.Sprintf("Error getting commit: %v", err), http.StatusInternalServerError)
 24		return
 25	}
 26
 27	file, err := commit.File(ctx.ReadmeName)
 28	if err != nil {
 29		http.NotFound(w, r)
 30		return
 31	}
 32
 33	content, err := file.Contents()
 34	if err != nil {
 35		http.Error(w, fmt.Sprintf("Error reading file: %v", err), http.StatusInternalServerError)
 36		return
 37	}
 38
 39	data := struct {
 40		*RepoContext
 41		Content template.HTML
 42	}{
 43		RepoContext: ctx,
 44		Content:     renderMarkdown(content),
 45	}
 46
 47	err = templates.ExecuteTemplate(w, "readme.html", data)
 48	if err != nil {
 49		http.Error(w, err.Error(), http.StatusInternalServerError)
 50	}
 51}
 52
 53func licenseHandler(w http.ResponseWriter, r *http.Request) {
 54	ctx, err := getRepoContext(w, r)
 55	if err != nil {
 56		http.Error(w, err.Error(), http.StatusInternalServerError)
 57		return
 58	}
 59
 60	if ctx.LicenseName == "" {
 61		http.NotFound(w, r)
 62		return
 63	}
 64
 65	commit, err := ctx.GitRepo.CommitObject(ctx.Hash)
 66	if err != nil {
 67		http.Error(w, fmt.Sprintf("Error getting commit: %v", err), http.StatusInternalServerError)
 68		return
 69	}
 70
 71	file, err := commit.File(ctx.LicenseName)
 72	if err != nil {
 73		http.NotFound(w, r)
 74		return
 75	}
 76
 77	content, err := file.Contents()
 78	if err != nil {
 79		http.Error(w, fmt.Sprintf("Error reading file: %v", err), http.StatusInternalServerError)
 80		return
 81	}
 82
 83	data := struct {
 84		*RepoContext
 85		Content template.HTML
 86	}{
 87		RepoContext: ctx,
 88		Content:     renderMarkdown(content),
 89	}
 90
 91	err = templates.ExecuteTemplate(w, "license.html", data)
 92	if err != nil {
 93		http.Error(w, err.Error(), http.StatusInternalServerError)
 94	}
 95}
 96
 97func markersHandler(w http.ResponseWriter, r *http.Request) {
 98	ctx, err := getRepoContext(w, r)
 99	if err != nil {
100		http.Error(w, err.Error(), http.StatusInternalServerError)
101		return
102	}
103
104	markers, err := scanMarkers(ctx)
105	if err != nil {
106		http.Error(w, fmt.Sprintf("Error scanning markers: %v", err), http.StatusInternalServerError)
107		return
108	}
109
110	data := struct {
111		*RepoContext
112		Markers []Marker
113	}{
114		RepoContext: ctx,
115		Markers:     markers,
116	}
117
118	err = templates.ExecuteTemplate(w, "markers.html", data)
119	if err != nil {
120		http.Error(w, err.Error(), http.StatusInternalServerError)
121	}
122}