all repos — grayfriday @ be0fb4602b8113ab16b70ab34fe48c0573bd65ef

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