Compare commits
No commits in common. "04e19f51862c7b0395586131a22146c63932d351" and "63d18d068d3b01041a4d1e73cf5eb78c6d3eac27" have entirely different histories.
04e19f5186
...
63d18d068d
411
hswg/main.go
411
hswg/main.go
@ -4,7 +4,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
@ -12,12 +11,10 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@ -137,13 +134,12 @@ func Render(r io.Reader, config configuration.Configuration) (
|
|||||||
|
|
||||||
// Entry contains all context information about a single page.
|
// Entry contains all context information about a single page.
|
||||||
type Entry struct {
|
type Entry struct {
|
||||||
Metadata // metadata
|
Metadata // metadata
|
||||||
PathSource string // path to source AsciiDoc
|
PathSource string // path to source AsciiDoc
|
||||||
PathDestination string // path to destination HTML
|
PathDestination string // path to destination HTML
|
||||||
mtime time.Time // modification time
|
mtime time.Time // modification time
|
||||||
raw []byte // raw inner document
|
Content template.HTML // inner document with expanded LinkWords
|
||||||
Content template.HTML // inner document with expanded LinkWords
|
backlinks []string // what documents link back here
|
||||||
backlinks map[string]bool // what documents link back here
|
|
||||||
Backlinks []template.HTML
|
Backlinks []template.HTML
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,14 +156,7 @@ func (e *Entry) Published() *time.Time {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var extRE = regexp.MustCompile(`\.[^/.]*$`)
|
||||||
globs = []string{"*.adoc", "*.asciidoc"}
|
|
||||||
extRE = regexp.MustCompile(`\.[^/.]*$`)
|
|
||||||
)
|
|
||||||
|
|
||||||
func pathToName(path string) string {
|
|
||||||
return stripExtension(filepath.Base(path))
|
|
||||||
}
|
|
||||||
|
|
||||||
func stripExtension(path string) string {
|
func stripExtension(path string) string {
|
||||||
return extRE.ReplaceAllString(path, "")
|
return extRE.ReplaceAllString(path, "")
|
||||||
@ -182,8 +171,7 @@ func resultPath(path string) string {
|
|||||||
|
|
||||||
func makeLink(m *map[string]*Entry, name string) string {
|
func makeLink(m *map[string]*Entry, name string) string {
|
||||||
e := (*m)[name]
|
e := (*m)[name]
|
||||||
return fmt.Sprintf("<a href='%s'>%s</a>",
|
return fmt.Sprintf("<a href='%s'>%s</a>", e.PathDestination, name)
|
||||||
filepath.Clean(e.PathDestination), name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var linkWordRE = regexp.MustCompile(`\b\p{Lu}\p{L}*\b`)
|
var linkWordRE = regexp.MustCompile(`\b\p{Lu}\p{L}*\b`)
|
||||||
@ -192,96 +180,132 @@ func expand(m *map[string]*Entry, name string, chunk []byte) []byte {
|
|||||||
return linkWordRE.ReplaceAllFunc(chunk, func(match []byte) []byte {
|
return linkWordRE.ReplaceAllFunc(chunk, func(match []byte) []byte {
|
||||||
if link, ok := (*m)[string(match)]; ok && string(match) != name &&
|
if link, ok := (*m)[string(match)]; ok && string(match) != name &&
|
||||||
!link.IsDraft() {
|
!link.IsDraft() {
|
||||||
link.backlinks[name] = true
|
link.backlinks = append(link.backlinks, name)
|
||||||
return []byte(makeLink(m, string(match)))
|
return []byte(makeLink(m, string(match)))
|
||||||
}
|
}
|
||||||
return match
|
return match
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var tagRE = regexp.MustCompile(`<[^<>]+>`)
|
func singleFile() {
|
||||||
|
html, meta, err := Render(os.Stdin, configuration.NewConfiguration())
|
||||||
func renderEntry(name string, e *Entry) error {
|
|
||||||
f, err := os.Open(e.PathSource)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Println(err)
|
||||||
|
} else if meta.Title != "" {
|
||||||
|
_, _ = os.Stdout.WriteString("<h1>")
|
||||||
|
_ = xml.EscapeText(os.Stdout, []byte(meta.Title))
|
||||||
|
_, _ = os.Stdout.WriteString("</h1>\n")
|
||||||
}
|
}
|
||||||
|
_, _ = io.Copy(os.Stdout, html)
|
||||||
if i, err := f.Stat(); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
e.mtime = i.ModTime()
|
|
||||||
}
|
|
||||||
|
|
||||||
var html *bytes.Buffer
|
|
||||||
if html, e.Metadata, err = Render(f, configuration.NewConfiguration(
|
|
||||||
configuration.WithFilename(e.PathSource),
|
|
||||||
configuration.WithLastUpdated(e.mtime),
|
|
||||||
)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Every page needs to have a title.
|
|
||||||
if e.Title == "" {
|
|
||||||
e.Title = name
|
|
||||||
}
|
|
||||||
|
|
||||||
e.raw = html.Bytes()
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeEntry(path string) *Entry {
|
func main() {
|
||||||
return &Entry{
|
if len(os.Args) < 2 {
|
||||||
PathSource: path,
|
singleFile()
|
||||||
PathDestination: resultPath(path),
|
return
|
||||||
|
}
|
||||||
|
if len(os.Args) < 3 {
|
||||||
|
log.Fatalf("usage: %s TEMPLATE GLOB...\n", os.Args[0])
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// loadEntries creates a map from document names to their page entries.
|
// Read the common page header.
|
||||||
func loadEntries(dirname string) (map[string]*Entry, error) {
|
header, err := ioutil.ReadFile(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := template.New("page").Parse(string(header))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a map from document names to their page entries.
|
||||||
entries := map[string]*Entry{}
|
entries := map[string]*Entry{}
|
||||||
for _, glob := range globs {
|
for _, glob := range os.Args[2:] {
|
||||||
matches, err := filepath.Glob(filepath.Join(dirname, glob))
|
matches, err := filepath.Glob(glob)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %s", dirname, err)
|
log.Fatalf("%s: %s\n", glob, err)
|
||||||
}
|
}
|
||||||
for _, path := range matches {
|
for _, path := range matches {
|
||||||
name := pathToName(path)
|
name := stripExtension(filepath.Base(path))
|
||||||
if conflict, ok := entries[name]; ok {
|
if conflict, ok := entries[name]; ok {
|
||||||
return nil, fmt.Errorf("%s: conflicts with %s",
|
log.Fatalf("%s: conflicts with %s\n", name, conflict.PathSource)
|
||||||
name, conflict.PathSource)
|
}
|
||||||
|
entries[name] = &Entry{
|
||||||
|
PathSource: path,
|
||||||
|
PathDestination: resultPath(path),
|
||||||
}
|
}
|
||||||
entries[name] = makeEntry(path)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return entries, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeEntry(e *Entry, t *template.Template,
|
tagRE := regexp.MustCompile(`<[^<>]+>`)
|
||||||
entries *map[string]*Entry) error {
|
for name, e := range entries {
|
||||||
f, err := os.Create(e.PathDestination)
|
f, err := os.Open(e.PathSource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if i, err := f.Stat(); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
} else {
|
||||||
|
e.mtime = i.ModTime()
|
||||||
|
}
|
||||||
|
|
||||||
|
var html *bytes.Buffer
|
||||||
|
if html, e.Metadata, err = Render(f, configuration.NewConfiguration(
|
||||||
|
configuration.WithFilename(e.PathSource),
|
||||||
|
configuration.WithLastUpdated(e.mtime),
|
||||||
|
)); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Every page needs to have a title.
|
||||||
|
if e.Title == "" {
|
||||||
|
e.Title = name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expand LinkWords anywhere between <tags>.
|
||||||
|
// We want something like the inverse of Regexp.ReplaceAllStringFunc.
|
||||||
|
raw, last, expanded := html.Bytes(), 0, bytes.NewBuffer(nil)
|
||||||
|
for _, where := range tagRE.FindAllIndex(raw, -1) {
|
||||||
|
_, _ = expanded.Write(expand(&entries, name, raw[last:where[0]]))
|
||||||
|
_, _ = expanded.Write(raw[where[0]:where[1]])
|
||||||
|
last = where[1]
|
||||||
|
}
|
||||||
|
_, _ = expanded.Write(expand(&entries, name, raw[last:]))
|
||||||
|
e.Content = template.HTML(expanded.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
backlinks := []string{}
|
for _, e := range entries {
|
||||||
for name := range e.backlinks {
|
sort.Strings(e.backlinks)
|
||||||
backlinks = append(backlinks, name)
|
|
||||||
}
|
last, uniq := "", []string{}
|
||||||
sort.Strings(backlinks)
|
for _, name := range e.backlinks {
|
||||||
for _, name := range backlinks {
|
if name != last {
|
||||||
e.Backlinks =
|
uniq = append(uniq, name)
|
||||||
append(e.Backlinks, template.HTML(makeLink(entries, name)))
|
}
|
||||||
|
last = name
|
||||||
|
}
|
||||||
|
e.backlinks = uniq
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.Execute(f, e)
|
for _, e := range entries {
|
||||||
}
|
f, err := os.Create(e.PathDestination)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
for _, name := range e.backlinks {
|
||||||
|
e.Backlinks = append(e.Backlinks,
|
||||||
|
template.HTML(makeLink(&entries, name)))
|
||||||
|
}
|
||||||
|
if err = t.Execute(f, e); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func writeIndex(path string, t *template.Template,
|
|
||||||
entries *map[string]*Entry) error {
|
|
||||||
// Reorder entries reversely, primarily by date, secondarily by filename.
|
// Reorder entries reversely, primarily by date, secondarily by filename.
|
||||||
ordered := []*Entry{}
|
ordered := []*Entry{}
|
||||||
for _, e := range *entries {
|
for _, e := range entries {
|
||||||
ordered = append(ordered, e)
|
ordered = append(ordered, e)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,226 +327,15 @@ func writeIndex(path string, t *template.Template,
|
|||||||
return p2.Before(*p1)
|
return p2.Before(*p1)
|
||||||
})
|
})
|
||||||
|
|
||||||
f, err := os.Create(path)
|
// Execute a template from the standard input.
|
||||||
if err != nil {
|
var input []byte
|
||||||
return err
|
if input, err = ioutil.ReadAll(os.Stdin); err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(p): Splitting content to categories would be nice. Or tags.
|
|
||||||
return t.Execute(f, ordered)
|
|
||||||
}
|
|
||||||
|
|
||||||
func finalizeEntries(entries *map[string]*Entry, t *template.Template,
|
|
||||||
indexPath string, indexT *template.Template) {
|
|
||||||
for name, e := range *entries {
|
|
||||||
e.backlinks = map[string]bool{}
|
|
||||||
if e.raw == nil {
|
|
||||||
if err := renderEntry(name, e); err != nil {
|
|
||||||
log.Printf("%s: %s\n", name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for name, e := range *entries {
|
|
||||||
// Expand LinkWords anywhere between <tags>.
|
|
||||||
// We want something like the inverse of Regexp.ReplaceAllStringFunc.
|
|
||||||
raw, last, expanded := e.raw, 0, bytes.NewBuffer(nil)
|
|
||||||
for _, where := range tagRE.FindAllIndex(raw, -1) {
|
|
||||||
_, _ = expanded.Write(expand(entries, name, raw[last:where[0]]))
|
|
||||||
_, _ = expanded.Write(raw[where[0]:where[1]])
|
|
||||||
last = where[1]
|
|
||||||
}
|
|
||||||
_, _ = expanded.Write(expand(entries, name, raw[last:]))
|
|
||||||
e.Content = template.HTML(expanded.String())
|
|
||||||
}
|
|
||||||
for name, e := range *entries {
|
|
||||||
// Don't overwrite failed renders.
|
|
||||||
if e.raw == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := writeEntry(e, t, entries); err != nil {
|
|
||||||
log.Printf("%s: %s\n", name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := writeIndex(indexPath, indexT, entries); err != nil {
|
|
||||||
log.Printf("%s: %s\n", indexPath, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type watchEvent struct {
|
|
||||||
path string // the path of the target
|
|
||||||
present bool // if not, the file has been removed
|
|
||||||
}
|
|
||||||
|
|
||||||
func dispatchEvents(dirname string, r io.Reader, ch chan<- *watchEvent) error {
|
|
||||||
var e syscall.InotifyEvent
|
|
||||||
for {
|
|
||||||
// FIXME(p): This has to respect the machine's endianness.
|
|
||||||
// Perhaps use the unsafe package.
|
|
||||||
err := binary.Read(r, binary.LittleEndian, &e)
|
|
||||||
if err == io.EOF {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case e.Mask&syscall.IN_IGNORED != 0:
|
|
||||||
return fmt.Errorf("watch removed by kernel")
|
|
||||||
case e.Mask&syscall.IN_Q_OVERFLOW != 0:
|
|
||||||
log.Println("inotify: queue overflowed")
|
|
||||||
ch <- nil
|
|
||||||
continue
|
|
||||||
case e.Len == 0:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
base := make([]byte, e.Len)
|
|
||||||
if n, err := r.Read(base); err != nil {
|
|
||||||
return err
|
|
||||||
} else if n < int(e.Len) {
|
|
||||||
return fmt.Errorf("short read")
|
|
||||||
}
|
|
||||||
|
|
||||||
basename, interesting := string(base[:bytes.IndexByte(base, 0)]), false
|
|
||||||
for _, glob := range globs {
|
|
||||||
if matches, _ := filepath.Match(glob, basename); matches {
|
|
||||||
interesting = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !interesting {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
event := &watchEvent{path: filepath.Join(dirname, basename)}
|
|
||||||
if e.Mask&syscall.IN_MODIFY != 0 || e.Mask&syscall.IN_MOVED_TO != 0 ||
|
|
||||||
e.Mask&syscall.IN_CLOSE_WRITE != 0 {
|
|
||||||
event.present = true
|
|
||||||
ch <- event
|
|
||||||
}
|
|
||||||
if e.Mask&syscall.IN_DELETE != 0 || e.Mask&syscall.IN_MOVED_FROM != 0 {
|
|
||||||
event.present = false
|
|
||||||
ch <- event
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func watchDirectory(dirname string) (<-chan *watchEvent, error) {
|
|
||||||
inotifyFD, err := syscall.InotifyInit1(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// We're ignoring IN_CREATE, as it doesn't seem to be useful,
|
|
||||||
// and we're leaving out IN_MODIFY since VIM always triggers IN_CLOSE_WRITE,
|
|
||||||
// saving us from having to coalesce plentiful similar events.
|
|
||||||
_, err = syscall.InotifyAddWatch(inotifyFD, dirname, syscall.IN_ONLYDIR|
|
|
||||||
syscall.IN_MOVE|syscall.IN_DELETE|syscall.IN_CLOSE_WRITE)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
inotifyFile := os.NewFile(uintptr(inotifyFD), "inotify")
|
|
||||||
buf := make([]byte, syscall.SizeofInotifyEvent+syscall.PathMax+1)
|
|
||||||
ch := make(chan *watchEvent)
|
|
||||||
go func() {
|
|
||||||
// Trigger an initial rendering run.
|
|
||||||
ch <- nil
|
|
||||||
|
|
||||||
defer close(ch)
|
|
||||||
for {
|
|
||||||
n, err := inotifyFile.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = dispatchEvents(dirname, bytes.NewReader(buf[:n]), ch)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("inotify: %s\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return ch, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func singleFile() {
|
|
||||||
html, meta, err := Render(os.Stdin, configuration.NewConfiguration())
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
} else if meta.Title != "" {
|
|
||||||
_, _ = os.Stdout.WriteString("<h1>")
|
|
||||||
_ = xml.EscapeText(os.Stdout, []byte(meta.Title))
|
|
||||||
_, _ = os.Stdout.WriteString("</h1>\n")
|
|
||||||
}
|
|
||||||
_, _ = io.Copy(os.Stdout, html)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
if len(os.Args) < 2 {
|
|
||||||
singleFile()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(os.Args) != 4 {
|
|
||||||
log.Fatalf("usage: %s TEMPLATE INDEX DIRECTORY\n", os.Args[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
argTemplate, argIndex, argDirectory := os.Args[1], os.Args[2], os.Args[3]
|
|
||||||
|
|
||||||
// Read a template for entries.
|
|
||||||
header, err := ioutil.ReadFile(argTemplate)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
tmplEntry, err := template.New("entry").Parse(string(header))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read a template for the index from the standard input.
|
// TODO(p): Splitting content to categories would be nice.
|
||||||
index, err := ioutil.ReadAll(os.Stdin)
|
t, err = template.New("-").Parse(string(input))
|
||||||
if err != nil {
|
if err = t.Execute(os.Stdout, ordered); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
tmplIndex, err := template.New("index").Parse(string(index))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Re-render as needed, avoid having to trigger anything manually.
|
|
||||||
var entries map[string]*Entry
|
|
||||||
directoryWatch, err := watchDirectory(argDirectory)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
signals := make(chan os.Signal)
|
|
||||||
signal.Notify(signals, syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-signals:
|
|
||||||
os.Exit(0)
|
|
||||||
case event, ok := <-directoryWatch:
|
|
||||||
if !ok {
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if event == nil {
|
|
||||||
log.Println("reloading all files")
|
|
||||||
if entries, err = loadEntries(argDirectory); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
} else if event.present {
|
|
||||||
log.Printf("updating %s\n", event.path)
|
|
||||||
entries[pathToName(event.path)] = makeEntry(event.path)
|
|
||||||
} else {
|
|
||||||
log.Printf("removing %s\n", event.path)
|
|
||||||
delete(entries, pathToName(event.path))
|
|
||||||
os.Remove(resultPath(event.path))
|
|
||||||
}
|
|
||||||
|
|
||||||
finalizeEntries(&entries, tmplEntry, argIndex, tmplIndex)
|
|
||||||
log.Println("done")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user