all repos — grayfriday @ 9a11e9663315629a72612dd4bd351b48001db90d

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	"bytes"
17	"fmt"
18	"io/ioutil"
19    "github.com/russross/blackfriday"
20	"os"
21)
22
23func main() {
24	// read the input
25	var input []byte
26	var err os.Error
27	switch len(os.Args) {
28	case 1:
29		if input, err = ioutil.ReadAll(os.Stdin); err != nil {
30			fmt.Fprintln(os.Stderr, "Error reading from Stdin:", err)
31			os.Exit(-1)
32		}
33	case 2, 3:
34		if input, err = ioutil.ReadFile(os.Args[1]); err != nil {
35			fmt.Fprintln(os.Stderr, "Error reading from", os.Args[1], ":", err)
36			os.Exit(-1)
37		}
38	default:
39		fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "[inputfile [outputfile]]")
40		os.Exit(-1)
41	}
42
43	// set up options
44	output := bytes.NewBuffer(nil)
45	var extensions uint32
46	extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
47	extensions |= blackfriday.EXTENSION_TABLES
48	extensions |= blackfriday.EXTENSION_FENCED_CODE
49	extensions |= blackfriday.EXTENSION_AUTOLINK
50	extensions |= blackfriday.EXTENSION_STRIKETHROUGH
51	extensions |= blackfriday.EXTENSION_SPACE_HEADERS
52
53	html_flags := 0
54	html_flags |= blackfriday.HTML_USE_XHTML
55	html_flags |= blackfriday.HTML_USE_SMARTYPANTS
56	html_flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
57	html_flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
58
59    // render the data
60	blackfriday.Markdown(output, input, blackfriday.HtmlRenderer(html_flags), extensions)
61
62	// output the result
63	if len(os.Args) == 3 {
64		if err = ioutil.WriteFile(os.Args[2], output.Bytes(), 0644); err != nil {
65			fmt.Fprintln(os.Stderr, "Error writing to", os.Args[2], ":", err)
66			os.Exit(-1)
67		}
68	} else {
69		if _, err = os.Stdout.Write(output.Bytes()); err != nil {
70			fmt.Fprintln(os.Stderr, "Error writing to Stdout:", err)
71			os.Exit(-1)
72		}
73	}
74}