1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-17 20:58:08 +02:00

Add option to prevent surroundign pre tags

This commit is contained in:
Felix Knischewski 2018-09-18 11:39:03 +02:00 committed by Alec Thomas
parent 0ee91688c8
commit e36baa268a
2 changed files with 41 additions and 24 deletions

View File

@ -55,6 +55,7 @@ var (
htmlHighlightFlag = kingpin.Flag("html-highlight", "Highlight these lines.").PlaceHolder("N[:M][,...]").String() htmlHighlightFlag = kingpin.Flag("html-highlight", "Highlight these lines.").PlaceHolder("N[:M][,...]").String()
htmlHighlightStyleFlag = kingpin.Flag("html-highlight-style", "Style used for highlighting lines.").String() htmlHighlightStyleFlag = kingpin.Flag("html-highlight-style", "Style used for highlighting lines.").String()
htmlBaseLineFlag = kingpin.Flag("html-base-line", "Base line number.").Default("1").Int() htmlBaseLineFlag = kingpin.Flag("html-base-line", "Base line number.").Default("1").Int()
htmlPreventSurroundingPreFlag = kingpin.Flag("html-prevent-surrounding-pre", "Prevent the surrounding pre tag.").Bool()
filesArgs = kingpin.Arg("files", "Files to highlight.").ExistingFiles() filesArgs = kingpin.Arg("files", "Files to highlight.").ExistingFiles()
) )
@ -151,6 +152,9 @@ command, for Go.
if *htmlLinesTableFlag { if *htmlLinesTableFlag {
options = append(options, html.LineNumbersInTable()) options = append(options, html.LineNumbersInTable())
} }
if *htmlPreventSurroundingPreFlag {
options = append(options, html.PreventSurroundingPre())
}
if len(*htmlHighlightFlag) > 0 { if len(*htmlHighlightFlag) > 0 {
ranges := [][2]int{} ranges := [][2]int{}
for _, span := range strings.Split(*htmlHighlightFlag, ",") { for _, span := range strings.Split(*htmlHighlightFlag, ",") {

View File

@ -25,6 +25,9 @@ func WithClasses() Option { return func(f *Formatter) { f.Classes = true } }
// TabWidth sets the number of characters for a tab. Defaults to 8. // TabWidth sets the number of characters for a tab. Defaults to 8.
func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } } func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } }
// PreventSurroundingPre prevents the surrounding pre tags around the generated code
func PreventSurroundingPre() Option { return func(f *Formatter) { f.preventSurroundingPre = true } }
// WithLineNumbers formats output with line numbers. // WithLineNumbers formats output with line numbers.
func WithLineNumbers() Option { func WithLineNumbers() Option {
return func(f *Formatter) { return func(f *Formatter) {
@ -73,6 +76,7 @@ type Formatter struct {
standalone bool standalone bool
prefix string prefix string
Classes bool // Exported field to detect when classes are being used Classes bool // Exported field to detect when classes are being used
preventSurroundingPre bool
tabWidth int tabWidth int
lineNumbers bool lineNumbers bool
lineNumbersInTable bool lineNumbersInTable bool
@ -158,7 +162,9 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma
fmt.Fprintf(w, "<div%s>\n", f.styleAttr(css, chroma.Background)) 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, "<table%s><tr>", f.styleAttr(css, chroma.LineTable))
fmt.Fprintf(w, "<td%s>\n", f.styleAttr(css, chroma.LineTableTD)) fmt.Fprintf(w, "<td%s>\n", f.styleAttr(css, chroma.LineTableTD))
if !f.preventSurroundingPre {
fmt.Fprintf(w, "<pre%s>", f.styleAttr(css, chroma.Background)) fmt.Fprintf(w, "<pre%s>", f.styleAttr(css, chroma.Background))
}
for index := range lines { for index := range lines {
line := f.baseLineNumber + index line := f.baseLineNumber + index
highlight, next := f.shouldHighlight(highlightIndex, line) highlight, next := f.shouldHighlight(highlightIndex, line)
@ -175,11 +181,16 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma
fmt.Fprintf(w, "</span>") fmt.Fprintf(w, "</span>")
} }
} }
fmt.Fprint(w, "</pre></td>\n") if !f.preventSurroundingPre {
fmt.Fprint(w, "</pre>")
}
fmt.Fprint(w, "</td>\n")
fmt.Fprintf(w, "<td%s>\n", f.styleAttr(css, chroma.LineTableTD)) fmt.Fprintf(w, "<td%s>\n", f.styleAttr(css, chroma.LineTableTD))
} }
if !f.preventSurroundingPre {
fmt.Fprintf(w, "<pre%s>", f.styleAttr(css, chroma.Background)) fmt.Fprintf(w, "<pre%s>", f.styleAttr(css, chroma.Background))
}
highlightIndex = 0 highlightIndex = 0
for index, tokens := range lines { for index, tokens := range lines {
// 1-based line number. // 1-based line number.
@ -209,7 +220,9 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma
} }
} }
if !f.preventSurroundingPre {
fmt.Fprint(w, "</pre>") fmt.Fprint(w, "</pre>")
}
if wrapInTable { if wrapInTable {
fmt.Fprint(w, "</td></tr></table>\n") fmt.Fprint(w, "</td></tr></table>\n")