diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 3c6f407..5b14a54 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -6,6 +6,7 @@ import ( "github.com/asciimoo/wuzz/config" "github.com/nwidger/jsoncolor" + "github.com/x86kernel/htmlcolor" ) func TestFormat(t *testing.T) { @@ -17,18 +18,22 @@ func TestFormat(t *testing.T) { var htmlBuffer bytes.Buffer New(configFixture(true), "text/html; charset=utf-8").Format(&htmlBuffer, []byte("unfomatted")) - if htmlBuffer.String() != "unfomatted" { + var htmltargetBuffer bytes.Buffer + htmlcolor.NewFormatter().Format(&htmltargetBuffer, []byte("unfomatted")) + htmltarget := htmltargetBuffer.String() + + if htmlBuffer.String() != htmltarget { t.Error("Expected html to eq " + htmlBuffer.String()) } var jsonEnabledBuffer bytes.Buffer New(configFixture(true), "application/json; charset=utf-8").Format(&jsonEnabledBuffer, []byte("{\"json\": \"some value\"}")) - var targetBuffer bytes.Buffer - jsoncolor.NewFormatter().Format(&targetBuffer, []byte("{\"json\": \"some value\"}")) - target := targetBuffer.String() + var jsontargetBuffer bytes.Buffer + jsoncolor.NewFormatter().Format(&jsontargetBuffer, []byte("{\"json\": \"some value\"}")) + jsontarget := jsontargetBuffer.String() - if jsonEnabledBuffer.String() != target { - t.Error("Expected json to eq \n" + jsonEnabledBuffer.String() + "\nbut not\n" + target) + if jsonEnabledBuffer.String() != jsontarget { + t.Error("Expected json to eq \n" + jsonEnabledBuffer.String() + "\nbut not\n" + jsontarget) } var jsonDisabledBuffer bytes.Buffer diff --git a/formatter/html.go b/formatter/html.go index 7a4ad4d..fa9bb94 100644 --- a/formatter/html.go +++ b/formatter/html.go @@ -1,9 +1,30 @@ package formatter +import ( + "bytes" + "errors" + "io" + + "github.com/x86kernel/htmlcolor" +) + type htmlFormatter struct { textFormatter } +func (f *htmlFormatter) Format(writer io.Writer, data []byte) error { + htmlFormatter := htmlcolor.NewFormatter() + buf := bytes.NewBuffer(make([]byte, 0, len(data))) + err := htmlFormatter.Format(buf, data) + + if err == io.EOF { + writer.Write(buf.Bytes()) + return nil + } + + return errors.New("html formatter error") +} + func (f *htmlFormatter) Title() string { return "[html]" }