From 59dc1f8599d7fa0c0328a1bf5704a3f0ca397de6 Mon Sep 17 00:00:00 2001 From: Russ Ross Date: Sat, 28 May 2011 22:50:33 -0600 Subject: [PATCH] fix smartypants and html entity escaping --- example/main.go | 4 ++-- markdown.go | 4 ++-- smartypants.go | 12 ++++++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/example/main.go b/example/main.go index f5e1261..4d1a375 100644 --- a/example/main.go +++ b/example/main.go @@ -15,7 +15,7 @@ package main import ( "fmt" "io/ioutil" - "github.com/russross/blackfriday" + "github.com/russross/blackfriday" "os" ) @@ -54,7 +54,7 @@ func main() { html_flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS html_flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES - // render the data + // render the data output := blackfriday.Markdown(input, blackfriday.HtmlRenderer(html_flags), extensions) // output the result diff --git a/markdown.go b/markdown.go index 046462f..c20a0a9 100644 --- a/markdown.go +++ b/markdown.go @@ -255,7 +255,7 @@ func Markdown(input []byte, renderer *Renderer, extensions uint32) []byte { } // second pass: actual rendering - output := bytes.NewBuffer(nil) + output := bytes.NewBuffer(nil) if rndr.mk.doc_header != nil { rndr.mk.doc_header(output, rndr.mk.opaque) } @@ -277,7 +277,7 @@ func Markdown(input []byte, renderer *Renderer, extensions uint32) []byte { panic("Nesting level did not end at zero") } - return output.Bytes() + return output.Bytes() } diff --git a/smartypants.go b/smartypants.go index a9f282a..7339bdb 100644 --- a/smartypants.go +++ b/smartypants.go @@ -218,9 +218,11 @@ func smartypants_cb__number_generic(ob *bytes.Buffer, smrt *smartypants_data, pr num_end++ } if num_end == 0 { + ob.WriteByte(text[0]) return 0 } if len(text) < num_end+2 || text[num_end] != '/' { + ob.WriteByte(text[0]) return 0 } den_end := num_end + 1 @@ -228,6 +230,7 @@ func smartypants_cb__number_generic(ob *bytes.Buffer, smrt *smartypants_data, pr den_end++ } if den_end == num_end+1 { + ob.WriteByte(text[0]) return 0 } if len(text) == den_end || word_boundary(text[den_end]) { @@ -328,11 +331,16 @@ func rndr_smartypants(ob *bytes.Buffer, text []byte, opaque interface{}) { options := opaque.(*htmlOptions) smrt := smartypants_data{false, false} + // first do normal entity escaping + escaped := bytes.NewBuffer(nil) + attr_escape(escaped, text) + text = escaped.Bytes() + mark := 0 for i := 0; i < len(text); i++ { if action := options.smartypants[text[i]]; action != nil { if i > mark { - attr_escape(ob, text[mark:i]) + ob.Write(text[mark:i]) } previous_char := byte(0) @@ -345,6 +353,6 @@ func rndr_smartypants(ob *bytes.Buffer, text []byte, opaque interface{}) { } if mark < len(text) { - attr_escape(ob, text[mark:]) + ob.Write(text[mark:]) } }