From c984ca45c7f8c531cedb6cbf16fe9a7eb433ea9f Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sun, 24 Sep 2017 20:33:50 +1000 Subject: [PATCH] Add html.BaseLineNumber(n). Fixes #22. --- cmd/chroma/main.go | 6 +++++- formatters/html/html.go | 22 +++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/chroma/main.go b/cmd/chroma/main.go index 32726c5..6c15f86 100644 --- a/cmd/chroma/main.go +++ b/cmd/chroma/main.go @@ -44,6 +44,7 @@ var ( 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() filesArgs = kingpin.Arg("files", "Files to highlight.").ExistingFiles() ) @@ -109,7 +110,10 @@ command, for Go. kingpin.FatalIfError(err, "") if *formatterFlag == "html" { - options := []html.Option{html.TabWidth(*htmlTabWidthFlag)} + options := []html.Option{ + html.TabWidth(*htmlTabWidthFlag), + html.BaseLineNumber(*htmlBaseLineFlag), + } if *htmlPrefixFlag != "" { options = append(options, html.ClassPrefix(*htmlPrefixFlag)) } diff --git a/formatters/html/html.go b/formatters/html/html.go index e71cf9a..2f01f6c 100644 --- a/formatters/html/html.go +++ b/formatters/html/html.go @@ -42,9 +42,18 @@ func HighlightLines(ranges [][2]int) Option { } } +// BaseLineNumber sets the initial number to start line numbering at. Defaults to 1. +func BaseLineNumber(n int) Option { + return func(f *Formatter) { + f.baseLineNumber = n + } +} + // New HTML formatter. func New(options ...Option) *Formatter { - f := &Formatter{} + f := &Formatter{ + baseLineNumber: 1, + } for _, option := range options { option(f) } @@ -59,6 +68,7 @@ type Formatter struct { tabWidth int lineNumbers bool highlightRanges highlightRanges + baseLineNumber int } type highlightRanges [][2]int @@ -131,14 +141,16 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma lines := splitTokensIntoLines(tokens) lineDigits := len(fmt.Sprintf("%d", len(lines))) highlightIndex := 0 - for line, tokens := range lines { + for index, tokens := range lines { + // 1-based line number. + line := f.baseLineNumber + index highlight := false - for highlightIndex < len(f.highlightRanges) && line+1 > f.highlightRanges[highlightIndex][1] { + for highlightIndex < len(f.highlightRanges) && line > f.highlightRanges[highlightIndex][1] { highlightIndex++ } if highlightIndex < len(f.highlightRanges) { hrange := f.highlightRanges[highlightIndex] - if line+1 >= hrange[0] && line+1 <= hrange[1] { + if line >= hrange[0] && line <= hrange[1] { highlight = true } } @@ -146,7 +158,7 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma fmt.Fprintf(w, "", f.styleAttr(css, chroma.LineHighlight)) } if f.lineNumbers { - fmt.Fprintf(w, "%*d", f.styleAttr(css, chroma.LineNumbers), lineDigits, line+1) + fmt.Fprintf(w, "%*d", f.styleAttr(css, chroma.LineNumbers), lineDigits, line) } for _, token := range tokens {