diff --git a/go.mod b/go.mod index 27feb04..07ee372 100644 --- a/go.mod +++ b/go.mod @@ -3,16 +3,16 @@ module github.com/rwxrob/bonzai go 1.18 require ( - github.com/rwxrob/compcmd v0.2.3 + github.com/rwxrob/compcmd v0.3.0 github.com/rwxrob/fn v0.3.3 - github.com/rwxrob/scan v0.6.1 + github.com/rwxrob/scan v0.9.0 github.com/rwxrob/structs v0.6.0 - github.com/rwxrob/term v0.2.7 - github.com/rwxrob/to v0.5.5 + github.com/rwxrob/term v0.2.8 + github.com/rwxrob/to v0.7.0 ) require ( - golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect - golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect - golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect + golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect + golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect + golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect ) diff --git a/go.sum b/go.sum index a15aba8..65aa11c 100644 --- a/go.sum +++ b/go.sum @@ -2,10 +2,14 @@ github.com/rwxrob/compcmd v0.1.1 h1:VCpA28yXWSS5ijAe6N5DswYektT1KT3ZryirINZVYgs= github.com/rwxrob/compcmd v0.1.1/go.mod h1:l4anbcmNEBs4vNb2uxfD45jSgm2FmPvpYfBNGqlO3rc= github.com/rwxrob/compcmd v0.2.3 h1:L05+CnLoJWpRp745+Hpz8fB0dz8SIzH0+uwSZxIZgEM= github.com/rwxrob/compcmd v0.2.3/go.mod h1:fS9smeE6XIb/2511RU6618IimOJjg0tC69eXISxaxU4= +github.com/rwxrob/compcmd v0.3.0 h1:AlJNItb7+Yk17qmH5E7TJFyBXhna/rS3NeQAgjqbFls= +github.com/rwxrob/compcmd v0.3.0/go.mod h1:XOHl6bS2Uen6Wx2mxtbtUhT8Sbz1IhnaE55xPkhTBD4= github.com/rwxrob/fn v0.3.3 h1:ymRQGWDhrrvoHKXLJ4WZlgI2qrC7gMOotowQMGvwmVQ= github.com/rwxrob/fn v0.3.3/go.mod h1:omPqOqEB+dDna09z5pi5YFxq4IZqDvv3wFPUCES5LvY= github.com/rwxrob/scan v0.6.1 h1:KMGi/kV7TY4pcVo8H/HVwwyQZ2gBFNQI3g7tT121Voo= github.com/rwxrob/scan v0.6.1/go.mod h1:eF28AgH3YUvP7E9UrhjePRUyLq3Gh7+fR28P7Z/Ku4E= +github.com/rwxrob/scan v0.9.0 h1:0FhHGrjmwRvRJWCW6I2BlOgEZKa4Af/R6t/2or8abAE= +github.com/rwxrob/scan v0.9.0/go.mod h1:sDIllL5+Ppk33AHkhhdrkPLP0EheWQPoNotKMhUfCIg= github.com/rwxrob/structs v0.5.1 h1:epff+4ihej+EgsQx0qOAS/cn7ix3qSLor0M/AjWiV3M= github.com/rwxrob/structs v0.5.1/go.mod h1:e888JzBSTrMMizNYFnk426F77QneVUKgdxM5inMHErY= github.com/rwxrob/structs v0.6.0 h1:t8JVd/Pee1OGaXgT6QYmGed470C9vOw6scdH8Cr5LPg= @@ -14,17 +18,27 @@ github.com/rwxrob/term v0.2.6 h1:C8BqqHaEh8MGYp1cVrPRlDAYPEK3HfvhGjf7l5AvnV8= github.com/rwxrob/term v0.2.6/go.mod h1:II0qQ7aHUdPniZCAPWOdYwugcZqdmRmEWIJQN7Z8NA0= github.com/rwxrob/term v0.2.7 h1:uFhFcZkjphRQHhkzaV5G34r4es7R6qEw52XA3xD0yn0= github.com/rwxrob/term v0.2.7/go.mod h1:II0qQ7aHUdPniZCAPWOdYwugcZqdmRmEWIJQN7Z8NA0= +github.com/rwxrob/term v0.2.8 h1:N9spr+owF8mRizSqPiATu5F0v2VJUIKwRldsibAUys8= +github.com/rwxrob/term v0.2.8/go.mod h1:UruWGJ5mrQH5v1XBg52s7ldZUBXxEODevbkoFUM4SrQ= github.com/rwxrob/to v0.5.2 h1:BK2xKkWNhnR5TwEzbsPpXjGODFrTrgZfXzfEdtgCeyU= github.com/rwxrob/to v0.5.2/go.mod h1:lojk6scni4ZRYjnKJO/f2DVRTW0BB6l9LZQ/NvZZt4Y= github.com/rwxrob/to v0.5.4 h1:eUKF6O4eLhiA/pdcl/BXfp492PZ6Lz1Vs1gQZnSZlj4= github.com/rwxrob/to v0.5.4/go.mod h1:lojk6scni4ZRYjnKJO/f2DVRTW0BB6l9LZQ/NvZZt4Y= github.com/rwxrob/to v0.5.5 h1:gtcsVTF/0XAfsWLTQ3lC0byKc9XWE96Gz5icus3zk3I= github.com/rwxrob/to v0.5.5/go.mod h1:PzU60XDkBkkiQxuiUsqwwAkjja2B5O0nKiRV9nHO84Q= +github.com/rwxrob/to v0.7.0 h1:hTs6pH6J4/JAex/T+ktsz3to8ojuuS9tCW4j4fHBTvY= +github.com/rwxrob/to v0.7.0/go.mod h1:5z3Y1tyXRmPfa+be5hvnxYuLQ0NSBQsE6mIBVTqlYTA= golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA= golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c= +golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/sys v0.0.0-20220412071739-889880a91fd5 h1:NubxfvTRuNb4RVzWrIDAUzUvREH1HkCD4JjyQTSG9As= golang.org/x/sys v0.0.0-20220412071739-889880a91fd5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc= golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 h1:v1W7bwXHsnLLloWYTVEdvGvA7BHMeBYsPcF0GLDxIRs= +golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 h1:EH1Deb8WZJ0xc0WK//leUHXcX9aLE5SymusoTmMZye8= golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc= +golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/go.work.off b/go.work.off new file mode 100644 index 0000000..b5616c1 --- /dev/null +++ b/go.work.off @@ -0,0 +1,6 @@ +go 1.18 + +use ( + . + ../to +) diff --git a/z/mark.go b/z/mark.go index 80f5c21..8a47c56 100644 --- a/z/mark.go +++ b/z/mark.go @@ -58,6 +58,9 @@ func (s *Block) String() string { return string(s.V) } // For now, these blocks are added as is, but plans are to eventually // add support for short and long lists much like CommonMark. // +// If no blocks are parsed returns an empty slice of Block pointers ([] +// *Block). +// // Note that because of the nature of Verbatim's block's initial (4 // space) token Verbatim blocks must never be first since the entire // input buffer is first dedented and the spaces would grouped with the @@ -65,100 +68,96 @@ func (s *Block) String() string { return string(s.V) } // because Verbatim blocks never make sense as the first block in // a BonzaiMark document. This simplicity and clarity of 4-space tokens // far outweighs the advantages of alternatives (such as fences). +// +// PEGN Spefication +// +// Grammar <-- Block* +// Block <-- Bulleted / Numbered / Verbatim / Paragraph +// Bulleted <-- '* ' (!EOB unipoint)* EOB +// Numbered <-- '1. ' (!EOB unipoint)* EOB +// Verbatim <-- ' ' (!EOB unipoint)* EOB +// Paragraph <-- (!EOB unipoint)* EOB +// EOB <- LF{2} / EOD +// EOD <- # end of data stream +// func Blocks(in string) []*Block { - var blocks []*Block - s := scan.R{B: []byte(to.Dedented(in))} + in = to.Dedented(in) // also trims initial white space + + s := scan.R{B: []byte(in)} //s.Trace++ for s.Scan() { - // bulleted list - if s.Peek("* ") { + // Bulleted + if s.Is("* ") { var beg, end int beg = s.P - 1 - for s.Scan() { - if s.Peek("\n\n") { + if s.Is("\n\n") { end = s.P - 1 s.P++ break } + end = s.P } - blocks = append(blocks, &Block{Bulleted, s.B[beg:end]}) continue } - // numbered list - if s.Peek("1. ") { + // Numbered + if s.Is("1. ") { var beg, end int beg = s.P - 1 - for s.Scan() { - if s.Peek("\n\n") { + if s.Is("\n\n") { end = s.P - 1 s.P++ break } + end = s.P } - blocks = append(blocks, &Block{Numbered, s.B[beg:end]}) continue } - // verbatim + // Verbatim if ln := s.Match(begVerbatim); ln >= 4 { - s.P-- - var beg, end int - beg = s.P - + beg = s.LP for s.Scan() { - - if s.Peek("\n\n") { + if s.Is("\n\n") { + end = s.P - 1 s.P++ - end = s.P - 2 break } - + end = s.P } - dedented := to.Dedented(string(s.B[beg:end])) blocks = append(blocks, &Block{Verbatim, []byte(dedented)}) continue } - // paragraph (default) + // Paragraph (default) if !unicode.IsSpace(s.R) { - - buf := []byte(string(s.R)) - + var beg, end int + beg = s.LP for s.Scan() { - - if s.Peek("\n\n") { + if s.Is("\n\n") { + end = s.P - 1 s.P++ break } - - if ln := s.Match(ws); ln > 0 { - buf = append(buf, ' ') - s.P += ln - 1 - continue - } - - buf = append(buf, []byte(string(s.R))...) - - } - - if len(buf) > 0 { - blocks = append(blocks, &Block{Paragraph, buf}) + end = s.P } + blocks = append(blocks, &Block{Paragraph, + []byte(to.Words(string(s.B[beg:end])))}) continue } } + return blocks } @@ -227,6 +226,7 @@ func Emph[T string | []byte | []rune](buf T) string { // **Bold** if s.Match(begBold) > 0 { + s.P += 1 nbuf = append(nbuf, []rune(term.Bold)...) for s.Scan() { diff --git a/z/mark_test.go b/z/mark_test.go index 96391f9..3620933 100644 --- a/z/mark_test.go +++ b/z/mark_test.go @@ -39,6 +39,7 @@ func ExampleBlocks_bulleted() { ` blocks := Z.Blocks(in) + //fmt.Println(blocks) fmt.Printf("%q\n", blocks[0]) fmt.Printf("%q\n", blocks[1]) @@ -86,6 +87,8 @@ func ExampleBlocks_paragraph() { ` blocks := Z.Blocks(in) + //fmt.Println(len(blocks)) + //fmt.Printf("%v", blocks) fmt.Printf("%q\n", blocks[0]) fmt.Printf("%q\n", blocks[1]) @@ -104,7 +107,7 @@ func ExampleBlocks_verbatim() { // verbatim prefix. (If using Vi/m try set :list to display them.) in := ` - Must have another block type first. + * Must have another block type first. Now we can start a Verbatim @@ -112,19 +115,21 @@ func ExampleBlocks_verbatim() { Which can have blank lines, even. - And back to a paragraph block. + * And back to another bulleted list. ` blocks := Z.Blocks(in) + //fmt.Println(len(blocks)) + //fmt.Println(blocks) fmt.Printf("%q\n", blocks[0]) fmt.Printf("%q\n", blocks[1]) fmt.Printf("%q\n", blocks[2]) //Output: - // "Must have another block type first." + // "* Must have another block type first." // "Now we can start\na Verbatim\nblock.\n\nWhich can have blank lines, even." - // "And back to a paragraph block." + // "* And back to another bulleted list." }