func renderContent(bf Settings, input []byte, outputFileName string, fileName string) { var renderer blackfriday.Renderer var templatewithtitle string if bf.latex { // render the data into LaTeX renderer = blackfriday.LatexRenderer(0) } else { // render the data into HTML title := DEFAULT_TITLE if bf.htmlFlags|blackfriday.HTML_COMPLETE_PAGE == blackfriday.HTML_COMPLETE_PAGE { title = getTitle(input) } if bf.templateText != "" { title = getTitle(input) templatewithtitle = strings.Replace(bf.templateText, "{{title}}", title, -1) templatewithtitle = strings.Replace(templatewithtitle, "{{filename}}", fileName, -1) } renderer = blackfriday.HtmlRenderer(bf.htmlFlags, title, bf.css) } // parse and render var output []byte for i := 0; i < bf.repeat; i++ { output = blackfriday.Markdown(input, renderer, bf.extensions) } if templatewithtitle != "" { output = []byte(strings.Replace(templatewithtitle, "{{content}}", string(output[:]), -1)) } // output the result var out *os.File var err error if outputFileName != "" { if out, err = os.Create(outputFileName); err != nil { fmt.Fprintf(os.Stderr, "Error creating %s: %v", outputFileName, err) os.Exit(-1) } defer out.Close() } else { out = os.Stdout } if _, err = out.Write(output); err != nil { fmt.Fprintln(os.Stderr, "Error writing output:", err) os.Exit(-1) } }
func main() { // parse command-line options var page, toc, toconly, xhtml, latex, smartypants, latexdashes, fractions bool var css, cpuprofile string var repeat int flag.BoolVar(&page, "page", false, "Generate a standalone HTML page (implies -latex=false)") flag.BoolVar(&toc, "toc", false, "Generate a table of contents (implies -latex=false)") flag.BoolVar(&toconly, "toconly", false, "Generate a table of contents only (implies -toc)") flag.BoolVar(&xhtml, "xhtml", true, "Use XHTML-style tags in HTML output") flag.BoolVar(&latex, "latex", false, "Generate LaTeX output instead of HTML") flag.BoolVar(&smartypants, "smartypants", true, "Apply smartypants-style substitutions") flag.BoolVar(&latexdashes, "latexdashes", true, "Use LaTeX-style dash rules for smartypants") flag.BoolVar(&fractions, "fractions", true, "Use improved fraction rules for smartypants") flag.StringVar(&css, "css", "", "Link to a CSS stylesheet (implies -page)") flag.StringVar(&cpuprofile, "cpuprofile", "", "Write cpu profile to a file") flag.IntVar(&repeat, "repeat", 1, "Process the input multiple times (for benchmarking)") flag.Usage = func() { fmt.Fprintf(os.Stderr, "Blackfriday Markdown Processor v"+blackfriday.VERSION+ "\nAvailable at http://github.com/russross/blackfriday\n\n"+ "Copyright © 2011 Russ Ross <*****@*****.**>\n"+ "Distributed under the Simplified BSD License\n"+ "See website for details\n\n"+ "Usage:\n"+ " %s [options] [inputfile [outputfile]]\n\n"+ "Options:\n", os.Args[0]) flag.PrintDefaults() } flag.Parse() // enforce implied options if css != "" { page = true } if page { latex = false } if toconly { toc = true } if toc { latex = false } // turn on profiling? if cpuprofile != "" { f, err := os.Create(cpuprofile) if err != nil { fmt.Fprintln(os.Stderr, err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } // read the input var input []byte var err error args := flag.Args() switch len(args) { case 0: if input, err = ioutil.ReadAll(os.Stdin); err != nil { fmt.Fprintln(os.Stderr, "Error reading from Stdin:", err) os.Exit(-1) } case 1, 2: if input, err = ioutil.ReadFile(args[0]); err != nil { fmt.Fprintln(os.Stderr, "Error reading from", args[0], ":", err) os.Exit(-1) } default: flag.Usage() os.Exit(-1) } // set up options extensions := 0 extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS extensions |= blackfriday.EXTENSION_TABLES extensions |= blackfriday.EXTENSION_FENCED_CODE extensions |= blackfriday.EXTENSION_AUTOLINK extensions |= blackfriday.EXTENSION_STRIKETHROUGH extensions |= blackfriday.EXTENSION_SPACE_HEADERS var renderer blackfriday.Renderer if latex { // render the data into LaTeX renderer = blackfriday.LatexRenderer(0) } else { // render the data into HTML htmlFlags := 0 if xhtml { htmlFlags |= blackfriday.HTML_USE_XHTML } if smartypants { htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS } if fractions { htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS } if latexdashes { htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES } title := "" if page { htmlFlags |= blackfriday.HTML_COMPLETE_PAGE title = getTitle(input) } if toconly { htmlFlags |= blackfriday.HTML_OMIT_CONTENTS } if toc { htmlFlags |= blackfriday.HTML_TOC } renderer = blackfriday.HtmlRenderer(htmlFlags, title, css) } // parse and render var output []byte for i := 0; i < repeat; i++ { output = blackfriday.Markdown(input, renderer, extensions) } // output the result var out *os.File if len(args) == 2 { if out, err = os.Create(args[1]); err != nil { fmt.Fprintf(os.Stderr, "Error creating %s: %v", args[1], err) os.Exit(-1) } defer out.Close() } else { out = os.Stdout } if _, err = out.Write(output); err != nil { fmt.Fprintln(os.Stderr, "Error writing output:", err) os.Exit(-1) } }
func main() { // parse config file and command-line options if err := parseConfig(); err != nil { log.Printf("warn: problem parsing config: %v", err) } parseFlags() // enforce implied options if cfg.CSS != "" || cfg.Title != "" { cfg.Page = true } if cfg.Page { cfg.Latex = false } if cfg.TOCOnly { cfg.TOC = true } if cfg.TOC { cfg.Latex = false } // turn on profiling? if cfg.CPUProfile != "" { f, err := os.Create(cfg.CPUProfile) if err != nil { fmt.Fprintln(os.Stderr, err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } // read the input var input []byte var err error args := flag.Args() switch len(args) { case 0: if input, err = ioutil.ReadAll(os.Stdin); err != nil { fmt.Fprintln(os.Stderr, "Error reading from Stdin:", err) os.Exit(-1) } case 1, 2: if input, err = ioutil.ReadFile(args[0]); err != nil { fmt.Fprintln(os.Stderr, "Error reading from", args[0], ":", err) os.Exit(-1) } // Use filename as title if there isn't one already if cfg.Title == "" { cfg.Title = args[0] } default: flag.Usage() os.Exit(-1) } // set up options extensions := 0 extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS extensions |= blackfriday.EXTENSION_TABLES extensions |= blackfriday.EXTENSION_FENCED_CODE extensions |= blackfriday.EXTENSION_AUTOLINK extensions |= blackfriday.EXTENSION_STRIKETHROUGH extensions |= blackfriday.EXTENSION_SPACE_HEADERS if cfg.Footnotes { extensions |= blackfriday.EXTENSION_FOOTNOTES } var renderer blackfriday.Renderer if cfg.Latex { // render the data into LaTeX renderer = blackfriday.LatexRenderer(0) } else { // render the data into HTML htmlFlags := 0 if cfg.XHTML { htmlFlags |= blackfriday.HTML_USE_XHTML } if cfg.Smartypants { htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS } if cfg.Fractions { htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS } if cfg.LatexDashes { htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES } if cfg.Page { htmlFlags |= blackfriday.HTML_COMPLETE_PAGE } if cfg.TOCOnly { htmlFlags |= blackfriday.HTML_OMIT_CONTENTS } if cfg.TOC { htmlFlags |= blackfriday.HTML_TOC } renderer = blackfriday.HtmlRenderer(htmlFlags, cfg.Title, cfg.CSS) } // parse and render var output []byte for i := 0; i < cfg.Repeat; i++ { output = blackfriday.Markdown(input, renderer, extensions) } // output the result var out *os.File if len(args) == 2 { if out, err = os.Create(args[1]); err != nil { fmt.Fprintf(os.Stderr, "Error creating %s: %v", args[1], err) os.Exit(-1) } defer out.Close() } else { out = os.Stdout } if _, err = out.Write(output); err != nil { fmt.Fprintln(os.Stderr, "Error writing output:", err) os.Exit(-1) } }