Skip to content

Commit

Permalink
Merge pull request #17 from gopherguides/doc.ids
Browse files Browse the repository at this point in the history
assign documents ids
  • Loading branch information
markbates authored Feb 9, 2024
2 parents 32a0c39 + 7fccdeb commit 284e9c9
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 24 deletions.
4 changes: 4 additions & 0 deletions cmd/hype/cli/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func Test_Encode_JSON(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
r := require.New(t)

p.DocIDGen = func() (string, error) {
return tc.name, nil
}

cmd := &Encode{
Parser: p,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "execute file",
"nodes": [
{
"file": "module.md",
Expand Down
1 change: 1 addition & 0 deletions cmd/hype/cli/testdata/encode/json/success/parse-file.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "parse file",
"nodes": [
{
"file": "module.md",
Expand Down
3 changes: 3 additions & 0 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Document struct {
fs.FS `json:"-"`
sync.RWMutex `json:"-"`

ID string `json:"id,omitempty"`
Nodes Nodes `json:"nodes,omitempty"`
Parser *Parser `json:"parser,omitempty"` // Parser used to create the document
Root string `json:"root,omitempty"`
Expand All @@ -31,6 +32,7 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
}

x := struct {
ID string `json:"id,omitempty"`
Nodes Nodes `json:"nodes,omitempty"`
Parser *Parser `json:"parser,omitempty"` // Parser used to create the document
Root string `json:"root,omitempty"`
Expand All @@ -46,6 +48,7 @@ func (doc *Document) MarshalJSON() ([]byte, error) {
Snippets: doc.Snippets,
Title: doc.Title,
Nodes: doc.Nodes,
ID: doc.ID,
}

return json.Marshal(x)
Expand Down
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ go 1.21

require (
github.com/gobuffalo/flect v1.0.2
github.com/gofrs/uuid/v5 v5.0.0
github.com/markbates/clam v0.0.0-20220808175708-ef60f46826fb
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/iox v0.0.0-20230819160303-65441f89f313
github.com/markbates/fsx v1.3.0
github.com/markbates/garlic v1.0.0
github.com/markbates/iox v0.0.0-20230829013604-e0813da73cc6
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.14.0
golang.org/x/sync v0.3.0
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.21.0
golang.org/x/sync v0.6.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
29 changes: 16 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gobuffalo/flect v1.0.2 h1:eqjPGSo2WmjgY2XlpGwo2NXgL3RucAKo4k4qQMNA5sA=
github.com/gobuffalo/flect v1.0.2/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs=
github.com/gofrs/uuid/v5 v5.0.0 h1:p544++a97kEL+svbcFbCQVM9KFu0Yo25UoISXGNNH9M=
github.com/gofrs/uuid/v5 v5.0.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/markbates/clam v0.0.0-20220808175708-ef60f46826fb h1:9V50gZSw997pZqgLCjhyApFsS8JA1sIZmD+3+MLJcl4=
github.com/markbates/clam v0.0.0-20220808175708-ef60f46826fb/go.mod h1:5bD7elUD4b404zK6eOn4Q0wKJYb0mxL6yDeQZSRn9Hk=
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.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-20230819160303-65441f89f313 h1:xZIGATqh50ydGM4YeYOoHNDAEnvlcERoLhgCmGqE/OA=
github.com/markbates/iox v0.0.0-20230819160303-65441f89f313/go.mod h1:q+P6BLqC21cbcmL82OdIO3A9VFCjq9fRQ+ije7T85ow=
github.com/markbates/fsx v1.3.0 h1:eunGc5BKa7ELGDnsN1Fe8LBH1/ZSeDmAbqIx2DAQRtM=
github.com/markbates/fsx v1.3.0/go.mod h1:t7HbKJBmGY9+Wz+4kZI+67Utu3vYLcxKMjfUzz/eOA4=
github.com/markbates/garlic v1.0.0 h1:IOsPWAMbH2p/jnh2OYXTous891NSAkXQdosToJvlg+E=
github.com/markbates/garlic v1.0.0/go.mod h1:k4nxVa4o0HPTVgFiL6IZV8JAfJP9xXqxOkbzEZWO+oQ=
github.com/markbates/iox v0.0.0-20230829013604-e0813da73cc6 h1:z/d6wuXj40wjOVq9dnpYJVNlO7PFuBK0vz0eyzJ70lk=
github.com/markbates/iox v0.0.0-20230829013604-e0813da73cc6/go.mod h1:q+P6BLqC21cbcmL82OdIO3A9VFCjq9fRQ+ije7T85ow=
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=
Expand All @@ -36,14 +38,15 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
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.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
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=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 h1:/RIbNt/Zr7rVhIkQhooTxCxFcdWLGIKnZA4IXNFSrvo=
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
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/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
39 changes: 35 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"sync"
"time"

"github.com/gofrs/uuid/v5"
"github.com/gopherguides/hype/binding"
"github.com/markbates/syncx"
"golang.org/x/net/html"
Expand All @@ -30,6 +31,7 @@ type Parser struct {
Snippets Snippets `json:"snippets,omitempty"`
Section int `json:"section,omitempty"`
NowFn func() time.Time `json:"-"` // default: time.Now()
DocIDGen func() (string, error) `json:"-"` // default: uuid.NewV4().String()
Vars syncx.Map[string, any] `json:"vars,omitempty"`

fileName string
Expand Down Expand Up @@ -122,7 +124,11 @@ func (p *Parser) Parse(r io.Reader) (*Document, error) {
return nil, p.wrapErr(err)
}

doc := p.newDoc()
doc, err := p.newDoc()
if err != nil {
return nil, p.wrapErr(err)
}

doc.Nodes = Nodes{node}
if len(doc.Title) == 0 {
doc.Title = FindTitle(doc.Nodes)
Expand Down Expand Up @@ -259,7 +265,10 @@ func (p *Parser) ParseExecuteFragment(ctx context.Context, r io.Reader) (Nodes,
return nil, p.wrapErr(err)
}

doc := p.newDoc()
doc, err := p.newDoc()
if err != nil {
return nil, p.wrapErr(err)
}
doc.Nodes = nodes

err = doc.Execute(ctx)
Expand Down Expand Up @@ -415,16 +424,38 @@ func NewParser(cab fs.FS) *Parser {
}
}

func (p *Parser) newDoc() *Document {
func (p *Parser) newDoc() (*Document, error) {
if p == nil {
return nil, ErrIsNil("parser")
}

p.mu.Lock()
defer p.mu.Unlock()

if p.DocIDGen == nil {
p.DocIDGen = func() (string, error) {
id, err := uuid.NewV4()
if err != nil {
return "", err
}
return id.String(), nil
}
}

id, err := p.DocIDGen()
if err != nil {
return nil, p.wrapErr(err)
}
doc := &Document{
ID: id,
FS: p.FS,
Parser: p,
Root: p.Root,
SectionID: p.Section,
Snippets: p.Snippets,
}

return doc
return doc, nil
}

func (p *Parser) wrapErr(err error) error {
Expand Down

0 comments on commit 284e9c9

Please sign in to comment.