Skip to content

Commit

Permalink
Add html.BaseLineNumber(n).
Browse files Browse the repository at this point in the history
Fixes #22.
  • Loading branch information
alecthomas committed Sep 24, 2017
1 parent d1be630 commit c984ca4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
6 changes: 5 additions & 1 deletion cmd/chroma/main.go
Expand Up @@ -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()
)
Expand Down Expand Up @@ -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))
}
Expand Down
22 changes: 17 additions & 5 deletions formatters/html/html.go
Expand Up @@ -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)
}
Expand All @@ -59,6 +68,7 @@ type Formatter struct {
tabWidth int
lineNumbers bool
highlightRanges highlightRanges
baseLineNumber int
}

type highlightRanges [][2]int
Expand Down Expand Up @@ -131,22 +141,24 @@ 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
}
}
if highlight {
fmt.Fprintf(w, "<span%s>", f.styleAttr(css, chroma.LineHighlight))
}
if f.lineNumbers {
fmt.Fprintf(w, "<span%s>%*d</span>", f.styleAttr(css, chroma.LineNumbers), lineDigits, line+1)
fmt.Fprintf(w, "<span%s>%*d</span>", f.styleAttr(css, chroma.LineNumbers), lineDigits, line)
}

for _, token := range tokens {
Expand Down

0 comments on commit c984ca4

Please sign in to comment.