From a08ab691dff6d846e65b25e10ad7838c1bf720b0 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Fri, 28 Oct 2022 19:20:39 +0100 Subject: [PATCH] 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. --- pkg/md/fmt.go | 3 +++ pkg/md/fmt_test.go | 10 ++++----- pkg/md/md.go | 8 ++++++++ pkg/md/optrace_test.go | 28 +++++++++++++++++++++++++ pkg/md/zstring.go | 46 ++++++++++++++++++++++-------------------- 5 files changed, 68 insertions(+), 27 deletions(-) create mode 100644 pkg/md/optrace_test.go diff --git a/pkg/md/fmt.go b/pkg/md/fmt.go index fcca99d8..c7e09294 100644 --- a/pkg/md/fmt.go +++ b/pkg/md/fmt.go @@ -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: diff --git a/pkg/md/fmt_test.go b/pkg/md/fmt_test.go index 4bb30ce9..7bff649f 100644 --- a/pkg/md/fmt_test.go +++ b/pkg/md/fmt_test.go @@ -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{}))) } } diff --git a/pkg/md/md.go b/pkg/md/md.go index 5a1210d8..ba0b9783 100644 --- a/pkg/md/md.go +++ b/pkg/md/md.go @@ -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() diff --git a/pkg/md/optrace_test.go b/pkg/md/optrace_test.go new file mode 100644 index 00000000..64861b09 --- /dev/null +++ b/pkg/md/optrace_test.go @@ -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) + } +} diff --git a/pkg/md/zstring.go b/pkg/md/zstring.go index 908b989e..734b697a 100644 --- a/pkg/md/zstring.go +++ b/pkg/md/zstring.go @@ -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) {