Skip to content

Commit

Permalink
Add newlines in line number spans when wrapping in an HTML table.
Browse files Browse the repository at this point in the history
Since these are wrapped in a `<pre>`, newlines hint the browser that the
line numbers should be on separate lines. This helps when rendering
content with broken CSS, or in a text-only browser.
  • Loading branch information
jmillikin authored and alecthomas committed Feb 25, 2018
1 parent 10c530a commit d7ee3c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions formatters/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma
fmt.Fprintf(w, "<span%s>", f.styleAttr(css, chroma.LineHighlight))
}

fmt.Fprintf(w, "<span%s>%*d</span>", f.styleAttr(css, chroma.LineNumbersTable), lineDigits, line)
fmt.Fprintf(w, "<span%s>%*d\n</span>", f.styleAttr(css, chroma.LineNumbersTable), lineDigits, line)

if highlight {
fmt.Fprintf(w, "</span>")
Expand Down Expand Up @@ -327,7 +327,7 @@ func (f *Formatter) styleToCSS(style *chroma.Style) map[chroma.TokenType]string
lineNumbersStyle := "margin-right: 0.4em; padding: 0 0.4em 0 0.4em;"
// All rules begin with default rules followed by user provided rules
classes[chroma.LineNumbers] = lineNumbersStyle + classes[chroma.LineNumbers]
classes[chroma.LineNumbersTable] = lineNumbersStyle + " display: block;" + classes[chroma.LineNumbersTable]
classes[chroma.LineNumbersTable] = lineNumbersStyle + classes[chroma.LineNumbersTable]
classes[chroma.LineHighlight] = "display: block; width: 100%;" + classes[chroma.LineHighlight]
classes[chroma.LineTable] = "border-spacing: 0; padding: 0; margin: 0; border: 0; width: auto; overflow: auto; display: block;" + classes[chroma.LineTable]
classes[chroma.LineTableTD] = "vertical-align: top; padding: 0; margin: 0; border: 0;" + classes[chroma.LineTableTD]
Expand Down
19 changes: 19 additions & 0 deletions formatters/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,22 @@ func TestClassPrefix(t *testing.T) {
t.Error("Stylesheets should have a class prefix")
}
}

func TestTableLineNumberNewlines(t *testing.T) {
f := New(WithClasses(), WithLineNumbers(), LineNumbersInTable())
it, err := lexers.Get("go").Tokenise(nil, "package main\nfunc main()\n{\nprintln(`hello world`)\n}\n")
assert.NoError(t, err)

var buf bytes.Buffer
err = f.Format(&buf, styles.Fallback, it)
assert.NoError(t, err)

// Don't bother testing the whole output, just verify it's got line numbers
// in a <pre>-friendly format.
// Note: placing the newlines inside the <span> lets browser selections look
// better, instead of "skipping" over the span margin.
assert.Contains(t, buf.String(), `<span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span>`)
}

0 comments on commit d7ee3c1

Please sign in to comment.