From e190cdfb34f076f50190fecdbe783226f69804c0 Mon Sep 17 00:00:00 2001 From: Egor Kovetskiy Date: Tue, 2 Feb 2021 17:12:08 +0300 Subject: [PATCH] rollback #53 because of #63, add tests --- .gitignore | 1 + go.mod | 5 ++++ go.sum | 20 +++++++++++++ pkg/mark/markdown.go | 2 +- pkg/mark/markdown_test.go | 50 +++++++++++++++++++++++++++++++ pkg/mark/testdata/codes.html | 52 +++++++++++++++++++++++++++++++++ pkg/mark/testdata/codes.md | 17 +++++++++++ pkg/mark/testdata/header.html | 13 +++++++++ pkg/mark/testdata/header.md | 9 ++++++ pkg/mark/testdata/lists.html | 24 +++++++++++++++ pkg/mark/testdata/lists.md | 13 +++++++++ pkg/mark/testdata/newlines.html | 16 ++++++++++ pkg/mark/testdata/newlines.md | 17 +++++++++++ pkg/mark/testdata/tags.html | 5 ++++ pkg/mark/testdata/tags.md | 5 ++++ test/alice.md | 14 +++++++++ test/bob.md | 5 ++++ 17 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 pkg/mark/markdown_test.go create mode 100644 pkg/mark/testdata/codes.html create mode 100644 pkg/mark/testdata/codes.md create mode 100644 pkg/mark/testdata/header.html create mode 100644 pkg/mark/testdata/header.md create mode 100644 pkg/mark/testdata/lists.html create mode 100644 pkg/mark/testdata/lists.md create mode 100644 pkg/mark/testdata/newlines.html create mode 100644 pkg/mark/testdata/newlines.md create mode 100644 pkg/mark/testdata/tags.html create mode 100644 pkg/mark/testdata/tags.md create mode 100644 test/alice.md create mode 100644 test/bob.md diff --git a/.gitignore b/.gitignore index 04d6b3b4..bc9f4600 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /docker /testdata .idea/ +/mark.test diff --git a/go.mod b/go.mod index 0021bd73..ab9b0dde 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,9 @@ go 1.14 require ( github.com/BurntSushi/toml v0.3.1 // indirect + github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 + github.com/alecthomas/colour v0.1.0 // indirect + github.com/alecthomas/repr v0.0.0-20201120212035-bb82daffcca2 // indirect github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 github.com/go-yaml/yaml v2.1.0+incompatible // indirect github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 // indirect @@ -11,10 +14,12 @@ require ( github.com/kovetskiy/ko v0.0.0-20190324102900-26b8dd0988bf github.com/kovetskiy/lorg v0.0.0-20200107130803-9a7136a95634 github.com/kovetskiy/toml v0.2.0 // indirect + github.com/mattn/go-isatty v0.0.12 // indirect github.com/reconquest/karma-go v0.0.0-20200326104714-79480464fdb5 github.com/reconquest/pkg v0.0.0-20201028091908-8e9a5e0226ef github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4 github.com/russross/blackfriday v1.5.2 + github.com/sergi/go-diff v1.1.0 // indirect github.com/stretchr/testify v1.5.1 gopkg.in/yaml.v2 v2.2.8 ) diff --git a/go.sum b/go.sum index 82167a87..e2567de1 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,16 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c= +github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U= +github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI= +github.com/alecthomas/colour v0.1.0 h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk= +github.com/alecthomas/colour v0.1.0/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0= +github.com/alecthomas/repr v0.0.0-20201120212035-bb82daffcca2 h1:G5TeG64Ox4OWq2YwlsxS7nOedU8vbGgNRTRDAjGvDCk= +github.com/alecthomas/repr v0.0.0-20201120212035-bb82daffcca2/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM= @@ -19,6 +27,11 @@ github.com/kovetskiy/lorg v0.0.0-20200107130803-9a7136a95634 h1:szpgh20EtHoQhJ38 github.com/kovetskiy/lorg v0.0.0-20200107130803-9a7136a95634/go.mod h1:B8HeKAukXULNzWWsW5k/SQyDkiQZPn7lTBJDB46MZ9I= github.com/kovetskiy/toml v0.2.0 h1:tMsPGWE3ejTjXop10/17b/tDtbwQJZdBfc0e+l3WndA= github.com/kovetskiy/toml v0.2.0/go.mod h1:+nh++V8wCesSlfPA3DSXGO1hiAHDVHDqem4ixTsWuRY= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/reconquest/cog v0.0.0-20191208202052-266c2467b936 h1:jSaVCkKLAGc8VWBRVKk0Ffxrv/NKD1ixkOyjwPWrPd4= @@ -35,8 +48,11 @@ github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4 h1:bcDXaT github.com/reconquest/regexputil-go v0.0.0-20160905154124-38573e70c1f4/go.mod h1:OI1di2iiFSwX3D70iZjzdmCPPfssjOl+HX40tI3VaXA= github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/xtgo/uuid v0.0.0-20140804021211-a0b114877d4c/go.mod h1:UrdRz5enIKZ63MEE3IF9l2/ebyx59GyGgPi+tICQdmM= @@ -49,9 +65,13 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/pkg/mark/markdown.go b/pkg/mark/markdown.go index a41a8586..dc684260 100644 --- a/pkg/mark/markdown.go +++ b/pkg/mark/markdown.go @@ -117,7 +117,7 @@ func CompileMarkdown( blackfriday.EXTENSION_TITLEBLOCK | blackfriday.EXTENSION_BACKSLASH_LINE_BREAK | blackfriday.EXTENSION_DEFINITION_LISTS | - blackfriday.EXTENSION_JOIN_LINES | + blackfriday.EXTENSION_HARD_LINE_BREAK | blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK, }, ) diff --git a/pkg/mark/markdown_test.go b/pkg/mark/markdown_test.go new file mode 100644 index 00000000..d831b88f --- /dev/null +++ b/pkg/mark/markdown_test.go @@ -0,0 +1,50 @@ +package mark + +import ( + "io/ioutil" + "path/filepath" + "strings" + "testing" + + "github.com/kovetskiy/mark/pkg/mark/stdlib" + "github.com/stretchr/testify/assert" +) + +const ( + NL = "\n" +) + +func text(lines ...string) string { + return strings.Join(lines, "\n") +} + +func TestCompileMarkdown(t *testing.T) { + test := assert.New(t) + + testcases, err := filepath.Glob("testdata/*.md") + if err != nil { + panic(err) + } + + for _, filename := range testcases { + basename := filepath.Base(filename) + testname := strings.TrimSuffix(basename, ".md") + htmlname := filepath.Join(filepath.Dir(filename), testname+".html") + + markdown, err := ioutil.ReadFile(filename) + if err != nil { + panic(err) + } + html, err := ioutil.ReadFile(htmlname) + if err != nil { + panic(err) + } + + lib, err := stdlib.New(nil) + if err != nil { + panic(err) + } + actual := CompileMarkdown(markdown, lib) + test.EqualValues(string(html), actual, filename+" vs "+htmlname) + } +} diff --git a/pkg/mark/testdata/codes.html b/pkg/mark/testdata/codes.html new file mode 100644 index 00000000..cf9db533 --- /dev/null +++ b/pkg/mark/testdata/codes.html @@ -0,0 +1,52 @@ +

inline

+ + + + + +false + + + + + + + + + +bash +false + + + + + + + + + +unknown +false + + + + + + +

text
+text 2

+ + + + +unknown +false + + + + + diff --git a/pkg/mark/testdata/codes.md b/pkg/mark/testdata/codes.md new file mode 100644 index 00000000..afbb7272 --- /dev/null +++ b/pkg/mark/testdata/codes.md @@ -0,0 +1,17 @@ +`inline` + +``` +some code +``` +```bash +code bash +``` + +```unknown +unknown code +``` +text +text 2 +```unknown +unknown code 2 +``` diff --git a/pkg/mark/testdata/header.html b/pkg/mark/testdata/header.html new file mode 100644 index 00000000..89d0a104 --- /dev/null +++ b/pkg/mark/testdata/header.html @@ -0,0 +1,13 @@ +

a

+ +

b

+ +

c

+ +

d

+ +
e
+ +

f

+ +

g

diff --git a/pkg/mark/testdata/header.md b/pkg/mark/testdata/header.md new file mode 100644 index 00000000..9c1d2f05 --- /dev/null +++ b/pkg/mark/testdata/header.md @@ -0,0 +1,9 @@ +# a +## b +### c +#### d +##### e +f += +g +- diff --git a/pkg/mark/testdata/lists.html b/pkg/mark/testdata/lists.html new file mode 100644 index 00000000..5a5b9097 --- /dev/null +++ b/pkg/mark/testdata/lists.html @@ -0,0 +1,24 @@ + + +

text

+ + diff --git a/pkg/mark/testdata/lists.md b/pkg/mark/testdata/lists.md new file mode 100644 index 00000000..0e67e892 --- /dev/null +++ b/pkg/mark/testdata/lists.md @@ -0,0 +1,13 @@ +- dash 1-1 +- dash 1-2 +- dash 1-3 + - dash 1-3-1 + - dash 1-3-2 + - dash 1-3-3 + - dash 1-3-3-1 + +text + +* a +* b +* c diff --git a/pkg/mark/testdata/newlines.html b/pkg/mark/testdata/newlines.html new file mode 100644 index 00000000..0e020cb6 --- /dev/null +++ b/pkg/mark/testdata/newlines.html @@ -0,0 +1,16 @@ +

one-1
+one-2

+ +

two-1

+ +

two-2

+ +

three-1

+ +

three-2

+ +

space-1
+space-2

+ +

2space-1
+2space-2

diff --git a/pkg/mark/testdata/newlines.md b/pkg/mark/testdata/newlines.md new file mode 100644 index 00000000..a56112ef --- /dev/null +++ b/pkg/mark/testdata/newlines.md @@ -0,0 +1,17 @@ +one-1 +one-2 + +two-1 + +two-2 + +three-1 + + +three-2 + +space-1 +space-2 + +2space-1 +2space-2 diff --git a/pkg/mark/testdata/tags.html b/pkg/mark/testdata/tags.html new file mode 100644 index 00000000..729cd5a6 --- /dev/null +++ b/pkg/mark/testdata/tags.html @@ -0,0 +1,5 @@ +

bold
+bold

+ +

vitalik
+vitalik

diff --git a/pkg/mark/testdata/tags.md b/pkg/mark/testdata/tags.md new file mode 100644 index 00000000..3dc28c9d --- /dev/null +++ b/pkg/mark/testdata/tags.md @@ -0,0 +1,5 @@ +bold +**bold** + +vitalik +*vitalik* diff --git a/test/alice.md b/test/alice.md new file mode 100644 index 00000000..5cdb428e --- /dev/null +++ b/test/alice.md @@ -0,0 +1,14 @@ + + + + + + + + + +a +b + +- a +- b diff --git a/test/bob.md b/test/bob.md new file mode 100644 index 00000000..21b9e188 --- /dev/null +++ b/test/bob.md @@ -0,0 +1,5 @@ + + + + +Alice: [alice](alice.md)