]> Sergey Matveev's repositories - paster.git/blobdiff - main.go
Texinfo documentation, website and asciicasts
[paster.git] / main.go
diff --git a/main.go b/main.go
index 139b5366d31f9caf6c75b001de1f671ab365877b..89bcc6eabdb6010ae235c37b162fb06aae213f65 100644 (file)
--- a/main.go
+++ b/main.go
@@ -22,22 +22,57 @@ import (
        "bytes"
        "crypto/rand"
        "crypto/sha512"
+       _ "embed"
        "encoding/base32"
        "encoding/hex"
        "flag"
        "fmt"
+       "html/template"
        "io"
        "os"
        "strconv"
 )
 
+var (
+       //go:embed asciicast.tmpl
+       ASCIICastHTMLTmplRaw string
+       ASCIICastHTMLTmpl    = template.Must(template.New("asciicast").Parse(
+               ASCIICastHTMLTmplRaw,
+       ))
+)
+
 func fatal(s string) {
        fmt.Println(s)
        os.Exit(1)
 }
 
+func asciicastHTML(playerPath, cast string) error {
+       var buf bytes.Buffer
+       err := ASCIICastHTMLTmpl.Execute(&buf, struct {
+               PlayerPath string
+               Cast       string
+       }{
+               PlayerPath: playerPath,
+               Cast:       cast,
+       })
+       if err != nil {
+               return err
+       }
+       fn := cast + ".html"
+       fd, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.FileMode(0666))
+       if err != nil {
+               return err
+       }
+       if _, err = fd.Write(buf.Bytes()); err != nil {
+               os.Remove(fn)
+               return err
+       }
+       return fd.Close()
+}
+
 func main() {
        maxSize := flag.Uint64("max-size", 1<<20, "Maximal upload size")
+       asciicastPath := flag.String("asciicast-path", "", "Generate HTMLs for .cast asciicasts, specify \"asciinema-player-v2.6.1\"")
        flag.Usage = func() {
                fmt.Fprintf(os.Stderr, "Usage: paster [options] URL [...]\n")
                flag.PrintDefaults()
@@ -150,7 +185,15 @@ AnotherKey:
        for _, u := range flag.Args() {
                fmt.Println(u + fn[1:])
        }
-       fmt.Println(hex.EncodeToString(h.Sum(nil)[:512/2/8]))
+       fmt.Println("SHA512/2:", hex.EncodeToString(h.Sum(nil)[:512/2/8]))
+       if ext == ".cast" && *asciicastPath != "" {
+               if err = asciicastHTML(*asciicastPath, fn[1:]); err != nil {
+                       goto Failed
+               }
+               for _, u := range flag.Args() {
+                       fmt.Println(u + fn[1:] + ".html")
+               }
+       }
        fmt.Fprintf(
                os.Stderr,
                "[%s]:%s %s %d\n",