pkg/md: Fix newlines around HTML blocks in FmtCodec.

Also:

- Show a trace of Op's when a test case fails.

- Don't loosify lists when comparing the HTML output of original and formatted.
This commit is contained in:
Qi Xiao 2022-10-28 19:20:39 +01:00
parent a0be886c87
commit a08ab691df
5 changed files with 68 additions and 27 deletions

View File

@ -95,6 +95,9 @@ func (c *FmtCodec) Do(op Op) {
c.write(delim)
c.write("\n")
c.code = false
case OpHTMLBlockStart:
c.ensureNewStanza()
case OpHTMLBlockEnd:
case OpParagraphStart:
c.ensureNewStanza()
case OpParagraphEnd:

View File

@ -20,8 +20,6 @@ func TestFmtPreservesHTMLRender(t *testing.T) {
switch tc.Example {
case 39, 40:
t.Skip("TODO escape sequence")
case 167, 280, 281, 282, 283, 284, 315:
t.Skip("TODO newline")
case 301, 302:
t.Skip("TODO change of list markers")
case 460, 462:
@ -37,10 +35,12 @@ func TestFmtPreservesHTMLRender(t *testing.T) {
func testFmtPreservesHTMLRender(t *testing.T, original string) {
formatted := render(original, &md.FmtCodec{})
formattedRender := render(formatted, &htmlCodec{})
originalRender := loosifyLists(render(original, &htmlCodec{}))
originalRender := render(original, &htmlCodec{})
if formattedRender != originalRender {
t.Errorf("original:\n%s\nformatted:\n%s\nHTML diff (-original +formatted):\n%s",
t.Errorf("original:\n%s\nformatted:\n%s\n"+
"HTML diff (-original +formatted):\n%sops diff (-original +formatted):\n%s",
hr+"\n"+original+hr, hr+"\n"+formatted+hr,
cmp.Diff(originalRender, formattedRender))
cmp.Diff(originalRender, formattedRender),
cmp.Diff(render(original, &md.OpTraceCodec{}), render(formatted, &md.OpTraceCodec{})))
}
}

View File

@ -82,6 +82,8 @@ const (
OpHeadingEnd
OpCodeBlockStart
OpCodeBlockEnd
OpHTMLBlockStart
OpHTMLBlockEnd
OpParagraphStart
OpParagraphEnd
@ -325,6 +327,9 @@ func (p *blockParser) parseIndentedCodeBlock(line string) {
}
func (p *blockParser) parseHTMLBlock(line string, closer func(string) bool) {
do(p.codec, OpHTMLBlockStart)
defer do(p.codec, OpHTMLBlockEnd)
doRawHTMLLine(p.codec, line)
if closer(line) {
return
@ -349,6 +354,9 @@ func (p *blockParser) parseHTMLBlock(line string, closer func(string) bool) {
}
func (p *blockParser) parseBlankLineTerminatedHTMLBlock(line string) {
do(p.codec, OpHTMLBlockStart)
defer do(p.codec, OpHTMLBlockEnd)
doRawHTMLLine(p.codec, line)
for p.lines.more() {
line := p.lines.next()

28
pkg/md/optrace_test.go Normal file
View File

@ -0,0 +1,28 @@
package md
import (
"fmt"
"strings"
)
// OpTraceCodec is a Codec that records all the Op's passed to its Do method.
type OpTraceCodec struct{ strings.Builder }
func (c *OpTraceCodec) Do(op Op) {
if c.Len() > 0 {
c.WriteByte('\n')
}
c.WriteString(op.Type.String())
if op.Number != 0 {
fmt.Fprintf(c, " Number=%d", op.Number)
}
if op.Text != "" {
fmt.Fprintf(c, " Text=%q", op.Text)
}
if op.Dest != "" {
fmt.Fprintf(c, " Dest=%q", op.Dest)
}
if op.Alt != "" {
fmt.Fprintf(c, " Alt=%q", op.Alt)
}
}

View File

@ -15,31 +15,33 @@ func _() {
_ = x[OpHeadingEnd-4]
_ = x[OpCodeBlockStart-5]
_ = x[OpCodeBlockEnd-6]
_ = x[OpParagraphStart-7]
_ = x[OpParagraphEnd-8]
_ = x[OpBlockquoteStart-9]
_ = x[OpBlockquoteEnd-10]
_ = x[OpListItemStart-11]
_ = x[OpListItemEnd-12]
_ = x[OpBulletListStart-13]
_ = x[OpBulletListEnd-14]
_ = x[OpOrderedListStart-15]
_ = x[OpOrderedListEnd-16]
_ = x[OpCodeSpanStart-17]
_ = x[OpCodeSpanEnd-18]
_ = x[OpEmphasisStart-19]
_ = x[OpEmphasisEnd-20]
_ = x[OpStrongEmphasisStart-21]
_ = x[OpStrongEmphasisEnd-22]
_ = x[OpLinkStart-23]
_ = x[OpLinkEnd-24]
_ = x[OpImage-25]
_ = x[OpHardLineBreak-26]
_ = x[OpHTMLBlockStart-7]
_ = x[OpHTMLBlockEnd-8]
_ = x[OpParagraphStart-9]
_ = x[OpParagraphEnd-10]
_ = x[OpBlockquoteStart-11]
_ = x[OpBlockquoteEnd-12]
_ = x[OpListItemStart-13]
_ = x[OpListItemEnd-14]
_ = x[OpBulletListStart-15]
_ = x[OpBulletListEnd-16]
_ = x[OpOrderedListStart-17]
_ = x[OpOrderedListEnd-18]
_ = x[OpCodeSpanStart-19]
_ = x[OpCodeSpanEnd-20]
_ = x[OpEmphasisStart-21]
_ = x[OpEmphasisEnd-22]
_ = x[OpStrongEmphasisStart-23]
_ = x[OpStrongEmphasisEnd-24]
_ = x[OpLinkStart-25]
_ = x[OpLinkEnd-26]
_ = x[OpImage-27]
_ = x[OpHardLineBreak-28]
}
const _OpType_name = "OpTextOpRawHTMLOpThematicBreakOpHeadingStartOpHeadingEndOpCodeBlockStartOpCodeBlockEndOpParagraphStartOpParagraphEndOpBlockquoteStartOpBlockquoteEndOpListItemStartOpListItemEndOpBulletListStartOpBulletListEndOpOrderedListStartOpOrderedListEndOpCodeSpanStartOpCodeSpanEndOpEmphasisStartOpEmphasisEndOpStrongEmphasisStartOpStrongEmphasisEndOpLinkStartOpLinkEndOpImageOpHardLineBreak"
const _OpType_name = "OpTextOpRawHTMLOpThematicBreakOpHeadingStartOpHeadingEndOpCodeBlockStartOpCodeBlockEndOpHTMLBlockStartOpHTMLBlockEndOpParagraphStartOpParagraphEndOpBlockquoteStartOpBlockquoteEndOpListItemStartOpListItemEndOpBulletListStartOpBulletListEndOpOrderedListStartOpOrderedListEndOpCodeSpanStartOpCodeSpanEndOpEmphasisStartOpEmphasisEndOpStrongEmphasisStartOpStrongEmphasisEndOpLinkStartOpLinkEndOpImageOpHardLineBreak"
var _OpType_index = [...]uint16{0, 6, 15, 30, 44, 56, 72, 86, 102, 116, 133, 148, 163, 176, 193, 208, 226, 242, 257, 270, 285, 298, 319, 338, 349, 358, 365, 380}
var _OpType_index = [...]uint16{0, 6, 15, 30, 44, 56, 72, 86, 102, 116, 132, 146, 163, 178, 193, 206, 223, 238, 256, 272, 287, 300, 315, 328, 349, 368, 379, 388, 395, 410}
func (i OpType) String() string {
if i >= OpType(len(_OpType_index)-1) {