microblog.go hinzugefügt
This commit is contained in:
+106
@@ -0,0 +1,106 @@
|
|||||||
|
/**
|
||||||
|
* microblog in go
|
||||||
|
*
|
||||||
|
* get goldmark md parser
|
||||||
|
* go get github.com/yuin/goldmark
|
||||||
|
**/
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/yuin/goldmark"
|
||||||
|
)
|
||||||
|
|
||||||
|
const articlesDir = "data/articles"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handlePage)
|
||||||
|
println("Server läuft auf http://localhost:8080")
|
||||||
|
http.ListenAndServe(":8080", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func handlePage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
path := r.URL.Path
|
||||||
|
if path == "/" {
|
||||||
|
path = "/index"
|
||||||
|
}
|
||||||
|
// Entferne führenden Slash
|
||||||
|
pagePath := strings.TrimPrefix(path, "/")
|
||||||
|
// Baue den Pfad zur Markdown-Datei
|
||||||
|
mdPath := filepath.Join(articlesDir, filepath.FromSlash(pagePath)+".md")
|
||||||
|
|
||||||
|
// Überprüfe, ob die Datei existiert
|
||||||
|
if _, err := os.Stat(mdPath); os.IsNotExist(err) {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Datei lesen
|
||||||
|
data, err := ioutil.ReadFile(mdPath)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Fehler beim Lesen der Seite", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Markdown in HTML umwandeln mit Goldmark
|
||||||
|
htmlContent, err := markdownToHTML(data)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Fehler bei der Konvertierung", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seiten-Titel aus Dateiname oder erster Überschrift (optional)
|
||||||
|
title := filepath.Base(pagePath)
|
||||||
|
title = strings.ReplaceAll(title, "_", " ")
|
||||||
|
title = strings.Title(title)
|
||||||
|
|
||||||
|
// HTML-Template
|
||||||
|
tmpl := `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||||
|
<title>{{.Title}}</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: sans-serif; margin: 0; padding: 1em; max-width: 800px; margin: auto; }
|
||||||
|
h1 { margin-top: 0; }
|
||||||
|
a { color: #333; text-decoration: none; }
|
||||||
|
a:hover { text-decoration: underline; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>{{.Title}}</h1>
|
||||||
|
<div>{{.Content}}</div>
|
||||||
|
<p><a href="/">Startseite</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
|
||||||
|
t, err := template.New("page").Parse(tmpl)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Template Fehler", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Execute(w, map[string]interface{}{
|
||||||
|
"Title": title,
|
||||||
|
"Content": template.HTML(htmlContent),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Funktion, um Markdown mit Goldmark in HTML umzuwandeln
|
||||||
|
func markdownToHTML(md []byte) ([]byte, error) {
|
||||||
|
var buf strings.Builder
|
||||||
|
mdParser := goldmark.New()
|
||||||
|
if err := mdParser.Convert(md, &buf); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return []byte(buf.String()), nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user