diff --git a/README.md b/README.md index dde04e4..71ffec3 100644 --- a/README.md +++ b/README.md @@ -4,30 +4,41 @@ Render markdown on the CLI, with _pizzazz_! ## Usage -Supply a JSON stylesheet with the `-s` flag. Use a markdown source as the argument. +Use a markdown source as the argument: Read from file: ``` -./gold -s dark.json README.md +./gold README.md ``` Read from stdin: ``` -./gold -s dark.json - +./gold - ``` Fetch README from GitHub: ``` -./gold -s dark.json github.com/charmbracelet/gold +./gold github.com/charmbracelet/gold ``` Fetch markdown from an HTTP source: ``` -./gold -s dark.json https://host.tld/file.md +./gold https://host.tld/file.md ``` -When `gold` is started without any markdown source, it will try to find a `README.md` -or `README` file in the current working directory. +When `gold` is started without any markdown source, it will try to find a +`README.md` or `README` file in the current working directory. + +You can supply a JSON stylesheet with the `-s` flag: +``` +./gold -s mystyle.json +``` + +Style definitions located in `styles/` can be embedded into the binary by +running [statik](https://github.com/rakyll/statik). +``` +statik -f -src styles +``` ## Colors @@ -35,4 +46,4 @@ Currently `gold` uses the [Aurora ANSI colors](https://godoc.org/github.com/logr ## Example Output -![Gold Dark Theme](https://github.com/charmbracelet/gold/raw/master/cmd/gold/gold_dark.png) +![Gold Dark Style](https://github.com/charmbracelet/gold/raw/master/cmd/gold/gold_dark.png) diff --git a/main.go b/main.go index 3ec8b6d..c282da7 100644 --- a/main.go +++ b/main.go @@ -12,9 +12,11 @@ import ( "github.com/mattn/go-isatty" "github.com/muesli/go-wordwrap" + "github.com/rakyll/statik/fs" "github.com/spf13/cobra" "github.com/charmbracelet/gold" + _ "github.com/charmbracelet/gold/cmd/gold/statik" ) var ( @@ -91,6 +93,27 @@ func readerFromArg(s string) (*Source, error) { return &Source{r, u}, err } +func loadStyle(f string) ([]byte, error) { + var r io.ReadCloser + var err error + + r, err = os.Open(f) + if err != nil { + statikFS, err := fs.New() + if err != nil { + return nil, err + } + + r, err = statikFS.Open("/" + f + ".json") + if err != nil { + return nil, err + } + } + + defer r.Close() + return ioutil.ReadAll(r) +} + func execute(cmd *cobra.Command, args []string) error { var arg string if len(args) > 0 { @@ -105,7 +128,12 @@ func execute(cmd *cobra.Command, args []string) error { r := gold.NewPlainTermRenderer() if isatty.IsTerminal(os.Stdout.Fd()) { - r, err = gold.NewTermRenderer(style) + json, err := loadStyle(style) + if err != nil { + return err + } + + r, err = gold.NewTermRendererFromBytes(json) if err != nil { return err } @@ -129,6 +157,6 @@ func main() { } func init() { - rootCmd.Flags().StringVarP(&style, "style", "s", "dark.json", "style JSON path") + rootCmd.Flags().StringVarP(&style, "style", "s", "dark", "style name or JSON path") rootCmd.Flags().UintVarP(&width, "width", "w", 100, "word-wrap at width") } diff --git a/statik/statik.go b/statik/statik.go new file mode 100644 index 0000000..8e70a85 --- /dev/null +++ b/statik/statik.go @@ -0,0 +1,12 @@ +// Code generated by statik. DO NOT EDIT. + +package statik + +import ( + "github.com/rakyll/statik/fs" +) + +func init() { + data := "PK\x03\x04\x14\x00\x08\x00\x08\x00\xa9\xacyO\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00 \x00dark.jsonUT\x05\x00\x01\x8fI\xdc]|\x8f\xdd\n\x830\x0cF\xef\xfb\x14!\xd7\x0e\x9c\xde\x0c_Fj\x1b\xb4\x18\x9bQ+\x8c\x0d\xdf}\x94\xfdt\x13\xb7\xbb\x10\xce9\xf0\xdd\x14\x00\xb2\xf3#6\x90n\x004\xc2\x12\xb0\x01