all repos — grayfriday @ 9f1d6199060129cf6c3ff4a530ab0f68825a6bfa

blackfriday fork with a few changes

example/main.go (view raw)

 1//
 2// Black Friday Markdown Processor
 3// Originally based on http://github.com/tanoku/upskirt
 4// by Russ Ross <russ@russross.com>
 5//
 6
 7//
 8//
 9// Example front-end for command-line use
10//
11//
12
13package main
14
15import (
16	"fmt"
17	"io/ioutil"
18	"github.com/russross/blackfriday"
19	"os"
20)
21
22func main() {
23	// read the input
24	var input []byte
25	var err os.Error
26	switch len(os.Args) {
27	case 1:
28		if input, err = ioutil.ReadAll(os.Stdin); err != nil {
29			fmt.Fprintln(os.Stderr, "Error reading from Stdin:", err)
30			os.Exit(-1)
31		}
32	case 2, 3:
33		if input, err = ioutil.ReadFile(os.Args[1]); err != nil {
34			fmt.Fprintln(os.Stderr, "Error reading from", os.Args[1], ":", err)
35			os.Exit(-1)
36		}
37	default:
38		fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "[inputfile [outputfile]]")
39		os.Exit(-1)
40	}
41
42	// set up options
43	var extensions uint32
44	extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
45	extensions |= blackfriday.EXTENSION_TABLES
46	extensions |= blackfriday.EXTENSION_FENCED_CODE
47	extensions |= blackfriday.EXTENSION_AUTOLINK
48	extensions |= blackfriday.EXTENSION_STRIKETHROUGH
49	extensions |= blackfriday.EXTENSION_SPACE_HEADERS
50
51	html_flags := 0
52	html_flags |= blackfriday.HTML_USE_XHTML
53	// note: uncomment the following line to enable smartypants
54	// it is commented out by default so that markdown
55	// compatibility tests pass
56	//html_flags |= blackfriday.HTML_USE_SMARTYPANTS
57	html_flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
58	html_flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
59
60	// render the data into HTML (comment this out to deselect HTML)
61    renderer := blackfriday.HtmlRenderer(html_flags)
62
63    // render the data into LaTeX (uncomment to select LaTeX)
64    //renderer := blackfriday.LatexRenderer(0)
65
66	output := blackfriday.Markdown(input, renderer, extensions)
67
68	// output the result
69	if len(os.Args) == 3 {
70		if err = ioutil.WriteFile(os.Args[2], output, 0644); err != nil {
71			fmt.Fprintln(os.Stderr, "Error writing to", os.Args[2], ":", err)
72			os.Exit(-1)
73		}
74	} else {
75		if _, err = os.Stdout.Write(output); err != nil {
76			fmt.Fprintln(os.Stderr, "Error writing to Stdout:", err)
77			os.Exit(-1)
78		}
79	}
80}