Exemplo n.º 1
0
func (d *Document) RenderHTML(outDir string) {
	text := []byte(d.RawBody)
	d.HTMLBody = template.HTML(github_flavored_markdown.Markdown(text))
	base := strings.TrimSuffix(d.BaseName, filepath.Ext(d.InputFile))
	htmlFile := strings.Join([]string{base, "html"}, ".")
	htmlPath := filepath.Join(outDir, htmlFile)

	f, err := os.Create(htmlPath)
	if err != nil {
		log.Fatalf("Couldn't open '%s' : %s\n", htmlPath, err)
	}
	defer f.Close()

	baseTemplate := "views/base.html"
	t := assets.NewView(baseTemplate).GetTemplate()
	t, err = t.ParseFiles(baseTemplate)
	if err != nil {
		log.Fatalf("Template '%s' parse error: %s\n", baseTemplate, err)
	}
	err = t.Execute(f, d)
	if err != nil {
		log.Fatalf("Template render error: %s\n", err)
	}

	// alternative approach:
	// http://stackoverflow.com/questions/23124008/how-can-i-render-markdown-to-a-golang-templatehtml-or-tmpl-with-blackfriday
}
func ExampleMarkdown() {
	text := []byte("Hello world github/linguist#1 **cool**, and #1!")

	os.Stdout.Write(github_flavored_markdown.Markdown(text))

	// Output:
	// <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>
}
func ExampleHeaderLink() {
	text := []byte("### [Some **bold** _italic_ link](http://www.example.com)")

	os.Stdout.Write(github_flavored_markdown.Markdown(text))

	// Output:
	// <h3><a name="some-bold-italic-link" class="anchor" href="#some-bold-italic-link" rel="nofollow" aria-hidden="true"><span class="octicon octicon-link"></span></a><a href="http://www.example.com" rel="nofollow">Some <strong>bold</strong> <em>italic</em> link</a></h3>
}
func ExampleHeader() {
	text := []byte("## git diff")

	os.Stdout.Write(github_flavored_markdown.Markdown(text))

	// Output:
	// <h2><a name="git-diff" class="anchor" href="#git-diff" rel="nofollow" aria-hidden="true"><span class="octicon octicon-link"></span></a>git diff</h2>
}
func ExampleTaskList() {
	text := []byte(`- [ ] This is an incomplete task.
- [x] This is done.
`)

	os.Stdout.Write(github_flavored_markdown.Markdown(text))

	// Output:
	// <ul>
	// <li><input type="checkbox" disabled=""> This is an incomplete task.</li>
	// <li><input type="checkbox" checked="" disabled=""> This is done.</li>
	// </ul>
}
// An example of how to generate a complete HTML page, including CSS styles.
func ExampleMarkdown_completeHtmlPage() {
	var w io.Writer = os.Stdout // It can be an http.ResponseWriter.
	markdown := []byte("# GitHub Flavored Markdown\n\nHello.")

	io.WriteString(w, `<html><head><meta charset="utf-8"><link href=".../github-flavored-markdown.css" media="all" rel="stylesheet" type="text/css" /><link href="//cdnjs.cloudflare.com/ajax/libs/octicons/2.1.2/octicons.css" media="all" rel="stylesheet" type="text/css" /></head><body><article class="markdown-body entry-content" style="padding: 30px;">`)
	w.Write(github_flavored_markdown.Markdown(markdown))
	io.WriteString(w, `</article></body></html>`)

	// Output:
	// <html><head><meta charset="utf-8"><link href=".../github-flavored-markdown.css" media="all" rel="stylesheet" type="text/css" /><link href="//cdnjs.cloudflare.com/ajax/libs/octicons/2.1.2/octicons.css" media="all" rel="stylesheet" type="text/css" /></head><body><article class="markdown-body entry-content" style="padding: 30px;"><h1><a name="github-flavored-markdown" class="anchor" href="#github-flavored-markdown" rel="nofollow" aria-hidden="true"><span class="octicon octicon-link"></span></a>GitHub Flavored Markdown</h1>
	//
	// <p>Hello.</p>
	// </article></body></html>
}