all repos — grayfriday @ 2f839dc39a59b7b94546e780af7322b0db3598c8

blackfriday fork with a few changes

example/main.go (view raw)

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