Skip to content

Commit

Permalink
Merge branch 'slides'
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Jan 31, 2024
2 parents 4d286cc + 4a1bbd7 commit dab8bfe
Show file tree
Hide file tree
Showing 23 changed files with 842 additions and 142 deletions.
9 changes: 9 additions & 0 deletions cmd/hype/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,22 @@ func New(root string) *App {
Parser: p,
}

sl := &Slides{
Cmd: cleo.Cmd{
Name: "slides",
Aliases: []string{"s"},
},
Parser: p,
}

app := &App{
Cmd: cleo.Cmd{
Name: "hype",
FS: cab,
Commands: map[string]cleo.Commander{
"marked": m,
"preview": mp,
"slides": sl,
},
},
Parser: p,
Expand Down
39 changes: 39 additions & 0 deletions cmd/hype/cli/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cli

import (
"os"
"sync"
)

type Env struct {
data map[string]string
mu sync.RWMutex
}

func (e *Env) Setenv(key string, value string) {
if e == nil {
return
}

e.mu.Lock()
defer e.mu.Unlock()
if e.data == nil {
e.data = map[string]string{}
}
e.data[key] = value
}

func (e *Env) Getenv(key string) string {
if e == nil || e.data == nil {
return os.Getenv(key)
}

e.mu.RLock()
defer e.mu.RUnlock()

if k, ok := e.data[key]; ok {
return k
}

return os.Getenv(key)
}
24 changes: 24 additions & 0 deletions cmd/hype/cli/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cli

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_Env_Getenv(t *testing.T) {
t.Parallel()
r := require.New(t)

e := &Env{}
r.Equal("", e.Getenv("unknown"))

v := e.Getenv("PATH")
r.NotEmpty(v)
r.NotEqual("bar", v)

e.Setenv("PATH", "bar")
v = e.Getenv("PATH")
r.NotEmpty(v)
r.Equal("bar", v)
}
208 changes: 208 additions & 0 deletions cmd/hype/cli/slides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package cli

import (
"context"
"flag"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"sync"

"github.com/gopherguides/hype"
"github.com/gopherguides/hype/slides"
"github.com/markbates/cleo"
"github.com/markbates/fsx"
"github.com/markbates/iox"
)

type Slides struct {
cleo.Cmd

// Env to be used by the app
// If nil, os.Getenv will be used.
*Env

// Web app to be used by the app
slides.App

Parser *hype.Parser

// Server to be used by the app
// If nil, a default server will be created.
Server *http.Server

// Port to listen on. Defaults to 3000.
Port int

mu sync.RWMutex
}

// snippet: main

func (a *Slides) Main(ctx context.Context, pwd string, args []string) error {
if a == nil {
return fmt.Errorf("nil app")
}

flags := a.flags()
err := flags.Parse(args)
if err != nil {
return err
}
if a.Parser != nil {
a.App.Parser = a.Parser
}
a.App.PWD = pwd
a.App.FileName = "module.md"

if p := flags.Arg(0); len(p) > 0 {
a.App.PWD = filepath.Dir(p)
a.App.FileName = filepath.Base(p)
}

a.App.PWD, err = filepath.Abs(a.App.PWD)
if err != nil {
return err
}
return WithinDir(a.App.PWD, func() error {
srv, err := a.server()
if err != nil {
return err
}

ctx, cause := context.WithCancelCause(ctx)
defer cause(nil)

srv.BaseContext = func(_ net.Listener) context.Context {
return ctx
}

go func() {
if err := srv.ListenAndServe(); err != nil {
cause(err)
}
}()

srvCtx, srvCancel := context.WithCancel(ctx)
defer srvCancel()

go func() {
<-ctx.Done()
defer srvCancel()
if err := srv.Shutdown(ctx); err != nil {
cause(err)
}
}()

<-ctx.Done()

err = context.Cause(ctx)
if err != nil && err != context.Canceled {
return err
}

<-srvCtx.Done()

return nil
})
}

// snippet: main

func (a *Slides) SetIO(oi iox.IO) {
if a == nil {
return
}

a.mu.Lock()
defer a.mu.Unlock()

a.IO = oi
}

func (a *Slides) Print(w io.Writer) error {
if a == nil {
return fmt.Errorf("nil app")
}

if w == nil {
w = os.Stdout
}

flags := a.flags()
flags.SetOutput(w)
flags.Usage()

return nil
}

func (a *Slides) Describe() string {
return "launches a web server"
}

func (a *Slides) Getenv(key string) (s string) {
if a == nil || a.Env == nil {
return os.Getenv(key)
}

return a.Env.Getenv(key)
}

func (a *Slides) flags() *flag.FlagSet {

flags := flag.NewFlagSet("server", flag.ContinueOnError)

flags.SetOutput(a.Stderr())
flags.IntVar(&a.Port, "port", 3000, "port to listen on")

return flags
}

func (a *Slides) server() (*http.Server, error) {
if a == nil {
return nil, fmt.Errorf("nil app")
}

a.mu.RLock()
srv := a.Server
port := a.Port
a.mu.RUnlock()

mux := http.NewServeMux()
mux.Handle("/", a)

cab := &fsx.ArrayFS{}
cab.Append(slides.AssetsFS)
cab.Append(a.Parser.FS)

mux.Handle("/templates/assets/", http.FileServer(http.FS(cab)))
mux.Handle("/assets/", http.FileServer(http.FS(cab)))

if srv != nil {
if srv.Handler == nil {
srv.Handler = mux
}
return srv, nil
}

if port == 0 {
p := a.Getenv("PORT")
pi, _ := strconv.Atoi(p)
if pi == 0 {
pi = 3000
}
port = pi
}

srv = &http.Server{
Addr: fmt.Sprintf(":%d", port),
}

srv.Handler = mux

return srv, nil
}
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ go 1.19
require (
github.com/gobuffalo/flect v1.0.2
github.com/markbates/clam v0.0.0-20220808175708-ef60f46826fb
github.com/markbates/cleo v0.0.0-20230420184757-5772f8c20bd0
github.com/markbates/fsx v1.2.0
github.com/markbates/cleo v0.0.0-20230821202903-72220ef5f7f0
github.com/markbates/fsx v1.2.1-0.20230825193402-1f8fe6c0541c
github.com/markbates/garlic v0.0.0-20230114180117-2dc132acb41e
github.com/markbates/plugins v0.0.0-20230420184715-249b4e34fb1b
github.com/markbates/plugins v0.0.0-20230821202759-9443baa9b3df
github.com/markbates/sweets v0.0.0-20210926032915-062eb9bcc0e5
github.com/markbates/syncx v1.5.1
github.com/markbates/table v0.0.0-20230314205021-441ed58296d1
github.com/mattn/go-shellwords v1.0.12
github.com/russross/blackfriday/v2 v2.1.0
github.com/stretchr/testify v1.8.1
golang.org/x/net v0.9.0
golang.org/x/sync v0.1.0
golang.org/x/net v0.14.0
golang.org/x/sync v0.3.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/markbates/iox v0.0.0-20221020165654-699382563dae // indirect
github.com/markbates/iox v0.0.0-20230819160303-65441f89f313 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20230420155640-133eef4313cb // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ github.com/markbates/clam v0.0.0-20220808175708-ef60f46826fb h1:9V50gZSw997pZqgL
github.com/markbates/clam v0.0.0-20220808175708-ef60f46826fb/go.mod h1:5bD7elUD4b404zK6eOn4Q0wKJYb0mxL6yDeQZSRn9Hk=
github.com/markbates/cleo v0.0.0-20230420184757-5772f8c20bd0 h1:1Q7q3yiCK+YgiLKv8dmRyZj3EY7vo/ZDio8o66dYH5E=
github.com/markbates/cleo v0.0.0-20230420184757-5772f8c20bd0/go.mod h1:lLA9DBGBx6/Mk5DiggW3NBytWB9QwMeqn7ZmBvDvSTc=
github.com/markbates/cleo v0.0.0-20230821202903-72220ef5f7f0 h1:x3wg9UbZFp4yB94Fm8MR9xKiJP0i1XeInUWFg/6/sqM=
github.com/markbates/cleo v0.0.0-20230821202903-72220ef5f7f0/go.mod h1:c3X0dnyY5ctGk3G9/h9J/oSA+NX8nt9cLKkFgrWBJ8M=
github.com/markbates/fsx v1.2.0 h1:UjhuPeCalR/DfoxQRcRe4selu1wz02NTlJE+VW6i4XI=
github.com/markbates/fsx v1.2.0/go.mod h1:Ekxcae72dKRZ/z5SLWQoul3Ox6AlCPMfNw+jw+9XSos=
github.com/markbates/fsx v1.2.1-0.20230825193402-1f8fe6c0541c h1:IX3/9dWerTpiNjFiO8sUJ+MXioPRi/ON5lbGgR0UxwU=
github.com/markbates/fsx v1.2.1-0.20230825193402-1f8fe6c0541c/go.mod h1:t7HbKJBmGY9+Wz+4kZI+67Utu3vYLcxKMjfUzz/eOA4=
github.com/markbates/garlic v0.0.0-20230114180117-2dc132acb41e h1:Iq1xGoj427D6efggc+47p4Hk6SuJo4dh4SM6N3ZRIEo=
github.com/markbates/garlic v0.0.0-20230114180117-2dc132acb41e/go.mod h1:k4nxVa4o0HPTVgFiL6IZV8JAfJP9xXqxOkbzEZWO+oQ=
github.com/markbates/iox v0.0.0-20221020165654-699382563dae h1:roYZOV/yXR7MfIDUf9mHoDYssqP1G+B+HMFL6GZM/qA=
github.com/markbates/iox v0.0.0-20221020165654-699382563dae/go.mod h1:q+P6BLqC21cbcmL82OdIO3A9VFCjq9fRQ+ije7T85ow=
github.com/markbates/iox v0.0.0-20230819160303-65441f89f313 h1:xZIGATqh50ydGM4YeYOoHNDAEnvlcERoLhgCmGqE/OA=
github.com/markbates/iox v0.0.0-20230819160303-65441f89f313/go.mod h1:q+P6BLqC21cbcmL82OdIO3A9VFCjq9fRQ+ije7T85ow=
github.com/markbates/plugins v0.0.0-20230420184715-249b4e34fb1b h1:g6WDjpbjm1/1KpsgzhaQ3sWNxHpiyVNv13UIOO6Wi3c=
github.com/markbates/plugins v0.0.0-20230420184715-249b4e34fb1b/go.mod h1:sW8lOXIwFRLa6PHo1sgWlVohCtYt1v+ZtgVPKM6fsVQ=
github.com/markbates/plugins v0.0.0-20230821202759-9443baa9b3df h1:FO5PuTGqpiILO5BUK0doGhzOF3Hyb9UZ5Ec/LjUBPPw=
github.com/markbates/plugins v0.0.0-20230821202759-9443baa9b3df/go.mod h1:sW8lOXIwFRLa6PHo1sgWlVohCtYt1v+ZtgVPKM6fsVQ=
github.com/markbates/sweets v0.0.0-20210926032915-062eb9bcc0e5 h1:od8GvggTptD2HUz8nGaQQe6n1zAHDbsG+82hfHPNExY=
github.com/markbates/sweets v0.0.0-20210926032915-062eb9bcc0e5/go.mod h1:NMNrJiINR9aeBgn5wfzZMJOnwWnDaj4hDOGpGoVV3XE=
github.com/markbates/syncx v1.5.0 h1:4uMUWiwaSyAyCMJVMxXeI345wJuSb7L04w8xZ0f6gq0=
Expand All @@ -41,10 +49,16 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/exp v0.0.0-20230420155640-133eef4313cb h1:rhjz/8Mbfa8xROFiH+MQphmAmgqRM0bOMnytznhWEXk=
golang.org/x/exp v0.0.0-20230420155640-133eef4313cb/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
1 change: 1 addition & 0 deletions hype.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func DefaultElements() map[Atom]ParseElementFn {
atomx.Thead: NewTHeadNodes,
atomx.Tr: NewTRNodes,
atomx.Ul: NewULNodes,
atomx.Var: NewVarNodes,
}

for _, h := range atomx.Headings() {
Expand Down
Loading

0 comments on commit dab8bfe

Please sign in to comment.