mirror of
https://github.com/alecthomas/chroma.git
synced 2025-04-04 22:24:23 +02:00
Add "tokens" formatter.
This commit is contained in:
parent
5749aebe42
commit
5f7faa38d5
@ -17,10 +17,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
profileFlag = kingpin.Flag("profile", "Enable profiling to file.").PlaceHolder("FILE").String()
|
profileFlag = kingpin.Flag("profile", "Enable profiling to file.").PlaceHolder("FILE").Hidden().String()
|
||||||
listFlag = kingpin.Flag("list", "List lexers, styles and formatters.").Bool()
|
listFlag = kingpin.Flag("list", "List lexers, styles and formatters.").Bool()
|
||||||
|
|
||||||
tokensFlag = kingpin.Flag("tokens", "Dump raw tokens.").Bool()
|
|
||||||
lexerFlag = kingpin.Flag("lexer", "Lexer to use when formatting.").Default("autodetect").Short('l').String()
|
lexerFlag = kingpin.Flag("lexer", "Lexer to use when formatting.").Default("autodetect").Short('l').String()
|
||||||
styleFlag = kingpin.Flag("style", "Style to use for formatting.").Short('s').Default("swapoff").String()
|
styleFlag = kingpin.Flag("style", "Style to use for formatting.").Short('s').Default("swapoff").String()
|
||||||
formatterFlag = kingpin.Flag("formatter", "Formatter to use.").Default("terminal").Short('f').String()
|
formatterFlag = kingpin.Flag("formatter", "Formatter to use.").Default("terminal").Short('f').String()
|
||||||
@ -89,11 +88,6 @@ func selexer(path string) chroma.Lexer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getWriter(w io.Writer) func(*chroma.Token) {
|
func getWriter(w io.Writer) func(*chroma.Token) {
|
||||||
if *tokensFlag {
|
|
||||||
return func(token *chroma.Token) {
|
|
||||||
fmt.Printf("%#v\n", token)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
style := styles.Get(*styleFlag)
|
style := styles.Get(*styleFlag)
|
||||||
formatter := formatters.Get(*formatterFlag)
|
formatter := formatters.Get(*formatterFlag)
|
||||||
// formatter := formatters.TTY8
|
// formatter := formatters.TTY8
|
||||||
|
16
formatters/tokens.go
Normal file
16
formatters/tokens.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package formatters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/alecthomas/chroma"
|
||||||
|
"github.com/alecthomas/chroma/styles"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tokens formatter outputs the raw token structures.
|
||||||
|
var Tokens = Register("tokens", FormatterFunc(func(w io.Writer, s *styles.Style) (func(*chroma.Token), error) {
|
||||||
|
return func(token *chroma.Token) {
|
||||||
|
fmt.Fprintln(w, token.GoString())
|
||||||
|
}, nil
|
||||||
|
}))
|
Loading…
x
Reference in New Issue
Block a user