previewmd

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

commit 5aac27613d2922bb0106337c0ce3b6fcd849465d
Author: Brian C. Lane <bcl@brianlane.com>
Date:   Wed,  4 Nov 2020 07:53:45 -0800

Basic skeleton of file previewer

Diffstat:
Acmd/previewmd/main.go | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ago.mod | 3+++
2 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/cmd/previewmd/main.go b/cmd/previewmd/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os" + "time" +) + +type cmdlineArgs struct { + Debug bool // Extra debug output + Port int +} + +var cfg = cmdlineArgs{ + Debug: false, + Port: 8080, +} + +func init() { + flag.BoolVar(&cfg.Debug, "debug", cfg.Debug, "Extra debug output") + flag.IntVar(&cfg.Port, "port", cfg.Port, "Port to serve preview from") + + 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) + 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) + if err != nil { + fmt.Println(err) + time.Sleep(500 * time.Millisecond) + continue + } + if info.Size() != lastInfo.Size() || info.ModTime() != lastInfo.ModTime() { + lastInfo = info + + // Send message to preview + updatePreview <- filename + } + + time.Sleep(1 * time.Second) + } +} diff --git a/go.mod b/go.mod @@ -0,0 +1,3 @@ +module github.com/bcl/previewmd + +go 1.15