forked from mirror/oddmu
If any templates are missing when templates are initialized, an embedded version is written into the working directory.
91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/subcommands"
|
|
)
|
|
|
|
type feedCmd struct {
|
|
}
|
|
|
|
func (*feedCmd) Name() string { return "feed" }
|
|
func (*feedCmd) Synopsis() string { return "render a page as feed" }
|
|
func (*feedCmd) Usage() string {
|
|
return `feed <page name> ...:
|
|
Render one or more pages as a single feed.
|
|
Use a single - to read Markdown from stdin.
|
|
`
|
|
}
|
|
|
|
func (cmd *feedCmd) SetFlags(f *flag.FlagSet) {
|
|
}
|
|
|
|
func (cmd *feedCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
|
|
if len(f.Args()) == 0 {
|
|
fmt.Fprint(os.Stderr, cmd.Usage())
|
|
return subcommands.ExitFailure
|
|
}
|
|
return feedCli(os.Stdout, f.Args())
|
|
}
|
|
|
|
func feedCli(w io.Writer, args []string) subcommands.ExitStatus {
|
|
if len(args) == 1 && args[0] == "-" {
|
|
body, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Cannot read from stdin: %s\n", err)
|
|
return subcommands.ExitFailure
|
|
}
|
|
p := &Page{Name: "stdin", Body: body}
|
|
return p.printFeed(w, time.Now())
|
|
}
|
|
for _, name := range args {
|
|
if !strings.HasSuffix(name, ".md") {
|
|
fmt.Fprintf(os.Stderr, "%s does not end in '.md'\n", name)
|
|
return subcommands.ExitFailure
|
|
}
|
|
name = name[0 : len(name)-3]
|
|
p, err := loadPage(name)
|
|
p.handleTitle(false)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Cannot load %s: %s\n", name, err)
|
|
return subcommands.ExitFailure
|
|
}
|
|
ti, _ := p.ModTime()
|
|
status := p.printFeed(w, ti)
|
|
if status != subcommands.ExitSuccess {
|
|
return status
|
|
}
|
|
}
|
|
return subcommands.ExitSuccess
|
|
}
|
|
|
|
// printFeed prints the complete feed for a page (unpaginated).
|
|
func (p *Page) printFeed(w io.Writer, ti time.Time) subcommands.ExitStatus {
|
|
f := feed(p, ti, 0, 0, URL)
|
|
if len(f.Items) == 0 {
|
|
fmt.Fprintf(os.Stderr, "Empty feed for %s\n", p.Name)
|
|
return subcommands.ExitFailure
|
|
}
|
|
_, err := w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>`))
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Cannot write prefix: %s\n", err)
|
|
return subcommands.ExitFailure
|
|
}
|
|
initTemplates()
|
|
templates.RLock()
|
|
defer templates.RUnlock()
|
|
err = templates.template["feed.html"].Execute(w, f)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Cannot execute template: %s\n", err)
|
|
return subcommands.ExitFailure
|
|
}
|
|
return subcommands.ExitSuccess
|
|
}
|