This commit is contained in:
51
vendor/github.com/Depado/bfchroma/.drone.yml
generated
vendored
Normal file
51
vendor/github.com/Depado/bfchroma/.drone.yml
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
---
|
||||
kind: pipeline
|
||||
name: default
|
||||
type: docker
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang:1.13.1
|
||||
volumes:
|
||||
- name: deps
|
||||
path: /go
|
||||
commands:
|
||||
- go test -race -coverprofile=coverage.txt -covermode=atomic
|
||||
|
||||
- name: linter
|
||||
image: golang:1.13.1
|
||||
volumes:
|
||||
- name: deps
|
||||
path: /go
|
||||
commands:
|
||||
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.20.0
|
||||
- ./bin/golangci-lint run
|
||||
|
||||
- name: coverage
|
||||
image: plugins/codecov
|
||||
settings:
|
||||
token:
|
||||
from_secret: codecov_token
|
||||
files:
|
||||
- coverage.txt
|
||||
|
||||
- name: telegram
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
to: 790376882
|
||||
format: markdown
|
||||
token:
|
||||
from_secret: telegram_token
|
||||
message: >
|
||||
*{{repo.name}}*
|
||||
[Build {{build.number}}]({{build.link}}) by {{commit.author}} {{#success build.status}}succeeded{{else}}failed{{/success}} in {{buildtime build.started}}
|
||||
`{{truncate commit.sha 8}}`: "{{commit.message}}"
|
||||
when:
|
||||
status:
|
||||
- success
|
||||
- failure
|
||||
|
||||
volumes:
|
||||
- name: deps
|
||||
host:
|
||||
path: /var/lib/cache/godeps/
|
||||
17
vendor/github.com/Depado/bfchroma/.gitignore
generated
vendored
Normal file
17
vendor/github.com/Depado/bfchroma/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, build with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
||||
.glide/
|
||||
vendor/
|
||||
coverage.txt
|
||||
|
||||
16
vendor/github.com/Depado/bfchroma/.golangci.yml
generated
vendored
Normal file
16
vendor/github.com/Depado/bfchroma/.golangci.yml
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
run:
|
||||
timeout: 5m
|
||||
|
||||
linters:
|
||||
enable:
|
||||
- megacheck
|
||||
- govet
|
||||
disable:
|
||||
- errcheck
|
||||
- maligned
|
||||
- prealloc
|
||||
disable-all: false
|
||||
presets:
|
||||
- bugs
|
||||
- unused
|
||||
fast: false
|
||||
21
vendor/github.com/Depado/bfchroma/LICENSE
generated
vendored
Normal file
21
vendor/github.com/Depado/bfchroma/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
209
vendor/github.com/Depado/bfchroma/README.md
generated
vendored
Normal file
209
vendor/github.com/Depado/bfchroma/README.md
generated
vendored
Normal file
@@ -0,0 +1,209 @@
|
||||
# bfchroma
|
||||
|
||||
[](https://forthebadge.com)[](https://forthebadge.com)
|
||||
|
||||

|
||||
[](https://goreportcard.com/report/github.com/Depado/bfchroma)
|
||||
[](https://drone.depado.eu/Depado/bfchroma)
|
||||
[](https://codecov.io/gh/Depado/bfchroma)
|
||||
[](https://github.com/Depado/bfchroma/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/Depado/bfchroma)
|
||||
[](https://sourcegraph.com/github.com/Depado/bfchroma?badge)
|
||||
[](https://saythanks.io/to/Depado)
|
||||
|
||||
|
||||
Integrating [Chroma](https://github.com/alecthomas/chroma) syntax highlighter as
|
||||
a [Blackfriday](https://github.com/russross/blackfriday) renderer.
|
||||
|
||||
## Install and prerequisites
|
||||
|
||||
This project requires and uses the `v2` version of
|
||||
[Blackfriday](https://github.com/russross/blackfriday/tree/v2).
|
||||
|
||||
```
|
||||
$ go get -u github.com/Depado/bfchroma
|
||||
```
|
||||
|
||||
_This project uses the module approach of go 1.11_
|
||||
|
||||
## Features
|
||||
|
||||
This renderer integrates chroma to highlight code with triple backtick notation.
|
||||
It will try to use the given language when available otherwise it will try to
|
||||
detect the language. If none of these two method works it will fallback to sane
|
||||
defaults.
|
||||
|
||||
## Usage
|
||||
|
||||
bfchroma uses the functional options approach so you can customize the behavior
|
||||
of the renderer. It uses sane defaults when no option is passed so you can use
|
||||
the renderer simply by doing so :
|
||||
|
||||
```go
|
||||
html := bf.Run([]byte(md), bf.WithRenderer(bfchroma.NewRenderer()))
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
- `Style(s string)`
|
||||
Define the style used by chroma for the rendering. The full list can be found [here](https://github.com/alecthomas/chroma/tree/master/styles)
|
||||
- `ChromaStyle(*chroma.Style)`
|
||||
This option can be used to passe directly a `*chroma.Style` instead of the
|
||||
string representing the style as with the `Style(string)` option.
|
||||
- `WithoutAutodetect()`
|
||||
By default when no language information is written in the code block, this
|
||||
renderer will try to auto-detect the used language. This option disables
|
||||
this behavior and will fallback to a sane default when no language
|
||||
information is available.
|
||||
- `EmbedCSS()`
|
||||
This option will embed CSS needed for chroma's `html.WithClasses()` at the beginning of blackfriday document.
|
||||
CSS can also be extracted separately by calling `Renderer`'s.`ChromaCSS(w)` method, which will return styleshet for currently set style
|
||||
- `Extend(bf.Renderer)`
|
||||
This option allows to define the base blackfriday that will be extended.
|
||||
- `ChromaOptions(...html.Option)`
|
||||
This option allows you to pass Chroma's html options in the renderer. Such
|
||||
options can be found [here](https://github.com/alecthomas/chroma#the-html-formatter).
|
||||
|
||||
### Option examples
|
||||
|
||||
Disabling language auto-detection and displaying line numbers
|
||||
|
||||
```go
|
||||
r := bfchroma.NewRenderer(
|
||||
bfchroma.WithoutAutodetect(),
|
||||
bfchroma.ChromaOptions(html.WithLineNumbers()),
|
||||
)
|
||||
```
|
||||
|
||||
Extend a blackfriday renderer
|
||||
|
||||
```go
|
||||
b := bf.NewHTMLRenderer(bf.HTMLRendererParameters{
|
||||
Flags: bf.CommonHTMLFlags,
|
||||
})
|
||||
|
||||
r := bfchroma.NewRenderer(bfchroma.Extend(b))
|
||||
```
|
||||
|
||||
Use a different style
|
||||
|
||||
```go
|
||||
r := bfchroma.NewRenderer(bfchroma.Style("dracula"))
|
||||
// Or
|
||||
r = bfchroma.NewRenderer(bfchroma.ChromaStyle(styles.Dracula))
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Depado/bfchroma"
|
||||
|
||||
bf "github.com/russross/blackfriday/v2"
|
||||
)
|
||||
|
||||
var md = "This is some sample code.\n\n```go\n" +
|
||||
`func main() {
|
||||
fmt.Println("Hi")
|
||||
}
|
||||
` + "```"
|
||||
|
||||
func main() {
|
||||
html := bf.Run([]byte(md), bf.WithRenderer(bfchroma.NewRenderer()))
|
||||
fmt.Println(string(html))
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Will output :
|
||||
|
||||
```html
|
||||
<p>This is some sample code.</p>
|
||||
<pre style="color:#f8f8f2;background-color:#272822"><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
|
||||
<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">"Hi"</span>)
|
||||
}
|
||||
</pre>
|
||||
```
|
||||
|
||||
## Real-life example
|
||||
|
||||
In [smallblog](https://github.com/Depado/smallblog) I'm using bfchroma to render
|
||||
my articles. It's using a combination of both bfchroma's options and blackfriday
|
||||
extensions and flags.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/Depado/bfchroma"
|
||||
"github.com/alecthomas/chroma/formatters/html"
|
||||
bf "github.com/russross/blackfriday/v2"
|
||||
)
|
||||
|
||||
// Defines the extensions that are used
|
||||
var exts = bf.NoIntraEmphasis | bf.Tables | bf.FencedCode | bf.Autolink |
|
||||
bf.Strikethrough | bf.SpaceHeadings | bf.BackslashLineBreak |
|
||||
bf.DefinitionLists | bf.Footnotes
|
||||
|
||||
// Defines the HTML rendering flags that are used
|
||||
var flags = bf.UseXHTML | bf.Smartypants | bf.SmartypantsFractions |
|
||||
bf.SmartypantsDashes | bf.SmartypantsLatexDashes | bf.TOC
|
||||
|
||||
// render will take a []byte input and will render it using a new renderer each
|
||||
// time because reusing the same can mess with TOC and header IDs
|
||||
func render(input []byte) []byte {
|
||||
return bf.Run(
|
||||
input,
|
||||
bf.WithRenderer(
|
||||
bfchroma.NewRenderer(
|
||||
bfchroma.WithoutAutodetect(),
|
||||
bfchroma.ChromaOptions(
|
||||
html.WithLineNumbers(),
|
||||
),
|
||||
bfchroma.Extend(
|
||||
bf.NewHTMLRenderer(bf.HTMLRendererParameters{
|
||||
Flags: flags,
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
bf.WithExtensions(exts),
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## Classes
|
||||
|
||||
If you have loads of code in your markdown, you might want to consider using
|
||||
`html.WithClasses()` in your `bfchroma.ChromaOptions()`. The CSS of the style
|
||||
you chose can then be accessed like this :
|
||||
|
||||
```go
|
||||
r := bfchroma.NewRenderer(
|
||||
bfchroma.WithoutAutodetect(),
|
||||
bfchroma.Extend(
|
||||
bf.NewHTMLRenderer(bf.HTMLRendererParameters{Flags: flags}),
|
||||
),
|
||||
bfchroma.Style("monokai"),
|
||||
bfchroma.ChromaOptions(html.WithClasses()),
|
||||
)
|
||||
|
||||
var css template.CSS
|
||||
|
||||
b := new(bytes.Buffer)
|
||||
if err := r.ChromaCSS(b); err != nil {
|
||||
logrus.WithError(err).Warning("Couldn't write CSS")
|
||||
}
|
||||
css = template.CSS(b.String())
|
||||
|
||||
bf.Run(input, bf.WithRenderer(r), bf.WithExtensions(exts))
|
||||
```
|
||||
|
||||
This way, you can pass your `css` var to any template and render it along the
|
||||
rendered markdown.
|
||||
11
vendor/github.com/Depado/bfchroma/go.mod
generated
vendored
Normal file
11
vendor/github.com/Depado/bfchroma/go.mod
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
module github.com/Depado/bfchroma
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/alecthomas/chroma v0.7.3
|
||||
github.com/alecthomas/repr v0.0.0-20200325044227-4184120f674c // indirect
|
||||
github.com/russross/blackfriday/v2 v2.0.1
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.6.1
|
||||
)
|
||||
46
vendor/github.com/Depado/bfchroma/go.sum
generated
vendored
Normal file
46
vendor/github.com/Depado/bfchroma/go.sum
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38 h1:smF2tmSOzy2Mm+0dGI2AIUHY+w0BUc+4tn40djz7+6U=
|
||||
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38/go.mod h1:r7bzyVFMNntcxPZXK3/+KdruV1H5KSlyVY0gc+NgInI=
|
||||
github.com/alecthomas/chroma v0.7.3 h1:NfdAERMy+esYQs8OXk0I868/qDxxCEo7FMz1WIqMAeI=
|
||||
github.com/alecthomas/chroma v0.7.3/go.mod h1:sko8vR34/90zvl5QdcUdvzL3J8NKjAUx9va9jPuFNoM=
|
||||
github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721 h1:JHZL0hZKJ1VENNfmXvHbgYlbUOvpzYzvy2aZU5gXVeo=
|
||||
github.com/alecthomas/colour v0.0.0-20160524082231-60882d9e2721/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
|
||||
github.com/alecthomas/kong v0.2.4/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
|
||||
github.com/alecthomas/repr v0.0.0-20180818092828-117648cd9897/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
|
||||
github.com/alecthomas/repr v0.0.0-20200325044227-4184120f674c h1:MVVbswUlqicyj8P/JljoocA7AyCo62gzD0O7jfvrhtE=
|
||||
github.com/alecthomas/repr v0.0.0-20200325044227-4184120f674c/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ=
|
||||
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
|
||||
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dlclark/regexp2 v1.2.0 h1:8sAhBGEM0dRWogWqWyQeIJnxjWO6oIjl8FKqREDsGfk=
|
||||
github.com/dlclark/regexp2 v1.2.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
|
||||
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 h1:opSr2sbRXk5X5/givKrrKj9HXxFpW2sdCiP8MJSKLQY=
|
||||
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
147
vendor/github.com/Depado/bfchroma/renderer.go
generated
vendored
Normal file
147
vendor/github.com/Depado/bfchroma/renderer.go
generated
vendored
Normal file
@@ -0,0 +1,147 @@
|
||||
// Package bfchroma provides an easy and extensible blackfriday renderer that
|
||||
// uses the chroma syntax highlighter to render code blocks.
|
||||
package bfchroma
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/alecthomas/chroma"
|
||||
"github.com/alecthomas/chroma/formatters/html"
|
||||
"github.com/alecthomas/chroma/lexers"
|
||||
"github.com/alecthomas/chroma/styles"
|
||||
bf "github.com/russross/blackfriday/v2"
|
||||
)
|
||||
|
||||
// Option defines the functional option type
|
||||
type Option func(r *Renderer)
|
||||
|
||||
// Style is a function option allowing to set the style used by chroma
|
||||
// Default : "monokai"
|
||||
func Style(s string) Option {
|
||||
return func(r *Renderer) {
|
||||
r.Style = styles.Get(s)
|
||||
}
|
||||
}
|
||||
|
||||
// ChromaStyle is an option to directly set the style of the renderer using a
|
||||
// chroma style instead of a string
|
||||
func ChromaStyle(s *chroma.Style) Option {
|
||||
return func(r *Renderer) {
|
||||
r.Style = s
|
||||
}
|
||||
}
|
||||
|
||||
// WithoutAutodetect disables chroma's language detection when no codeblock
|
||||
// extra information is given. It will fallback to a sane default instead of
|
||||
// trying to detect the language.
|
||||
func WithoutAutodetect() Option {
|
||||
return func(r *Renderer) {
|
||||
r.Autodetect = false
|
||||
}
|
||||
}
|
||||
|
||||
// EmbedCSS will embed CSS needed for html.WithClasses() in beginning of the document
|
||||
func EmbedCSS() Option {
|
||||
return func(r *Renderer) {
|
||||
r.embedCSS = true
|
||||
}
|
||||
}
|
||||
|
||||
// ChromaOptions allows to pass Chroma html.Option such as Standalone()
|
||||
// WithClasses(), ClassPrefix(prefix)...
|
||||
func ChromaOptions(options ...html.Option) Option {
|
||||
return func(r *Renderer) {
|
||||
r.ChromaOptions = options
|
||||
}
|
||||
}
|
||||
|
||||
// Extend allows to specify the blackfriday renderer which is extended
|
||||
func Extend(br bf.Renderer) Option {
|
||||
return func(r *Renderer) {
|
||||
r.Base = br
|
||||
}
|
||||
}
|
||||
|
||||
// NewRenderer will return a new bfchroma renderer with sane defaults
|
||||
func NewRenderer(options ...Option) *Renderer {
|
||||
r := &Renderer{
|
||||
Base: bf.NewHTMLRenderer(bf.HTMLRendererParameters{
|
||||
Flags: bf.CommonHTMLFlags,
|
||||
}),
|
||||
Style: styles.Monokai,
|
||||
Autodetect: true,
|
||||
}
|
||||
for _, option := range options {
|
||||
option(r)
|
||||
}
|
||||
r.Formatter = html.New(r.ChromaOptions...)
|
||||
return r
|
||||
}
|
||||
|
||||
// RenderWithChroma will render the given text to the w io.Writer
|
||||
func (r *Renderer) RenderWithChroma(w io.Writer, text []byte, data bf.CodeBlockData) error {
|
||||
var lexer chroma.Lexer
|
||||
|
||||
// Determining the lexer to use
|
||||
if len(data.Info) > 0 {
|
||||
lexer = lexers.Get(string(data.Info))
|
||||
} else if r.Autodetect {
|
||||
lexer = lexers.Analyse(string(text))
|
||||
}
|
||||
if lexer == nil {
|
||||
lexer = lexers.Fallback
|
||||
}
|
||||
|
||||
// Tokenize the code
|
||||
iterator, err := lexer.Tokenise(nil, string(text))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.Formatter.Format(w, r.Style, iterator)
|
||||
}
|
||||
|
||||
// Renderer is a custom Blackfriday renderer that uses the capabilities of
|
||||
// chroma to highlight code with triple backtick notation
|
||||
type Renderer struct {
|
||||
Base bf.Renderer
|
||||
Autodetect bool
|
||||
ChromaOptions []html.Option
|
||||
Style *chroma.Style
|
||||
Formatter *html.Formatter
|
||||
embedCSS bool
|
||||
}
|
||||
|
||||
// RenderNode satisfies the Renderer interface
|
||||
func (r *Renderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus {
|
||||
switch node.Type {
|
||||
case bf.Document:
|
||||
if entering && r.embedCSS {
|
||||
w.Write([]byte("<style>"))
|
||||
r.Formatter.WriteCSS(w, r.Style)
|
||||
w.Write([]byte("</style>"))
|
||||
}
|
||||
return r.Base.RenderNode(w, node, entering)
|
||||
case bf.CodeBlock:
|
||||
if err := r.RenderWithChroma(w, node.Literal, node.CodeBlockData); err != nil {
|
||||
return r.Base.RenderNode(w, node, entering)
|
||||
}
|
||||
return bf.SkipChildren
|
||||
default:
|
||||
return r.Base.RenderNode(w, node, entering)
|
||||
}
|
||||
}
|
||||
|
||||
// RenderHeader satisfies the Renderer interface
|
||||
func (r *Renderer) RenderHeader(w io.Writer, ast *bf.Node) {
|
||||
r.Base.RenderHeader(w, ast)
|
||||
}
|
||||
|
||||
// RenderFooter satisfies the Renderer interface
|
||||
func (r *Renderer) RenderFooter(w io.Writer, ast *bf.Node) {
|
||||
r.Base.RenderFooter(w, ast)
|
||||
}
|
||||
|
||||
// ChromaCSS returns CSS used with chroma's html.WithClasses() option
|
||||
func (r *Renderer) ChromaCSS(w io.Writer) error {
|
||||
return r.Formatter.WriteCSS(w, r.Style)
|
||||
}
|
||||
Reference in New Issue
Block a user