Skip to content

Commit

Permalink
Add a chroma class wrapper when in "table mode" (#63)
Browse files Browse the repository at this point in the history
This commits adds a div wrapper when linenumbers are formatted in table, to get the correct CSS matchers.

This commit also fixes some CSS table issues when tables are put into a larger context with other stylesheets:

* No borders.
* Overflow to fit in potentially narrower containers.

Fixes #62
  • Loading branch information
bep authored and alecthomas committed Oct 14, 2017
1 parent 4a55aef commit 02c4adc
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions formatters/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma

if wrapInTable {
// List line numbers in its own <td>
fmt.Fprintf(w, "<div%s>\n", f.styleAttr(css, chroma.Background))
fmt.Fprintf(w, "<table%s><tr>", f.styleAttr(css, chroma.LineTable))
fmt.Fprintf(w, "<td%s>\n", f.styleAttr(css, chroma.LineTableTD))
fmt.Fprintf(w, "<pre%s>", f.styleAttr(css, chroma.Background))
Expand Down Expand Up @@ -212,6 +213,7 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma

if wrapInTable {
fmt.Fprint(w, "</td></tr></table>\n")
fmt.Fprint(w, "</div>\n")
}

if f.standalone {
Expand Down Expand Up @@ -317,10 +319,10 @@ func (f *Formatter) styleToCSS(style *chroma.Style) map[chroma.TokenType]string
classes[chroma.Background] += f.tabWidthStyle()
lineNumbersStyle := "; margin-right: 0.4em; padding: 0 0.4em 0 0.4em;"
classes[chroma.LineNumbers] += lineNumbersStyle
classes[chroma.LineNumbersTable] += lineNumbersStyle + "; display: block"
classes[chroma.LineNumbersTable] += lineNumbersStyle + " display: block;"
classes[chroma.LineHighlight] += "; display: block; width: 100%"
classes[chroma.LineTable] += "; border-spacing: 0; padding: 0; margin: 0;"
classes[chroma.LineTableTD] += "; vertical-align: top; padding: 0; margin: 0;"
classes[chroma.LineTable] += "; border-spacing: 0; padding: 0; margin: 0; border: 0; width: 100%; overflow: auto; display: block;"
classes[chroma.LineTableTD] += "; vertical-align: top; padding: 0; margin: 0; border: 0;"

return classes
}
Expand Down

0 comments on commit 02c4adc

Please sign in to comment.