mirror of
https://github.com/alecthomas/chroma.git
synced 2025-03-21 21:17:50 +02:00
More README.
This commit is contained in:
parent
ca585f2681
commit
1374cf9ffb
43
README.md
43
README.md
@ -1,14 +1,32 @@
|
|||||||
# Chroma - A general purpose syntax highlighter for Go
|
# Chroma - A general purpose syntax highlighter for Go
|
||||||
|
|
||||||
Chroma takes source code and other structured text and converts it into syntax
|
Chroma takes source code and other structured text and converts it into syntax
|
||||||
highlighted output, as HTML, ANSI-coloured text, etc.
|
highlighted HTML, ANSI-coloured text, etc.
|
||||||
|
|
||||||
Chroma is based heavily on [Pygments](http://pygments.org/), and includes
|
Chroma is based heavily on [Pygments](http://pygments.org/), and includes
|
||||||
translaters for Pygments lexers and styles.
|
translaters for Pygments lexers and styles.
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
<!-- MarkdownTOC -->
|
||||||
|
|
||||||
|
- [Using the library](#using-the-library)
|
||||||
|
- [Quick start](#quick-start)
|
||||||
|
- [Identifying the language](#identifying-the-language)
|
||||||
|
- [Formatting the output](#formatting-the-output)
|
||||||
|
- [The HTML formatter](#the-html-formatter)
|
||||||
|
- [More detail](#more-detail)
|
||||||
|
- [Lexers](#lexers)
|
||||||
|
- [Formatters](#formatters)
|
||||||
|
- [Styles](#styles)
|
||||||
|
- [Command-line interface](#command-line-interface)
|
||||||
|
- [What's missing compared to Pygments?](#whats-missing-compared-to-pygments)
|
||||||
|
|
||||||
|
<!-- /MarkdownTOC -->
|
||||||
|
|
||||||
## Using the library
|
## Using the library
|
||||||
|
|
||||||
Chroma, like Pygments, has the concept of
|
Chroma, like Pygments, has the concepts of
|
||||||
[lexers](https://github.com/alecthomas/chroma/tree/master/lexers),
|
[lexers](https://github.com/alecthomas/chroma/tree/master/lexers),
|
||||||
[formatters](https://github.com/alecthomas/chroma/tree/master/formatters) and
|
[formatters](https://github.com/alecthomas/chroma/tree/master/formatters) and
|
||||||
[styles](https://github.com/alecthomas/chroma/tree/master/styles).
|
[styles](https://github.com/alecthomas/chroma/tree/master/styles).
|
||||||
@ -102,6 +120,27 @@ contents, err := ioutil.ReadAll(r)
|
|||||||
err := lexer.Tokenise(nil, string(contents), writer)
|
err := lexer.Tokenise(nil, string(contents), writer)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### The HTML formatter
|
||||||
|
|
||||||
|
By default the `html` registered formatter generates standalone HTML with
|
||||||
|
embedded CSS. More flexibility is available through the `lexers/html` package.
|
||||||
|
|
||||||
|
Firstly, the output generated by the formatter can be customised with the
|
||||||
|
following constructor options:
|
||||||
|
|
||||||
|
- `Standalone()` - generate standalone HTML with embedded CSS.
|
||||||
|
- `WithClasses()` - use classes rather than inlined style attributes.
|
||||||
|
- `ClassPrefix(prefix)` - prefix each generated CSS class.
|
||||||
|
|
||||||
|
If `WithClasses()` is used, the corresponding CSS can be obtained from the formatter with:
|
||||||
|
|
||||||
|
```go
|
||||||
|
formatter := html.New(html.WithClasses())
|
||||||
|
err := formatter.WriteCSS(w, style)
|
||||||
|
```
|
||||||
|
|
||||||
|
## More detail
|
||||||
|
|
||||||
### Lexers
|
### Lexers
|
||||||
|
|
||||||
See the [Pygments documentation](http://pygments.org/docs/lexerdevelopment/)
|
See the [Pygments documentation](http://pygments.org/docs/lexerdevelopment/)
|
||||||
|
@ -77,7 +77,7 @@ command, for Go.
|
|||||||
}
|
}
|
||||||
if *htmlStylesFlag {
|
if *htmlStylesFlag {
|
||||||
formatter := html.New(html.WithClasses())
|
formatter := html.New(html.WithClasses())
|
||||||
formatter.WriteStyles(w, styles.Get(*styleFlag))
|
formatter.WriteCSS(w, styles.Get(*styleFlag))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
formatters.Register("html", html.New(options...))
|
formatters.Register("html", html.New(options...))
|
||||||
|
@ -102,7 +102,7 @@ func (h *HTMLFormatter) formatWithClasses(w io.Writer, style *chroma.Style) (fun
|
|||||||
fmt.Fprint(w, "<html>\n")
|
fmt.Fprint(w, "<html>\n")
|
||||||
}
|
}
|
||||||
fmt.Fprint(w, "<style type=\"text/css\">\n")
|
fmt.Fprint(w, "<style type=\"text/css\">\n")
|
||||||
h.WriteStyles(w, style)
|
h.WriteCSS(w, style)
|
||||||
if h.standalone {
|
if h.standalone {
|
||||||
fmt.Fprintf(w, "body { %s; }\n", classes[chroma.Background])
|
fmt.Fprintf(w, "body { %s; }\n", classes[chroma.Background])
|
||||||
}
|
}
|
||||||
@ -136,10 +136,12 @@ func (h *HTMLFormatter) formatWithClasses(w io.Writer, style *chroma.Style) (fun
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteStyles writes style definitions (without any surrounding HTML).
|
// WriteCSS writes CSS style definitions (without any surrounding HTML).
|
||||||
func (h *HTMLFormatter) WriteStyles(w io.Writer, style *chroma.Style) {
|
func (h *HTMLFormatter) WriteCSS(w io.Writer, style *chroma.Style) error {
|
||||||
classes := h.typeStyles(style)
|
classes := h.typeStyles(style)
|
||||||
fmt.Fprintf(w, "/* %s */ .chroma { %s }\n", chroma.Background, classes[chroma.Background])
|
if _, err := fmt.Fprintf(w, "/* %s */ .chroma { %s }\n", chroma.Background, classes[chroma.Background]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
tts := []int{}
|
tts := []int{}
|
||||||
for tt := range classes {
|
for tt := range classes {
|
||||||
tts = append(tts, int(tt))
|
tts = append(tts, int(tt))
|
||||||
@ -151,8 +153,11 @@ func (h *HTMLFormatter) WriteStyles(w io.Writer, style *chroma.Style) {
|
|||||||
if tt < 0 {
|
if tt < 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "/* %s */ .chroma .%ss%x { %s }\n", tt, h.prefix, int(tt), styles)
|
if _, err := fmt.Fprintf(w, "/* %s */ .chroma .%ss%x { %s }\n", tt, h.prefix, int(tt), styles); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTMLFormatter) typeStyles(style *chroma.Style) map[chroma.TokenType]string {
|
func (h *HTMLFormatter) typeStyles(style *chroma.Style) map[chroma.TokenType]string {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user