Skip to content

Commit

Permalink
present: test Markdown & fix splitting of pre blocks
Browse files Browse the repository at this point in the history
Consider this input:

	This is preformatted:

		line 1
		line 2

		line 3 after blank line

Before, this would split into two different <pre> sections
in Markdown mode. Now it matches legacy mode and doesn't.

Fixes golang/go#37972.

Change-Id: I6bf156c76ef9c63b6c3ffe6cce431d9589def867
Reviewed-on: https://go-review.googlesource.com/c/tools/+/224958
Reviewed-by: Andrew Bonventre <andybons@golang.org>
  • Loading branch information
rsc committed Mar 23, 2020
1 parent e41bc02 commit e609210
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 26 deletions.
77 changes: 51 additions & 26 deletions present/parse.go
Expand Up @@ -470,13 +470,61 @@ func parseSections(ctx *Context, name, prefix string, lines *Lines, number []int
return nil, err
}
e = t

case isMarkdown:
// Collect Markdown lines, including blank lines and indented text.
var block []string
endLine, endBlock := lines.line-1, -1 // end is last non-empty line
for ok {
trim := strings.TrimSpace(text)
if trim != "" {
// Command breaks text block.
// Section heading breaks text block in markdown.
if text[0] == '.' || text[0] == '#' || isSpeakerNote(text) {
break
}
if strings.HasPrefix(text, `\.`) { // Backslash escapes initial period.
text = text[1:]
}
endLine, endBlock = lines.line, len(block)
}
block = append(block, text)
text, ok = lines.next()
}
block = block[:endBlock+1]
lines.line = endLine + 1
if len(block) == 0 {
break
}

// Replace all leading tabs with 4 spaces,
// which render better in code blocks.
// CommonMark defines that for parsing the structure of the file
// a tab is equivalent to 4 spaces, so this change won't
// affect the later parsing at all.
// An alternative would be to apply this to code blocks after parsing,
// at the same time that we update <a> targets, but that turns out
// to be quite difficult to modify in the AST.
for i, line := range block {
if len(line) > 0 && line[0] == '\t' {
short := strings.TrimLeft(line, "\t")
line = strings.Repeat(" ", len(line)-len(short)) + short
block[i] = line
}
}
html, err := renderMarkdown([]byte(strings.Join(block, "\n")))
if err != nil {
return nil, err
}
e = HTML{HTML: html}

default:
// Collect text lines.
var block []string
for ok && strings.TrimSpace(text) != "" {
// Command breaks text block.
// Section heading breaks text block in markdown.
if text[0] == '.' || isMarkdown && text[0] == '#' || isSpeakerNote(text) {
lines.back()
if text[0] == '.' || isSpeakerNote(text) {
break
}
if strings.HasPrefix(text, `\.`) { // Backslash escapes initial period.
Expand All @@ -488,30 +536,7 @@ func parseSections(ctx *Context, name, prefix string, lines *Lines, number []int
if len(block) == 0 {
break
}
if isMarkdown {
// Replace all leading tabs with 4 spaces,
// which render better in code blocks.
// CommonMark defines that for parsing the structure of the file
// a tab is equivalent to 4 spaces, so this change won't
// affect the later parsing at all.
// An alternative would be to apply this to code blocks after parsing,
// at the same time that we update <a> targets, but that turns out
// to be quite difficult to modify in the AST.
for i, line := range block {
if len(line) > 0 && line[0] == '\t' {
short := strings.TrimLeft(line, "\t")
line = strings.Repeat(" ", len(line)-len(short)) + short
block[i] = line
}
}
html, err := renderMarkdown([]byte(strings.Join(block, "\n")))
if err != nil {
return nil, err
}
e = HTML{HTML: html}
} else {
e = Text{Lines: block}
}
e = Text{Lines: block}
}
if e != nil {
section.Elem = append(section.Elem, e)
Expand Down
24 changes: 24 additions & 0 deletions present/testdata/basic.md
@@ -0,0 +1,24 @@
# Title
Subtitle

Name

## Heading

Text
on two lines.

More text.

---
<h1>Title</h1>
<h2>Subtitle</h2>
<author>
<p>Name</p>
</author>
<section>
<h2 id="TOC_1.">Heading</h2>
<p>Text
on two lines.</p>
<p>More text.</p>
</section>
39 changes: 39 additions & 0 deletions present/testdata/code.md
@@ -0,0 +1,39 @@
# Code

##

Code:

.code testdata/code.txt

Snippet:

.code testdata/code.txt /Snippet/

Highlight:

.code testdata/code.txt HL1

---
<h1>Code</h1>
<section>
<p>Code:</p>
<div class="code">
<pre><span num="1">code file</span>
<span num="2">Snippet</span>
<span num="3">important</span>
</pre>
</div>
<p>Snippet:</p>
<div class="code">
<pre><span num="2">Snippet</span>
</pre>
</div>
<p>Highlight:</p>
<div class="code">
<pre><span num="1">code file</span>
<span num="2">Snippet</span>
<span num="3"><b>important</b></span>
</pre>
</div>
</section>
34 changes: 34 additions & 0 deletions present/testdata/list.md
@@ -0,0 +1,34 @@
# List

##

- Item 1
on two lines.
- Item 2.
- Item 3.

- Item 4 in list despite preceding blank line.
- Item 5.

---
<h1>List</h1>
<section>
<ul>
<li>
<p>Item 1
on two lines.</p>
</li>
<li>
<p>Item 2.</p>
</li>
<li>
<p>Item 3.</p>
</li>
<li>
<p>Item 4 in list despite preceding blank line.</p>
</li>
<li>
<p>Item 5.</p>
</li>
</ul>
</section>
22 changes: 22 additions & 0 deletions present/testdata/media.md
@@ -0,0 +1,22 @@
# Media

##

.image gopher.jpg _ 100
.caption A gopher.

.iframe https://golang.org/

.link https://golang.org/ The Gopher's home.

.html testdata/media.html

---
<h1>Media</h1>
<section>
<img src="gopher.jpg" width="100" alt="">
<figcaption>A gopher.</figcaption>
<iframe src="https://golang.org/"></iframe>
<p class="link"><a href="https://golang.org/">The Gopher&#39;s home.</a></p>
<!-- media.html -->
</section>
39 changes: 39 additions & 0 deletions present/testdata/pre.md
@@ -0,0 +1,39 @@
# Pre

##

Pre with tab:

code block
on two lines

Text with space:

now a text block
on two lines

Pre with blank line:

before

after

EOF

---
<h1>Pre</h1>
<section>
<p>Pre with tab:</p>
<pre><code>code block
on two lines
</code></pre>
<p>Text with space:</p>
<p>now a text block
on two lines</p>
<p>Pre with blank line:</p>
<pre><code>before

after
</code></pre>
<p>EOF</p>
</section>

0 comments on commit e609210

Please sign in to comment.