mirror of
https://github.com/alecthomas/chroma.git
synced 2025-03-19 21:10:15 +02:00
parent
d1be6303e6
commit
c984ca45c7
@ -44,6 +44,7 @@ var (
|
|||||||
htmlLinesStyleFlag = kingpin.Flag("html-lines-style", "Style for line numbers.").String()
|
htmlLinesStyleFlag = kingpin.Flag("html-lines-style", "Style for line numbers.").String()
|
||||||
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()
|
||||||
|
|
||||||
filesArgs = kingpin.Arg("files", "Files to highlight.").ExistingFiles()
|
filesArgs = kingpin.Arg("files", "Files to highlight.").ExistingFiles()
|
||||||
)
|
)
|
||||||
@ -109,7 +110,10 @@ command, for Go.
|
|||||||
kingpin.FatalIfError(err, "")
|
kingpin.FatalIfError(err, "")
|
||||||
|
|
||||||
if *formatterFlag == "html" {
|
if *formatterFlag == "html" {
|
||||||
options := []html.Option{html.TabWidth(*htmlTabWidthFlag)}
|
options := []html.Option{
|
||||||
|
html.TabWidth(*htmlTabWidthFlag),
|
||||||
|
html.BaseLineNumber(*htmlBaseLineFlag),
|
||||||
|
}
|
||||||
if *htmlPrefixFlag != "" {
|
if *htmlPrefixFlag != "" {
|
||||||
options = append(options, html.ClassPrefix(*htmlPrefixFlag))
|
options = append(options, html.ClassPrefix(*htmlPrefixFlag))
|
||||||
}
|
}
|
||||||
|
@ -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.
|
// New HTML formatter.
|
||||||
func New(options ...Option) *Formatter {
|
func New(options ...Option) *Formatter {
|
||||||
f := &Formatter{}
|
f := &Formatter{
|
||||||
|
baseLineNumber: 1,
|
||||||
|
}
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
option(f)
|
option(f)
|
||||||
}
|
}
|
||||||
@ -59,6 +68,7 @@ type Formatter struct {
|
|||||||
tabWidth int
|
tabWidth int
|
||||||
lineNumbers bool
|
lineNumbers bool
|
||||||
highlightRanges highlightRanges
|
highlightRanges highlightRanges
|
||||||
|
baseLineNumber int
|
||||||
}
|
}
|
||||||
|
|
||||||
type highlightRanges [][2]int
|
type highlightRanges [][2]int
|
||||||
@ -131,14 +141,16 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma
|
|||||||
lines := splitTokensIntoLines(tokens)
|
lines := splitTokensIntoLines(tokens)
|
||||||
lineDigits := len(fmt.Sprintf("%d", len(lines)))
|
lineDigits := len(fmt.Sprintf("%d", len(lines)))
|
||||||
highlightIndex := 0
|
highlightIndex := 0
|
||||||
for line, tokens := range lines {
|
for index, tokens := range lines {
|
||||||
|
// 1-based line number.
|
||||||
|
line := f.baseLineNumber + index
|
||||||
highlight := false
|
highlight := false
|
||||||
for highlightIndex < len(f.highlightRanges) && line+1 > f.highlightRanges[highlightIndex][1] {
|
for highlightIndex < len(f.highlightRanges) && line > f.highlightRanges[highlightIndex][1] {
|
||||||
highlightIndex++
|
highlightIndex++
|
||||||
}
|
}
|
||||||
if highlightIndex < len(f.highlightRanges) {
|
if highlightIndex < len(f.highlightRanges) {
|
||||||
hrange := f.highlightRanges[highlightIndex]
|
hrange := f.highlightRanges[highlightIndex]
|
||||||
if line+1 >= hrange[0] && line+1 <= hrange[1] {
|
if line >= hrange[0] && line <= hrange[1] {
|
||||||
highlight = true
|
highlight = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -146,7 +158,7 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma
|
|||||||
fmt.Fprintf(w, "<span%s>", f.styleAttr(css, chroma.LineHighlight))
|
fmt.Fprintf(w, "<span%s>", f.styleAttr(css, chroma.LineHighlight))
|
||||||
}
|
}
|
||||||
if f.lineNumbers {
|
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 {
|
for _, token := range tokens {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user