1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-19 21:10:15 +02:00

Adding HasClasses method for HTML formatter (#61)

* Adding HasClasses method for HTML formatter

* Removing helper function and simply export the field
This commit is contained in:
Depado 2017-10-13 23:41:40 +02:00 committed by Alec Thomas
parent 6604d06684
commit 4a55aefee6

View File

@ -20,7 +20,7 @@ func Standalone() Option { return func(f *Formatter) { f.standalone = true } }
func ClassPrefix(prefix string) Option { return func(f *Formatter) { f.prefix = prefix } } func ClassPrefix(prefix string) Option { return func(f *Formatter) { f.prefix = prefix } }
// WithClasses emits HTML using CSS classes, rather than inline styles. // WithClasses emits HTML using CSS classes, rather than inline styles.
func WithClasses() Option { return func(f *Formatter) { f.classes = true } } func WithClasses() Option { return func(f *Formatter) { f.Classes = true } }
// TabWidth sets the number of characters for a tab. Defaults to 8. // TabWidth sets the number of characters for a tab. Defaults to 8.
func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } } func TabWidth(width int) Option { return func(f *Formatter) { f.tabWidth = width } }
@ -72,7 +72,7 @@ func New(options ...Option) *Formatter {
type Formatter struct { type Formatter struct {
standalone bool standalone bool
prefix string prefix string
classes bool Classes bool // Exported field to detect when classes are being used
tabWidth int tabWidth int
lineNumbers bool lineNumbers bool
lineNumbersInTable bool lineNumbersInTable bool
@ -131,14 +131,14 @@ func (f *Formatter) writeHTML(w io.Writer, style *chroma.Style, tokens []*chroma
return err return err
} }
css := f.styleToCSS(style) css := f.styleToCSS(style)
if !f.classes { if !f.Classes {
for t, style := range css { for t, style := range css {
css[t] = compressStyle(style) css[t] = compressStyle(style)
} }
} }
if f.standalone { if f.standalone {
fmt.Fprint(w, "<html>\n") fmt.Fprint(w, "<html>\n")
if f.classes { if f.Classes {
fmt.Fprint(w, "<style type=\"text/css\">\n") fmt.Fprint(w, "<style type=\"text/css\">\n")
f.WriteCSS(w, style) f.WriteCSS(w, style)
fmt.Fprintf(w, "body { %s; }\n", css[chroma.Background]) fmt.Fprintf(w, "body { %s; }\n", css[chroma.Background])
@ -249,7 +249,7 @@ func (f *Formatter) class(t chroma.TokenType) string {
} }
func (f *Formatter) styleAttr(styles map[chroma.TokenType]string, tt chroma.TokenType) string { func (f *Formatter) styleAttr(styles map[chroma.TokenType]string, tt chroma.TokenType) string {
if f.classes { if f.Classes {
cls := f.class(tt) cls := f.class(tt)
if cls == "" { if cls == "" {
return "" return ""