Esempio n. 1
0
File: main.go Progetto: a8m/mark-cli
func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, fmt.Sprintf(usage))
	}
	flag.Parse()
	// Reader
	var reader *bufio.Reader
	if *input != "" {
		file, err := os.Open(*input)
		if err != nil {
			usageAndExit(fmt.Sprintf("Error to open file input: %s.", *input))
		}
		defer file.Close()
		reader = bufio.NewReader(file)
	} else {
		if stat, err := os.Stdin.Stat(); err == nil && stat.Size() > 0 {
			reader = bufio.NewReader(os.Stdin)
		} else {
			usageAndExit("")
		}
	}
	// Collect data
	var data string
	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			usageAndExit("Failed to reading input.")
		}
		data += line
	}
	// Writer
	var file *os.File
	var err error
	if *output != "" {
		file, err = os.Create(*output)
		if err != nil {
			usageAndExit("Error to create the wanted output file.")
		}
	} else {
		file = os.Stdout
	}
	// Mark rendering
	opts := mark.DefaultOptions()
	opts.Smartypants = *smarty
	opts.Fractions = *fractions
	m := mark.New(data, opts)
	if _, err := file.WriteString(m.Render()); err != nil {
		filename := *output
		if filename == "" {
			filename = "STDOUT"
		}
		usageAndExit(fmt.Sprintf("Error writing output to: %s.", filename))
	}
}
Esempio n. 2
0
//HTML returns body text as html.
func (p *Page) HTML() template.HTML {
	b, _ := ioutil.ReadAll(p.Body)
	return template.HTML(mark.New(string(b), mark.DefaultOptions()).Render())
}