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 "flag"
17 "fmt"
18 "io/ioutil"
19 "github.com/russross/blackfriday"
20 "os"
21 "runtime/pprof"
22)
23
24func main() {
25 // parse command-line options
26 var page, xhtml, latex, smartypants bool
27 var css, cpuprofile string
28 var repeat int
29 flag.BoolVar(&page, "page", false,
30 "Generate a standalone HTML page (implies -latex=false)")
31 flag.BoolVar(&xhtml, "xhtml", true,
32 "Use XHTML-style tags in HTML output")
33 flag.BoolVar(&latex, "latex", false,
34 "Generate LaTeX output instead of HTML")
35 flag.BoolVar(&smartypants, "smartypants", false,
36 "Apply smartypants-style substitutions")
37 flag.StringVar(&css, "css", "",
38 "Link to a CSS stylesheet (implies -page)")
39 flag.StringVar(&cpuprofile, "cpuprofile", "",
40 "Write cpu profile to a file")
41 flag.IntVar(&repeat, "repeat", 1,
42 "Process the input multiple times (for benchmarking)")
43 flag.Usage = func() {
44 fmt.Fprintf(os.Stderr, "Usage:\n"+
45 " %s [options] [inputfile [outputfile]]\n\n"+
46 "Options:\n", os.Args[0])
47 flag.PrintDefaults()
48 }
49 flag.Parse()
50
51 // enforce implied options
52 if css != "" {
53 page = true
54 }
55 if page {
56 latex = false
57 }
58
59 // turn on profiling?
60 if cpuprofile != "" {
61 f, err := os.Create(cpuprofile)
62 if err != nil {
63 fmt.Fprintln(os.Stderr, err)
64 }
65 pprof.StartCPUProfile(f)
66 defer pprof.StopCPUProfile()
67 }
68
69 // read the input
70 var input []byte
71 var err os.Error
72 args := flag.Args()
73 switch len(args) {
74 case 0:
75 if input, err = ioutil.ReadAll(os.Stdin); err != nil {
76 fmt.Fprintln(os.Stderr, "Error reading from Stdin:", err)
77 os.Exit(-1)
78 }
79 case 1, 2:
80 if input, err = ioutil.ReadFile(args[0]); err != nil {
81 fmt.Fprintln(os.Stderr, "Error reading from", args[0], ":", err)
82 os.Exit(-1)
83 }
84 default:
85 flag.Usage()
86 os.Exit(-1)
87 }
88
89 // set up options
90 var extensions uint32
91 extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
92 extensions |= blackfriday.EXTENSION_TABLES
93 extensions |= blackfriday.EXTENSION_FENCED_CODE
94 extensions |= blackfriday.EXTENSION_AUTOLINK
95 extensions |= blackfriday.EXTENSION_STRIKETHROUGH
96 extensions |= blackfriday.EXTENSION_SPACE_HEADERS
97
98 var renderer *blackfriday.Renderer
99 if latex {
100 // render the data into LaTeX
101 renderer = blackfriday.LatexRenderer(0)
102 } else {
103 // render the data into HTML
104 html_flags := 0
105 if xhtml {
106 html_flags |= blackfriday.HTML_USE_XHTML
107 }
108 if smartypants {
109 html_flags |= blackfriday.HTML_USE_SMARTYPANTS
110 html_flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
111 html_flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
112 }
113 renderer = blackfriday.HtmlRenderer(html_flags)
114 }
115
116 // parse and render
117 var output []byte
118 for i := 0; i < repeat; i++ {
119 output = blackfriday.Markdown(input, renderer, extensions)
120 }
121
122 // output the result
123 var out *os.File
124 if len(args) == 2 {
125 if out, err = os.Create(args[1]); err != nil {
126 fmt.Fprintf(os.Stderr, "Error creating %s: %v", args[1], err)
127 os.Exit(-1)
128 }
129 defer out.Close()
130 } else {
131 out = os.Stdout
132 }
133
134 if page {
135 ending := ""
136 if xhtml {
137 fmt.Fprint(out, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" ")
138 fmt.Fprintln(out, "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")
139 fmt.Fprintln(out, "<html xmlns=\"http://www.w3.org/1999/xhtml\">")
140 ending = " /"
141 } else {
142 fmt.Fprint(out, "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" ")
143 fmt.Fprintln(out, "\"http://www.w3.org/TR/html4/strict.dtd\">")
144 fmt.Fprintln(out, "<html>")
145 }
146 fmt.Fprintln(out, "<head>")
147 fmt.Fprintln(out, " <title></title>")
148 fmt.Fprintf(out, " <meta name=\"GENERATOR\" content=\"Blackfriday markdown processor\"%s>\n", ending)
149 fmt.Fprintf(out, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"%s>\n", ending)
150 if css != "" {
151 fmt.Fprintf(out, " <link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n", css)
152 }
153 fmt.Fprintln(out, "</head>")
154 fmt.Fprintln(out, "<body>")
155 }
156 if _, err = out.Write(output); err != nil {
157 fmt.Fprintln(os.Stderr, "Error writing output:", err)
158 os.Exit(-1)
159 }
160 if page {
161 fmt.Fprintln(out, "</body>")
162 fmt.Fprintln(out, "</html>")
163 }
164}