...

Source file src/github.com/alecthomas/chroma/quick/quick.go

Documentation: github.com/alecthomas/chroma/quick

     1  // Package quick provides simple, no-configuration source code highlighting.
     2  package quick
     3  
     4  import (
     5  	"io"
     6  
     7  	"github.com/alecthomas/chroma"
     8  	"github.com/alecthomas/chroma/formatters"
     9  	"github.com/alecthomas/chroma/lexers"
    10  	"github.com/alecthomas/chroma/styles"
    11  )
    12  
    13  // Highlight some text.
    14  //
    15  // Lexer, formatter and style may be empty, in which case a best-effort is made.
    16  func Highlight(w io.Writer, source, lexer, formatter, style string) error {
    17  	// Determine lexer.
    18  	l := lexers.Get(lexer)
    19  	if l == nil {
    20  		l = lexers.Analyse(source)
    21  	}
    22  	if l == nil {
    23  		l = lexers.Fallback
    24  	}
    25  	l = chroma.Coalesce(l)
    26  
    27  	// Determine formatter.
    28  	f := formatters.Get(formatter)
    29  	if f == nil {
    30  		f = formatters.Fallback
    31  	}
    32  
    33  	// Determine style.
    34  	s := styles.Get(style)
    35  	if s == nil {
    36  		s = styles.Fallback
    37  	}
    38  
    39  	it, err := l.Tokenise(nil, source)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	return f.Format(w, s, it)
    44  }
    45  

View as plain text