From 944cf9fb8b555f991003665fa433df9706da581e Mon Sep 17 00:00:00 2001 From: legitnull Date: Sun, 2 Apr 2023 12:55:48 -0600 Subject: [PATCH] add a simple layout html wrapper for the markdown pages with css support --- assets/_layout.html | 24 ++++++++++++++++++++++++ assets/main.css | 42 ++++++++++++++++++++++++++++++++++++++++++ render.go | 30 +++++++++++++++++++++++++++++- 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 assets/_layout.html create mode 100644 assets/main.css diff --git a/assets/_layout.html b/assets/_layout.html new file mode 100644 index 0000000..8e5f6c6 --- /dev/null +++ b/assets/_layout.html @@ -0,0 +1,24 @@ + + + + + + + TCP Wiki + + + +
+

TCP Wiki

+ +
+
+ {{.Content}} +
+ + diff --git a/assets/main.css b/assets/main.css new file mode 100644 index 0000000..f3bbb8a --- /dev/null +++ b/assets/main.css @@ -0,0 +1,42 @@ +/* assets/main.css */ + +/* placeholder for better sytle */ +body { + font-family: Arial, sans-serif; + line-height: 1.6; + margin: 20px; +} + +h1, h2, h3, h4, h5, h6 { + font-weight: bold; +} + +a { + color: #333; + text-decoration: none; +} + +a:hover { + color: #777; +} + +header { + padding: 1em; + background-color: #f1f1f1; + border-bottom: 1px solid #ccc; +} + +header h1 { + display: inline-block; + margin-right: 1em; +} + +nav ul { + list-style: none; + padding: 0; +} + +nav ul li { + display: inline; + margin-right: 1em; +} diff --git a/render.go b/render.go index 31792bc..f8ab888 100644 --- a/render.go +++ b/render.go @@ -1,13 +1,20 @@ package main import ( + "bytes" "fmt" + "html/template" + "io/ioutil" "net/http" "path/filepath" "github.com/russross/blackfriday/v2" ) +type Page struct { + Content template.HTML +} + func renderPage(w http.ResponseWriter, localPath, filePath string) error { content, err := readFileFromRepo(localPath, filePath) if err != nil { @@ -28,8 +35,29 @@ func renderPage(w http.ResponseWriter, localPath, filePath string) error { func renderMarkdown(w http.ResponseWriter, content []byte) { md := blackfriday.Run(content) + + layout, err := ioutil.ReadFile(filepath.Join(localPath, "assets/_layout.html")) + if err != nil { + http.Error(w, "Layout not found", http.StatusInternalServerError) + return + } + + page := &Page{Content: template.HTML(md)} + t, err := template.New("layout").Parse(string(layout)) + if err != nil { + http.Error(w, "Error parsing layout", http.StatusInternalServerError) + return + } + + var buf bytes.Buffer + err = t.Execute(&buf, page) + if err != nil { + http.Error(w, "Error rendering layout", http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.Write(md) + w.Write(buf.Bytes()) } func renderStatic(w http.ResponseWriter, content []byte, ext string) {