all repos — grayfriday @ de40da7ad692f7fb0c81bbc68ad49d27217c49f7

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	html_flags |= blackfriday.HTML_USE_SMARTYPANTS
54	html_flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
55	html_flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
56
57    // render the data
58	output := blackfriday.Markdown(input, blackfriday.HtmlRenderer(html_flags), extensions)
59
60	// output the result
61	if len(os.Args) == 3 {
62		if err = ioutil.WriteFile(os.Args[2], output, 0644); err != nil {
63			fmt.Fprintln(os.Stderr, "Error writing to", os.Args[2], ":", err)
64			os.Exit(-1)
65		}
66	} else {
67		if _, err = os.Stdout.Write(output); err != nil {
68			fmt.Fprintln(os.Stderr, "Error writing to Stdout:", err)
69			os.Exit(-1)
70		}
71	}
72}