Skip to content

Commit

Permalink
Htmlize
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Nov 27, 2015
1 parent a38b12f commit 9c6913a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
8 changes: 8 additions & 0 deletions ccat.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func (p PlainTextPrinter) Print(r io.Reader, w io.Writer) error {
return err
}

type HtmlPrinter struct {
ColorPalettes ColorPalettes
}

func (c HtmlPrinter) Print(r io.Reader, w io.Writer) error {
return HtmlPrint(r, w, c.ColorPalettes)
}

func CCat(fname string, p CCatPrinter, w io.Writer) error {
var r io.Reader

Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ccatCmd struct {
BG string
Color string
ColorCodes mapValue
HTML bool
ShowPalette bool
ShowVersion bool
}
Expand Down Expand Up @@ -61,7 +62,9 @@ Value of color can be %s
}

var printer CCatPrinter
if c.Color == "always" {
if c.HTML {
printer = HtmlPrinter{colorPalettes}
} else if c.Color == "always" {
printer = ColorPrinter{colorPalettes}
} else if c.Color == "never" {
printer = PlainTextPrinter{}
Expand Down Expand Up @@ -91,6 +94,7 @@ func main() {
Long: "Colorize FILE(s), or standard input, to standard output.",
Example: `$ ccat FILE1 FILE2 ...
$ ccat --bg=dark FILE1 FILE2 ... # dark background
$ ccat --html # output html
$ ccat -G String="_darkblue_" -G Plaintext="darkred" FILE # set color codes
$ ccat --palette # show palette
$ ccat # read from standard input
Expand All @@ -116,6 +120,7 @@ Examples:
rootCmd.PersistentFlags().StringVarP(&ccatCmd.BG, "bg", "", "light", `set to "light" or "dark" depending on the terminal's background`)
rootCmd.PersistentFlags().StringVarP(&ccatCmd.Color, "color", "C", "auto", `colorize the output; value can be "never", "always" or "auto"`)
rootCmd.PersistentFlags().VarP(&ccatCmd.ColorCodes, "color-code", "G", `set color codes`)
rootCmd.PersistentFlags().BoolVarP(&ccatCmd.HTML, "html", "", false, `output html`)
rootCmd.PersistentFlags().BoolVarP(&ccatCmd.ShowPalette, "palette", "", false, `show color palettes`)
rootCmd.PersistentFlags().BoolVarP(&ccatCmd.ShowVersion, "version", "v", false, `show version`)

Expand Down
36 changes: 36 additions & 0 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"io"
"sort"
"strings"

"github.com/jingweno/ccat/Godeps/_workspace/src/github.com/sourcegraph/syntaxhighlight"
Expand Down Expand Up @@ -143,3 +144,38 @@ func (p Printer) Print(w io.Writer, kind syntaxhighlight.Kind, tokText string) e

return err
}

func HtmlPrint(r io.Reader, w io.Writer, palettes ColorPalettes) error {
keys := []string{}
for k := range htmlCodes {
keys = append(keys, k)
}
sort.Strings(keys)
w.Write([]byte("<style>\n"))
for _, s := range keys {
if s == "" {
continue
}
w.Write([]byte(fmt.Sprintf(".%s { color: %s; }\n", s, s)))
}
w.Write([]byte("</style>\n"))
w.Write([]byte("<pre>\n"))
err := syntaxhighlight.Print(syntaxhighlight.NewScannerReader(r), w, HtmlCodePrinter{palettes})
w.Write([]byte("\n</pre>\n"))
return err
}

type HtmlCodePrinter struct {
ColorPalettes ColorPalettes
}

func (p HtmlCodePrinter) Print(w io.Writer, kind syntaxhighlight.Kind, tokText string) error {
c := p.ColorPalettes.Get(kind)
if len(c) > 0 {
tokText = Htmlize(c, tokText)
}

_, err := io.WriteString(w, tokText)

return err
}
48 changes: 48 additions & 0 deletions printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,51 @@ func TestCPrint(t *testing.T) {
t.Errorf("output is wrong: %s", s)
}
}

func TestHtmlPrint(t *testing.T) {
r := bytes.NewBufferString("hello")
var w bytes.Buffer

err := HtmlPrint(r, &w, LightColorPalettes)
if err != nil {
t.Errorf("error should be nil, but it's %s", err)
}

expect := `<style>
.black { color: black; }
.blink { color: blink; }
.blue { color: blue; }
.bold { color: bold; }
.brown { color: brown; }
.darkblue { color: darkblue; }
.darkgray { color: darkgray; }
.darkgreen { color: darkgreen; }
.darkred { color: darkred; }
.darkteal { color: darkteal; }
.darkyellow { color: darkyellow; }
.faint { color: faint; }
.fuchsia { color: fuchsia; }
.fuscia { color: fuscia; }
.green { color: green; }
.lightgray { color: lightgray; }
.overline { color: overline; }
.purple { color: purple; }
.red { color: red; }
.reset { color: reset; }
.standout { color: standout; }
.teal { color: teal; }
.turquoise { color: turquoise; }
.underline { color: underline; }
.white { color: white; }
.yellow { color: yellow; }
</style>
<pre>
<span class="darkblue">hello</span>
</pre>
`

s := w.String()
if s != expect {
t.Errorf("output is wrong: %s", s)
}
}

0 comments on commit 9c6913a

Please sign in to comment.