diff --git a/cmd/chroma/main.go b/cmd/chroma/main.go index 216bf9e..738ef58 100644 --- a/cmd/chroma/main.go +++ b/cmd/chroma/main.go @@ -43,18 +43,19 @@ var ( jsonFlag = kingpin.Flag("json", "Output JSON representation of tokens.").Bool() - htmlFlag = kingpin.Flag("html", "Enable HTML mode (equivalent to '--formatter html').").Bool() - htmlPrefixFlag = kingpin.Flag("html-prefix", "HTML CSS class prefix.").PlaceHolder("PREFIX").String() - htmlStylesFlag = kingpin.Flag("html-styles", "Output HTML CSS styles.").Bool() - htmlOnlyFlag = kingpin.Flag("html-only", "Output HTML fragment.").Bool() - htmlInlineStyleFlag = kingpin.Flag("html-inline-styles", "Output HTML with inline styles (no classes).").Bool() - htmlTabWidthFlag = kingpin.Flag("html-tab-width", "Set the HTML tab width.").Default("8").Int() - htmlLinesFlag = kingpin.Flag("html-lines", "Include line numbers in output.").Bool() - htmlLinesTableFlag = kingpin.Flag("html-lines-table", "Split line numbers and code in a HTML table").Bool() - htmlLinesStyleFlag = kingpin.Flag("html-lines-style", "Style for line numbers.").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() - htmlBaseLineFlag = kingpin.Flag("html-base-line", "Base line number.").Default("1").Int() + htmlFlag = kingpin.Flag("html", "Enable HTML mode (equivalent to '--formatter html').").Bool() + htmlPrefixFlag = kingpin.Flag("html-prefix", "HTML CSS class prefix.").PlaceHolder("PREFIX").String() + htmlStylesFlag = kingpin.Flag("html-styles", "Output HTML CSS styles.").Bool() + htmlOnlyFlag = kingpin.Flag("html-only", "Output HTML fragment.").Bool() + htmlInlineStyleFlag = kingpin.Flag("html-inline-styles", "Output HTML with inline styles (no classes).").Bool() + htmlTabWidthFlag = kingpin.Flag("html-tab-width", "Set the HTML tab width.").Default("8").Int() + htmlLinesFlag = kingpin.Flag("html-lines", "Include line numbers in output.").Bool() + htmlLinesTableFlag = kingpin.Flag("html-lines-table", "Split line numbers and code in a HTML table").Bool() + htmlLinesStyleFlag = kingpin.Flag("html-lines-style", "Style for line numbers.").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() + 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() ) @@ -151,6 +152,9 @@ command, for Go. if *htmlLinesTableFlag { options = append(options, html.LineNumbersInTable()) } + if *htmlPreventSurroundingPreFlag { + options = append(options, html.PreventSurroundingPre()) + } if len(*htmlHighlightFlag) > 0 { ranges := [][2]int{} for _, span := range strings.Split(*htmlHighlightFlag, ",") { diff --git a/formatters/html/html.go b/formatters/html/html.go index 806721c..05e464f 100644 --- a/formatters/html/html.go +++ b/formatters/html/html.go @@ -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. 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. func WithLineNumbers() Option { return func(f *Formatter) { @@ -70,14 +73,15 @@ func New(options ...Option) *Formatter { // Formatter that generates HTML. type Formatter struct { - standalone bool - prefix string - Classes bool // Exported field to detect when classes are being used - tabWidth int - lineNumbers bool - lineNumbersInTable bool - highlightRanges highlightRanges - baseLineNumber int + standalone bool + prefix string + Classes bool // Exported field to detect when classes are being used + preventSurroundingPre bool + tabWidth int + lineNumbers bool + lineNumbersInTable bool + highlightRanges highlightRanges + baseLineNumber int } type highlightRanges [][2]int @@ -158,7 +162,9 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma fmt.Fprintf(w, "\n", f.styleAttr(css, chroma.Background)) fmt.Fprintf(w, "", f.styleAttr(css, chroma.LineTable)) fmt.Fprintf(w, "\n", f.styleAttr(css, chroma.LineTableTD)) - fmt.Fprintf(w, "", f.styleAttr(css, chroma.Background)) + if !f.preventSurroundingPre { + fmt.Fprintf(w, "", f.styleAttr(css, chroma.Background)) + } for index := range lines { line := f.baseLineNumber + index 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, "") } } - fmt.Fprint(w, "\n") + if !f.preventSurroundingPre { + fmt.Fprint(w, "") + } + fmt.Fprint(w, "\n") fmt.Fprintf(w, "\n", f.styleAttr(css, chroma.LineTableTD)) } - fmt.Fprintf(w, "", f.styleAttr(css, chroma.Background)) + if !f.preventSurroundingPre { + fmt.Fprintf(w, "", f.styleAttr(css, chroma.Background)) + } highlightIndex = 0 for index, tokens := range lines { // 1-based line number. @@ -209,7 +220,9 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma } } - fmt.Fprint(w, "") + if !f.preventSurroundingPre { + fmt.Fprint(w, "") + } if wrapInTable { fmt.Fprint(w, "\n")