// Copyright 2024 Staysail Systems, Inc. // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this // file was obtained (LICENSE.txt). A copy of the license may also be // found online at https://opensource.org/licenses/MIT. // // This tool uses a local in memory copy of git, and a docker // installation, to format the man pages. The documentation will be // pushed up into a branch. package main import ( "context" "flag" "fmt" "io/ioutil" "log" "os" "os/exec" "path" "sort" "strings" "time" "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/memfs" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/storage/memory" "github.com/google/uuid" jww "github.com/spf13/jwalterweatherman" ) type Configuration struct { Version string Debug bool Trace bool Quiet bool DryRun bool Author string Email string Url string Message string } var Config Configuration func init() { flag.StringVar(&Config.Version, "v", "tip", "Version to publish") flag.BoolVar(&Config.Debug, "d", false, "Enable debugging") flag.BoolVar(&Config.Trace, "t", false, "Enable tracing") flag.BoolVar(&Config.Quiet, "q", false, "Run quietly") flag.BoolVar(&Config.DryRun, "n", false, "Dry run, does not push changes") flag.StringVar(&Config.Url, "u", "ssh://git@github.com/nanomsg/nng.git", "URL of repo to publish from") flag.StringVar(&Config.Email, "E", "info@staysail.tech", "Author email for commit") flag.StringVar(&Config.Author, "A", "Staysail Systems, Inc.", "Author name for commit") flag.StringVar(&Config.Message, "m", "", "Commit message") } func (g *Global) CheckError(err error, prefix string, args ...interface{}) { if err == nil { g.Log.TRACE.Printf("%s: ok", fmt.Sprintf(prefix, args...)) return } g.Log.FATAL.Fatalf("Error: %s: %v", fmt.Sprintf(prefix, args...), err) } func (g *Global) Fatal(format string, args ...interface{}) { g.Log.FATAL.Fatalf("Error: %s", fmt.Sprintf(format, args...)) } type Section struct { Name string Synopsis string Description string Pages []*Page } type Page struct { Name string Section string Description string Content string } type Global struct { Config Configuration SrcFs billy.Filesystem DstFs billy.Filesystem DstDir string Sections map[string]*Section Pages map[string]*Page Repo *git.Repository Index string ToC string Added map[string]bool WorkTree *git.Worktree Branch string OldHash plumbing.Hash NewHash plumbing.Hash Log *jww.Notepad } func (g *Global) Init() { g.Config = Config g.Sections = make(map[string]*Section) g.Pages = make(map[string]*Page) g.Added = make(map[string]bool) g.SrcFs = memfs.New() g.DstFs = memfs.New() g.DstDir = path.Join("man", g.Config.Version) thresh := jww.LevelInfo if g.Config.Quiet { thresh = jww.LevelError } if g.Config.Debug { thresh = jww.LevelDebug } if g.Config.Trace { thresh = jww.LevelTrace } g.Log = jww.NewNotepad(thresh, thresh, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime) } func (g *Global) Destroy() { } func (g *Global) Debug(format string, args ...interface{}) { g.Log.DEBUG.Printf(format, args...) } func (g *Global) Print(format string, args ...interface{}) { g.Log.INFO.Printf(format, args...) } func (g *Global) CloneSource() { tag := g.Config.Version if tag == "" || tag == "tip" { tag = "master" } ref := plumbing.NewBranchReferenceName(tag) if strings.HasPrefix(tag, "v") { ref = plumbing.NewTagReferenceName(tag) } ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() now := time.Now() _, err := git.CloneContext(ctx, memory.NewStorage(), g.SrcFs, &git.CloneOptions{ URL: g.Config.Url, ReferenceName: ref, }) g.CheckError(err, "clone source") g.Debug("Cloned source (%s) in %v", tag, time.Since(now)) } func (g *Global) ClonePages() { ref := plumbing.NewBranchReferenceName("gh-pages") ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() now := time.Now() repo, err := git.CloneContext(ctx, memory.NewStorage(), g.DstFs, &git.CloneOptions{ URL: g.Config.Url, ReferenceName: ref, RemoteName: "origin", }) g.CheckError(err, "clone gh-pages") g.Repo = repo g.Debug("Cloned pages in %v", time.Since(now)) } func (g *Global) DockerCmd(source string, output *strings.Builder) *exec.Cmd { cmd := exec.Command("/usr/local/bin/docker", "run", "-i", "--rm", "asciidoctor/docker-asciidoctor") cmd.Args = append(cmd.Args, "asciidoctor") cmd.Args = append(cmd.Args, "-a", "linkcss") cmd.Args = append(cmd.Args, "-a", "icons=font") cmd.Args = append(cmd.Args, "-a", "nofooter") cmd.Args = append(cmd.Args, "-a", "source-highlighter=pygments") // rouge is recommended, but our stylesheet is for pygments cmd.Args = append(cmd.Args, "-s", "-o", "-", "-") // pipe it cmd.Stdin = strings.NewReader(source) cmd.Stdout = output cmd.Stderr = &strings.Builder{} return cmd } func (g *Global) ProcessManPage(page os.FileInfo) { source := g.ReadFile(page.Name()) // Let's look for the description inName := false desc := "" name := "" title := "" for _, line := range strings.Split(source, "\n") { line = strings.TrimRight(line, " \t\r") if line == "" { continue } if strings.HasPrefix(line, "= ") { title = strings.TrimSpace(line[2:]) continue } if line == "== NAME" { inName = true continue } if inName { w := strings.SplitN(line, " - ", 2) if len(w) != 2 || w[1] == "" { g.Fatal("page %s NAME malformed", page.Name()) } name = w[0] desc = w[1] strings.TrimSpace(name) strings.TrimSpace(desc) break } } if desc == "" { g.Fatal("page %s NAME missing", page.Name()) } if title == "" { g.Fatal("page %s title missing", page.Name()) } html := &strings.Builder{} w := strings.SplitN(title, "(", 2) sect := strings.TrimSuffix(w[1], ")") if len(w) != 2 || name != w[0] || !strings.HasSuffix(w[1], ")") { g.Fatal("page %s title incorrect (%s)", page.Name(), name) } if page.Name() != name+"."+sect+".adoc" { g.Fatal("page %s(%s) does not match file name %s", name, sect, page.Name()) } _, _ = fmt.Fprintf(html, "---\n") _, _ = fmt.Fprintf(html, "version: %s\n", g.Config.Version) _, _ = fmt.Fprintf(html, "layout: %s\n", "manpage_v2") _, _ = fmt.Fprintf(html, "title: %s\n", fmt.Sprintf("%s(%s)", name, sect)) _, _ = fmt.Fprintf(html, "---\n") _, _ = fmt.Fprintf(html, "