mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-08-30 05:36:42 -04:00
Use default styles instead of statik
This commit is contained in:
committed by
Christian Muehlhaeuser
parent
bdbe6ae0c5
commit
76ba5695bd
+23
-20
@@ -3,11 +3,10 @@ package glamour
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/rakyll/statik/fs"
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
@@ -15,11 +14,8 @@ import (
|
||||
"github.com/yuin/goldmark/util"
|
||||
|
||||
"github.com/charmbracelet/glamour/ansi"
|
||||
_ "github.com/charmbracelet/glamour/statik" // pre-generated styles
|
||||
)
|
||||
|
||||
var statikFS http.FileSystem
|
||||
|
||||
// A TermRendererOption sets an option on a TermRenderer.
|
||||
type TermRendererOption func(*TermRenderer) error
|
||||
|
||||
@@ -32,14 +28,6 @@ type TermRenderer struct {
|
||||
renderBuf bytes.Buffer
|
||||
}
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
statikFS, err = fs.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Render initializes a new TermRenderer and renders a markdown with a specific
|
||||
// style.
|
||||
func Render(in string, stylePath string) (string, error) {
|
||||
@@ -103,11 +91,12 @@ func WithBaseURL(baseURL string) TermRendererOption {
|
||||
// style.
|
||||
func WithStandardStyle(style string) TermRendererOption {
|
||||
return func(tr *TermRenderer) error {
|
||||
jsonBytes, err := fs.ReadFile(statikFS, "/"+style+".json")
|
||||
styles, err := getDefaultStyle(style)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(jsonBytes, &tr.ansiOptions.Styles)
|
||||
tr.ansiOptions.Styles = *styles
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,13 +106,19 @@ func WithStandardStyle(style string) TermRendererOption {
|
||||
func WithStylePath(stylePath string) TermRendererOption {
|
||||
return func(tr *TermRenderer) error {
|
||||
jsonBytes, err := ioutil.ReadFile(stylePath)
|
||||
if os.IsNotExist(err) {
|
||||
jsonBytes, err = fs.ReadFile(statikFS, "/"+stylePath+".json")
|
||||
}
|
||||
if err != nil {
|
||||
switch {
|
||||
case err == nil:
|
||||
return json.Unmarshal(jsonBytes, &tr.ansiOptions.Styles)
|
||||
case os.IsNotExist(err):
|
||||
styles, err := getDefaultStyle(stylePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tr.ansiOptions.Styles = *styles
|
||||
return nil
|
||||
default:
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(jsonBytes, &tr.ansiOptions.Styles)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,3 +189,11 @@ func (tr *TermRenderer) RenderBytes(in []byte) ([]byte, error) {
|
||||
err := tr.md.Convert(in, &buf)
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
func getDefaultStyle(style string) (*ansi.StyleConfig, error) {
|
||||
styles, ok := DefaultStyles[style]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s: style not found", style)
|
||||
}
|
||||
return styles, nil
|
||||
}
|
||||
|
||||
@@ -9,6 +9,5 @@ require (
|
||||
github.com/microcosm-cc/bluemonday v1.0.2
|
||||
github.com/muesli/reflow v0.0.0-20191216070243-e5efeac4e302
|
||||
github.com/olekukonko/tablewriter v0.0.4
|
||||
github.com/rakyll/statik v0.1.6
|
||||
github.com/yuin/goldmark v1.1.19
|
||||
)
|
||||
|
||||
@@ -47,8 +47,6 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
|
||||
github.com/pkg/errors v0.8.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/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs=
|
||||
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
|
||||
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user