hswg: execute a template given on standard input
So that information can be extracted from documents easily.
This commit is contained in:
parent
ea8c59961c
commit
8f542c7120
58
hswg/main.go
58
hswg/main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
@ -127,13 +128,14 @@ func Render(r io.Reader, config configuration.Configuration) (
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// entry contains all context information about a single page.
|
// Entry contains all context information about a single page.
|
||||||
type entry struct {
|
type Entry struct {
|
||||||
path string // path
|
PathSource string // path to source AsciiDoc
|
||||||
mtime time.Time // modification time
|
PathDestination string // path to destination HTML
|
||||||
metadata Metadata // metadata
|
mtime time.Time // modification time
|
||||||
document []byte // inner document with expanded LinkWords
|
Metadata Metadata // metadata
|
||||||
backlinks []string // what documents link back here
|
document []byte // inner document with expanded LinkWords
|
||||||
|
backlinks []string // what documents link back here
|
||||||
}
|
}
|
||||||
|
|
||||||
var extRE = regexp.MustCompile(`\.[^/.]*$`)
|
var extRE = regexp.MustCompile(`\.[^/.]*$`)
|
||||||
|
@ -149,17 +151,17 @@ func resultPath(path string) string {
|
||||||
return path + ".html"
|
return path + ".html"
|
||||||
}
|
}
|
||||||
|
|
||||||
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>", resultPath(e.path), name)
|
return fmt.Sprintf("<a href='%s'>%s</a>", e.PathDestination, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
var linkWordRE = regexp.MustCompile(`\b\p{Lu}\p{L}*\b`)
|
var linkWordRE = regexp.MustCompile(`\b\p{Lu}\p{L}*\b`)
|
||||||
|
|
||||||
func expand(m *map[string]*entry, name string, chunk []byte) []byte {
|
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.metadata.IsDraft() {
|
!link.Metadata.IsDraft() {
|
||||||
link.backlinks = append(link.backlinks, name)
|
link.backlinks = append(link.backlinks, name)
|
||||||
return []byte(makeLink(m, string(match)))
|
return []byte(makeLink(m, string(match)))
|
||||||
}
|
}
|
||||||
|
@ -195,7 +197,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a map from document names to their page entries.
|
// Create a map from document names to their page entries.
|
||||||
entries := map[string]*entry{}
|
entries := map[string]*Entry{}
|
||||||
for _, glob := range os.Args[2:] {
|
for _, glob := range os.Args[2:] {
|
||||||
matches, err := filepath.Glob(glob)
|
matches, err := filepath.Glob(glob)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -204,15 +206,18 @@ func main() {
|
||||||
for _, path := range matches {
|
for _, path := range matches {
|
||||||
name := stripExtension(filepath.Base(path))
|
name := stripExtension(filepath.Base(path))
|
||||||
if conflict, ok := entries[name]; ok {
|
if conflict, ok := entries[name]; ok {
|
||||||
log.Fatalf("%s: conflicts with %s\n", name, conflict.path)
|
log.Fatalf("%s: conflicts with %s\n", name, conflict.PathSource)
|
||||||
|
}
|
||||||
|
entries[name] = &Entry{
|
||||||
|
PathSource: path,
|
||||||
|
PathDestination: resultPath(path),
|
||||||
}
|
}
|
||||||
entries[name] = &entry{path: path}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tagRE := regexp.MustCompile(`<[^<>]+>`)
|
tagRE := regexp.MustCompile(`<[^<>]+>`)
|
||||||
for name, e := range entries {
|
for name, e := range entries {
|
||||||
f, err := os.Open(e.path)
|
f, err := os.Open(e.PathSource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
@ -224,8 +229,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var html *bytes.Buffer
|
var html *bytes.Buffer
|
||||||
if html, e.metadata, err = Render(f, configuration.NewConfiguration(
|
if html, e.Metadata, err = Render(f, configuration.NewConfiguration(
|
||||||
configuration.WithFilename(e.path),
|
configuration.WithFilename(e.PathSource),
|
||||||
configuration.WithLastUpdated(e.mtime),
|
configuration.WithLastUpdated(e.mtime),
|
||||||
)); err != nil {
|
)); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
@ -243,15 +248,16 @@ func main() {
|
||||||
e.document = expanded.Bytes()
|
e.document = expanded.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(p): These should be run through html/template.
|
||||||
for name, e := range entries {
|
for name, e := range entries {
|
||||||
f, err := os.Create(resultPath(e.path))
|
f, err := os.Create(e.PathDestination)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _ = f.Write(header)
|
_, _ = f.Write(header)
|
||||||
|
|
||||||
title := e.metadata.Title
|
title := e.Metadata.Title
|
||||||
if title == "" {
|
if title == "" {
|
||||||
title = name
|
title = name
|
||||||
}
|
}
|
||||||
|
@ -277,6 +283,18 @@ func main() {
|
||||||
_, _ = f.Write(e.document)
|
_, _ = f.Write(e.document)
|
||||||
_, _ = f.WriteString(fmt.Sprintf("<p id=footer>Last updated: %s"+
|
_, _ = f.WriteString(fmt.Sprintf("<p id=footer>Last updated: %s"+
|
||||||
" — <a href='%s'>Source</p>\n",
|
" — <a href='%s'>Source</p>\n",
|
||||||
e.metadata.LastUpdated, e.path))
|
e.Metadata.LastUpdated, e.PathSource))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute a template from the standard input.
|
||||||
|
var input []byte
|
||||||
|
if input, err = ioutil.ReadAll(os.Stdin); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(p): Splitting content to categories would be nice.
|
||||||
|
t, err := template.New("-").Parse(string(input))
|
||||||
|
if err = t.Execute(os.Stdout, entries); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue