From 524b0140b514f9619b5956766b82dd1377c2968a Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Thu, 11 Jun 2020 22:20:17 +0200 Subject: [PATCH] Add export format: "markdown" --- main.go | 61 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index f48b157..bc4ad21 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "fmt" "html" "html/template" + "io" "log" "net/http" "net/url" @@ -441,6 +442,15 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } }() + format := r.URL.Query().Get("format") + if format == "" { + format = "html" + } + + if !(format == "html" || format == "markdown") { + http.Error(w, "unknown format", http.StatusBadRequest) + } + page := r.URL.Path[1:] if page == "" { page = "Home" @@ -501,32 +511,37 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { pageText = builder.String() } - pageText = renderLinks(pageText) + if format == "html" { + pageText = renderLinks(pageText) + pageText = renderMarkdown2(pageText) - pageText = renderMarkdown2(pageText) + pageBase := getPageBase() + data := indexPage{ + pageBaseInfo: pageBase, + Session: sess, + Title: cleanTitle(page), + Content: template.HTML(pageText), + Name: page, + Backrefs: mpPage.Refs, + } - pageBase := getPageBase() - data := indexPage{ - pageBaseInfo: pageBase, - Session: sess, - Title: cleanTitle(page), - Content: template.HTML(pageText), - Name: page, - Backrefs: mpPage.Refs, + t, err := template.ParseFiles("templates/layout.html", "templates/view.html") + if err != nil { + http.Error(w, err.Error(), 500) + return + } + err = t.Execute(w, data) + if err != nil { + log.Println(err) + // http.Error(w, err.Error(), 500) + return + } + } else if format == "markdown" { + _, err = io.WriteString(w, "# ") + _, err = io.WriteString(w, cleanTitle(page)) + _, err = io.WriteString(w, "\n\n") + _, err = io.WriteString(w, pageText) } - - t, err := template.ParseFiles("templates/layout.html", "templates/view.html") - if err != nil { - http.Error(w, err.Error(), 500) - return - } - err = t.Execute(w, data) - if err != nil { - log.Println(err) - // http.Error(w, err.Error(), 500) - return - } - } func renderLinks(pageText string) string {