added simplified interface for common usage

This commit is contained in:
Russ Ross 2011-06-28 15:55:27 -06:00
parent fde2c60665
commit c969dff782
5 changed files with 77 additions and 26 deletions

View File

@ -23,9 +23,25 @@ Assuming you have recent version of Go installed, along with git:
will download, compile, and install the package into
`$GOROOT/src/pkg/github.com/russross/blackfriday`.
Check out `example/main.go` for an example of how to use it. Run
`gomake` in that directory to build a simple command-line markdown
tool:
For basic usage, it is as simple as getting your input into a byte
slice and calling:
output := blackfriday.MarkdownBasic(input)
This renders it with no extensions enabled. To get a more useful
feature set, use this instead:
output := blackfriday.MarkdownCommon(input)
If you want to customize the set of options, first get a renderer
(currently either the HTML or LaTeX output engines), then use it to
call the more general `Markdown` function. For examples, see the
implementations of `MarkdownBasic` and `MarkdownCommon` in
`markdown.go`.
You can also check out `example/main.go` for a more complete example
of how to use it. Run `gomake` in that directory to build a simple
command-line markdown tool:
cd $GOROOT/src/pkg/github.com/russross/blackfriday/example
gomake

View File

@ -17,16 +17,16 @@ import (
"testing"
)
func runMarkdownBlock(input string, extensions uint32) string {
html_flags := 0
html_flags |= HTML_USE_XHTML
func runMarkdownBlock(input string, extensions int) string {
htmlFlags := 0
htmlFlags |= HTML_USE_XHTML
renderer := HtmlRenderer(html_flags)
renderer := HtmlRenderer(htmlFlags)
return string(Markdown([]byte(input), renderer, extensions))
}
func doTestsBlock(t *testing.T, tests []string, extensions uint32) {
func doTestsBlock(t *testing.T, tests []string, extensions int) {
for i := 0; i+1 < len(tests); i += 2 {
input := tests[i]
expected := tests[i+1]

View File

@ -56,7 +56,8 @@ func main() {
"See website for details\n\n"+
"Usage:\n"+
" %s [options] [inputfile [outputfile]]\n\n"+
"Options:\n",os.Args[0])
"Options:\n",
os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
@ -100,7 +101,7 @@ func main() {
}
// set up options
var extensions uint32
extensions := 0
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
extensions |= blackfriday.EXTENSION_TABLES
extensions |= blackfriday.EXTENSION_FENCED_CODE
@ -114,20 +115,20 @@ func main() {
renderer = blackfriday.LatexRenderer(0)
} else {
// render the data into HTML
html_flags := 0
htmlFlags := 0
if xhtml {
html_flags |= blackfriday.HTML_USE_XHTML
htmlFlags |= blackfriday.HTML_USE_XHTML
}
if smartypants {
html_flags |= blackfriday.HTML_USE_SMARTYPANTS
htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
}
if fractions {
html_flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
}
if latexdashes {
html_flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
}
renderer = blackfriday.HtmlRenderer(html_flags)
renderer = blackfriday.HtmlRenderer(htmlFlags)
}
// parse and render

View File

@ -18,14 +18,14 @@ import (
)
func runMarkdownInline(input string) string {
var extensions uint32
extensions := 0
extensions |= EXTENSION_AUTOLINK
extensions |= EXTENSION_STRIKETHROUGH
html_flags := 0
html_flags |= HTML_USE_XHTML
htmlFlags := 0
htmlFlags |= HTML_USE_XHTML
renderer := HtmlRenderer(html_flags)
renderer := HtmlRenderer(htmlFlags)
return string(Markdown([]byte(input), renderer, extensions))
}

View File

@ -149,7 +149,7 @@ type render struct {
mk *Renderer
refs map[string]*reference
inline [256]inlineParser
flags uint32
flags int
nesting int
maxNesting int
insideLink bool
@ -162,10 +162,44 @@ type render struct {
//
//
// Call Markdown with no extensions
func MarkdownBasic(input []byte) []byte {
// set up the HTML renderer
htmlFlags := HTML_USE_XHTML
renderer := HtmlRenderer(htmlFlags)
// set up the parser
extensions := 0
return Markdown(input, renderer, extensions)
}
// Call Markdown with most useful extensions enabled
func MarkdownCommon(input []byte) []byte {
// set up the HTML renderer
htmlFlags := 0
htmlFlags |= HTML_USE_XHTML
htmlFlags |= HTML_USE_SMARTYPANTS
htmlFlags |= HTML_SMARTYPANTS_FRACTIONS
htmlFlags |= HTML_SMARTYPANTS_LATEX_DASHES
renderer := HtmlRenderer(htmlFlags)
// set up the parser
extensions := 0
extensions |= EXTENSION_NO_INTRA_EMPHASIS
extensions |= EXTENSION_TABLES
extensions |= EXTENSION_FENCED_CODE
extensions |= EXTENSION_AUTOLINK
extensions |= EXTENSION_STRIKETHROUGH
extensions |= EXTENSION_SPACE_HEADERS
return Markdown(input, renderer, extensions)
}
// 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(input []byte, renderer *Renderer, extensions uint32) []byte {
func Markdown(input []byte, renderer *Renderer, extensions int) []byte {
// no point in parsing if we can't render
if renderer == nil {
return nil
@ -204,8 +238,8 @@ func Markdown(input []byte, renderer *Renderer, extensions uint32) []byte {
rndr.inline[':'] = inlineAutoLink
}
first := FirstPass(rndr, input)
second := SecondPass(rndr, first)
first := firstPass(rndr, input)
second := secondPass(rndr, first)
return second
}
@ -215,7 +249,7 @@ func Markdown(input []byte, renderer *Renderer, extensions uint32) []byte {
// - expand tabs
// - normalize newlines
// - copy everything else
func FirstPass(rndr *render, input []byte) []byte {
func firstPass(rndr *render, input []byte) []byte {
var out bytes.Buffer
tab_size := TAB_SIZE_DEFAULT
if rndr.flags&EXTENSION_TAB_SIZE_EIGHT != 0 {
@ -255,7 +289,7 @@ func FirstPass(rndr *render, input []byte) []byte {
}
// second pass: actual rendering
func SecondPass(rndr *render, input []byte) []byte {
func secondPass(rndr *render, input []byte) []byte {
var output bytes.Buffer
if rndr.mk.DocumentHeader != nil {
rndr.mk.DocumentHeader(&output, rndr.mk.Opaque)