Format syntax highlighted output manually
masterFor full control, follow these steps to manually tokenize and format code:
- Get the Lexer, Style, and Formatter: Use
lexers.Get,styles.Get, andformatters.Get. Always check forniland use the respectiveFallbackif necessary. - Tokenize: Call
lexer.Tokenise(nil, content)to get an iterator. - Format: Call
formatter.Format(writer, style, iterator)to produce the final output.
style := styles.Get("swapoff")
if style == nil {
style = styles.Fallback
}
formatter := formatters.Get("html")
if formatter == nil {
formatter = formatters.Fallback
}
contents, err := io.ReadAll(r)
iterator, err := lexer.Tokenise(nil, string(contents))
err := formatter.Format(w, style, iterator)