previewmd

Simple Markdown preview
git clone https://www.brianlane.com/git/previewmd
Log | Files | Refs | README

commit d286c19179e3c573c2f17113c856758a093ec336
parent 5aac27613d2922bb0106337c0ce3b6fcd849465d
Author: Brian C. Lane <bcl@brianlane.com>
Date:   Thu,  5 Nov 2020 07:50:20 -0800

Simplify to just a http handler

No need to 'watch' the file, use go routines or channels. Just re-read
it on each request.

Adds actual Markdown to HTML rendering using the blackfriday library.

Diffstat:
A.gitignore | 1+
AREADME.md | 10++++++++++
Mcmd/previewmd/main.go | 56+++++++++++++++++++++++---------------------------------
Mgo.mod | 5+++++
Ago.sum | 5+++++
5 files changed, 44 insertions(+), 33 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +previewmd diff --git a/README.md b/README.md @@ -0,0 +1,10 @@ +# Simple Markdown Preview + +No frills, just serve up the raw [Markdown](https://daringfireball.net/projects/markdown/syntax) html +to a port on localhost. + +Build it: `go build ./cmd/previewmd` + +Run it: `./previewmd -port 8080 README.md` + +Visit http://localhost:8080 diff --git a/cmd/previewmd/main.go b/cmd/previewmd/main.go @@ -3,9 +3,12 @@ package main import ( "flag" "fmt" + "io/ioutil" "log" + "net/http" "os" - "time" + + "github.com/russross/blackfriday/v2" ) type cmdlineArgs struct { @@ -25,53 +28,40 @@ func init() { flag.Parse() } -func PreviewMarkdown(update <-chan string, port int) { - - // Read file the first time - // Render html from markdown - - // Setup http server - for { - filename := <-update - fmt.Println(filename) - - // re-read the file - - // Render html from markdown - } -} - func main() { if flag.NArg() == 0 { flag.Usage() os.Exit(1) } filename := flag.Arg(0) - lastInfo, err := os.Stat(filename) + _, err := os.Stat(filename) if err != nil { log.Fatal(err) } fmt.Printf("Previewing %s on port %d\n", filename, cfg.Port) - updatePreview := make(chan string, 1) - go PreviewMarkdown(updatePreview, cfg.Port) - - // Loop, checking the file mtime every second - // If it has been modified, tell preview to update - for { - info, err := os.Stat(filename) + preview := func(w http.ResponseWriter, _ *http.Request) { + f, err := os.Open(filename) if err != nil { - fmt.Println(err) - time.Sleep(500 * time.Millisecond) - continue + fmt.Printf("Error opening %s: %s\n", filename, err) + return } - if info.Size() != lastInfo.Size() || info.ModTime() != lastInfo.ModTime() { - lastInfo = info - // Send message to preview - updatePreview <- filename + // XXX Is this right inside a loop? + defer f.Close() + markdown, err := ioutil.ReadAll(f) + if err != nil { + fmt.Printf("Error reading %s: %s\n", filename, err) + return } - time.Sleep(1 * time.Second) + // Render html from markdown + output := blackfriday.Run(markdown) + w.Write(output) } + + http.HandleFunc("/", preview) + + listen := fmt.Sprintf(":%d", cfg.Port) + log.Fatal(http.ListenAndServe(listen, nil)) } diff --git a/go.mod b/go.mod @@ -1,3 +1,8 @@ module github.com/bcl/previewmd go 1.15 + +require ( + github.com/russross/blackfriday/v2 v2.0.1 + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect +) diff --git a/go.sum b/go.sum @@ -0,0 +1,5 @@ +github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=