mdview/main.go

206 lines
4.1 KiB
Go
Raw Normal View History

2022-05-08 16:09:29 +00:00
package main
import (
2022-05-08 18:00:38 +00:00
_ "embed"
2024-04-05 03:50:14 +00:00
"errors"
2022-05-08 16:09:29 +00:00
"flag"
2022-05-08 17:19:48 +00:00
"fmt"
2022-05-08 16:09:29 +00:00
"log"
"os"
2022-05-08 17:19:48 +00:00
"os/signal"
2024-04-05 03:50:14 +00:00
"path"
2022-05-08 16:09:29 +00:00
"path/filepath"
2022-05-08 18:00:38 +00:00
"strings"
2022-05-08 17:19:48 +00:00
"syscall"
2022-05-08 16:09:29 +00:00
2023-12-31 03:58:29 +00:00
"git.foosoft.net/alex/goldsmith"
"git.foosoft.net/alex/goldsmith-components/filters/operator"
"git.foosoft.net/alex/goldsmith-components/filters/wildcard"
"git.foosoft.net/alex/goldsmith-components/plugins/document"
"git.foosoft.net/alex/goldsmith-components/plugins/frontmatter"
"git.foosoft.net/alex/goldsmith-components/plugins/livejs"
"git.foosoft.net/alex/goldsmith-components/plugins/markdown"
2022-05-08 18:00:38 +00:00
"github.com/PuerkitoBio/goquery"
2024-04-05 03:50:14 +00:00
"github.com/fsnotify/fsnotify"
2022-05-08 16:09:29 +00:00
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
)
2022-05-08 18:13:42 +00:00
//go:embed css/github-markdown.css
2022-05-08 18:00:38 +00:00
var githubStyle string
2022-05-08 18:13:42 +00:00
//go:embed css/github-fixup.css
2022-05-08 18:00:38 +00:00
var githubFixup string
2024-04-05 03:50:14 +00:00
func watch(dir string, watcher *fsnotify.Watcher) error {
watcher.Add(dir)
2022-05-08 16:09:29 +00:00
2024-04-05 03:50:14 +00:00
items, err := os.ReadDir(dir)
if err != nil {
return err
}
2022-05-08 18:00:38 +00:00
2024-04-05 03:50:14 +00:00
for _, item := range items {
fullPath := path.Join(dir, item.Name())
if item.IsDir() {
watch(fullPath, watcher)
} else {
watcher.Add(fullPath)
}
}
2022-05-08 18:00:38 +00:00
return nil
}
2024-04-05 03:50:14 +00:00
func builder(dir string, callback func()) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
if err := watch(dir, watcher); err != nil {
return err
}
2022-05-08 16:09:29 +00:00
2024-04-05 03:50:14 +00:00
var terminate bool
signaler := make(chan os.Signal, 1)
signal.Notify(
signaler,
os.Interrupt,
syscall.SIGINT,
syscall.SIGTERM,
2022-05-08 16:09:29 +00:00
)
2024-04-05 03:50:14 +00:00
callback()
for !terminate {
select {
case event := <-watcher.Events:
callback()
if event.Op&fsnotify.Create == fsnotify.Create {
if info, _ := os.Stat(event.Name); info != nil {
if info.IsDir() {
watch(event.Name, watcher)
} else {
watcher.Add(event.Name)
}
}
}
case <-signaler:
log.Println("terminating...")
terminate = true
}
}
return nil
}
func build(sourceDir, targetDir string) {
log.Println("building...")
embedCss := func(file *goldsmith.File, doc *goquery.Document) error {
var styleBuilder strings.Builder
styleBuilder.WriteString("<style type=\"text/css\">\n")
styleBuilder.WriteString(githubStyle)
styleBuilder.WriteString(githubFixup)
styleBuilder.WriteString("</style>")
doc.Find("body").AddClass("markdown-body")
doc.Find("head").SetHtml(styleBuilder.String())
return nil
}
2022-05-15 03:52:32 +00:00
allowedPaths := []string{
"**/*.gif",
"**/*.html",
"**/*.jpeg",
"**/*.jpg",
"**/*.md",
"**/*.png",
"**/*.svg",
}
forbiddenPaths := []string{
"**/.*/**",
}
2024-04-05 03:50:14 +00:00
gm := goldmark.New(
goldmark.WithExtensions(extension.GFM, extension.Typographer),
goldmark.WithParserOptions(parser.WithAutoHeadingID()),
goldmark.WithRendererOptions(html.WithUnsafe()),
)
2024-03-15 05:13:50 +00:00
var gs goldsmith.Goldsmith
2024-04-05 03:50:14 +00:00
errs := gs.Begin(sourceDir).
2022-05-08 16:09:29 +00:00
Clean(true).
2022-05-15 03:52:32 +00:00
FilterPush(wildcard.New(allowedPaths...)).
FilterPush(operator.Not(wildcard.New(forbiddenPaths...))).
2022-05-09 00:51:59 +00:00
Chain(frontmatter.New()).
2022-05-08 16:09:29 +00:00
Chain(markdown.NewWithGoldmark(gm)).
Chain(livejs.New()).
2022-05-08 18:00:38 +00:00
Chain(document.New(embedCss)).
2024-04-05 03:50:14 +00:00
End(targetDir)
2022-05-08 16:09:29 +00:00
for _, err := range errs {
2024-04-05 03:50:14 +00:00
log.Println(err)
2022-05-08 16:09:29 +00:00
}
2024-04-05 03:50:14 +00:00
}
2022-05-08 17:19:48 +00:00
2024-04-05 03:50:14 +00:00
func run(path string) error {
switch strings.ToLower(filepath.Ext(path)) {
case ".md", ".markdown":
break
default:
return errors.New("unexpected file type")
2022-05-08 17:19:48 +00:00
}
2024-04-05 03:50:14 +00:00
if info, err := os.Stat(path); err != nil {
return err
} else if info.IsDir() {
return errors.New("unexpected directory")
}
targetDir, err := os.MkdirTemp("", "mdv-*")
if err != nil {
return err
}
defer os.RemoveAll(targetDir)
sourceDir := filepath.Dir(path)
builder(sourceDir, func() {
build(sourceDir, targetDir)
})
// if !self.open {
// url := fmt.Sprintf("http://127.0.0.1:%d/%s", self.port, self.path)
// log.Printf("opening %s in browser...", url)
// webbrowser.Open(url)
// self.open = true
// }
return nil
2022-05-08 16:09:29 +00:00
}
func main() {
2022-05-08 23:59:37 +00:00
flag.Usage = func() {
2024-04-05 03:50:14 +00:00
fmt.Fprintf(os.Stderr, "Usage %s [options] <path>\n\n", filepath.Base(os.Args[0]))
2022-05-08 23:59:37 +00:00
fmt.Fprintln(os.Stderr, "Parameters:")
flag.PrintDefaults()
}
2022-05-08 16:09:29 +00:00
flag.Parse()
if flag.NArg() != 1 {
2022-05-08 23:59:37 +00:00
flag.Usage()
os.Exit(2)
2022-05-08 16:09:29 +00:00
}
2024-04-05 03:50:14 +00:00
if err := run(flag.Arg(0)); err != nil {
2022-05-08 16:09:29 +00:00
log.Fatal(err)
}
}