From 3ee2b137f8d5ffbd8675296a3001be5ba830fb58 Mon Sep 17 00:00:00 2001 From: Russ Ross Date: Sat, 28 May 2011 22:37:12 -0600 Subject: [PATCH] return result instead of taking buffer as input --- example/main.go | 8 +++----- markdown.go | 7 +++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/example/main.go b/example/main.go index 14783c5..f5e1261 100644 --- a/example/main.go +++ b/example/main.go @@ -13,7 +13,6 @@ package main import ( - "bytes" "fmt" "io/ioutil" "github.com/russross/blackfriday" @@ -41,7 +40,6 @@ func main() { } // set up options - output := bytes.NewBuffer(nil) var extensions uint32 extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS extensions |= blackfriday.EXTENSION_TABLES @@ -57,16 +55,16 @@ func main() { html_flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES // render the data - blackfriday.Markdown(output, input, blackfriday.HtmlRenderer(html_flags), extensions) + output := blackfriday.Markdown(input, blackfriday.HtmlRenderer(html_flags), extensions) // output the result if len(os.Args) == 3 { - if err = ioutil.WriteFile(os.Args[2], output.Bytes(), 0644); err != nil { + if err = ioutil.WriteFile(os.Args[2], output, 0644); err != nil { fmt.Fprintln(os.Stderr, "Error writing to", os.Args[2], ":", err) os.Exit(-1) } } else { - if _, err = os.Stdout.Write(output.Bytes()); err != nil { + if _, err = os.Stdout.Write(output); err != nil { fmt.Fprintln(os.Stderr, "Error writing to Stdout:", err) os.Exit(-1) } diff --git a/markdown.go b/markdown.go index 1b752cf..046462f 100644 --- a/markdown.go +++ b/markdown.go @@ -166,10 +166,10 @@ const ( // Parse and render a block of markdown-encoded text. // The renderer is used to format the output, and extensions dictates which // non-standard extensions are enabled. -func Markdown(output *bytes.Buffer, input []byte, renderer *Renderer, extensions uint32) { +func Markdown(input []byte, renderer *Renderer, extensions uint32) []byte { // no point in parsing if we can't render if renderer == nil { - return + return nil } // fill in the character-level parsers @@ -255,6 +255,7 @@ func Markdown(output *bytes.Buffer, input []byte, renderer *Renderer, extensions } // second pass: actual rendering + output := bytes.NewBuffer(nil) if rndr.mk.doc_header != nil { rndr.mk.doc_header(output, rndr.mk.opaque) } @@ -275,6 +276,8 @@ func Markdown(output *bytes.Buffer, input []byte, renderer *Renderer, extensions if rndr.nesting != 0 { panic("Nesting level did not end at zero") } + + return output.Bytes() }