Beispiel #1
0
func markdownRender(ctx *RenderingContext) []byte {
	if ctx.RenderTOC {
		return blackfriday.Markdown(ctx.Content,
			getHTMLRenderer(blackfriday.HTML_TOC, ctx),
			getMarkdownExtensions(ctx))
	}
	return blackfriday.Markdown(ctx.Content, getHTMLRenderer(0, ctx),
		getMarkdownExtensions(ctx))
}
Beispiel #2
0
func main() {
	flag.Parse()

	args := flag.Args()

	renderer := &Console{}
	extensions := 0 |
		blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
		blackfriday.EXTENSION_FENCED_CODE |
		blackfriday.EXTENSION_AUTOLINK |
		blackfriday.EXTENSION_STRIKETHROUGH |
		blackfriday.EXTENSION_SPACE_HEADERS |
		blackfriday.EXTENSION_HEADER_IDS |
		blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
		blackfriday.EXTENSION_DEFINITION_LISTS

	if len(args) > 0 {
		for i := 0; i < len(args); i++ {
			input, err := ioutil.ReadFile(args[i])
			if err != nil {
				os.Stderr.WriteString(fmt.Sprintf("mdcat: %s: unable to read from file\n", args[i]))
				os.Exit(1)
			}

			output := blackfriday.Markdown(input, renderer, extensions)
			os.Stdout.Write(output)
		}
	} else {
		reader := bufio.NewReader(os.Stdin)

		var input []byte
		buffer := make([]byte, 2<<20)

		for {
			count, err := reader.Read(buffer)

			if count == 0 {
				break
			}

			if err != nil {
				os.Stderr.WriteString("mdcat: unable to read from pipe\n")
				os.Exit(1)
			}

			input = append(input, buffer...)
		}

		output := blackfriday.Markdown(input, renderer, extensions)
		os.Stdout.Write(output)
	}

}
Beispiel #3
0
func (post *PostModel) RenderPost(postId string) *Post {
	result := post.Get(postId)
	renderer := blackfriday.HtmlRenderer(post.html_flags, "", "")
	if result.Type == 1 {
		result.Content = string(blackfriday.Markdown([]byte(result.Content), renderer, post.extensions))
	}

	// Also render all blog comments as markdown
	for i, v := range result.Comments {
		result.Comments[i].Content = string(blackfriday.Markdown([]byte(v.Content), renderer, post.extensions))
	}
	return result
}
Beispiel #4
0
func markdownFile(path string) (string, error) {
	if input, err := ioutil.ReadFile(path); err == nil {
		htmlFlags := 0
		htmlFlags |= blackfriday.HTML_USE_XHTML
		htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
		htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
		htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
		renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")

		// set up the parser
		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
		extensions |= blackfriday.EXTENSION_FOOTNOTES

		data := blackfriday.Markdown(input, renderer, extensions)
		return string(data), nil
	} else {
		return "", err
	}
}
Beispiel #5
0
// RenderRaw renders Markdown to HTML without handling special links.
func RenderRaw(body []byte, urlPrefix string) []byte {
	htmlFlags := 0
	htmlFlags |= blackfriday.HTML_SKIP_STYLE
	htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
	renderer := &Renderer{
		Renderer:  blackfriday.HtmlRenderer(htmlFlags, "", ""),
		urlPrefix: urlPrefix,
	}

	// set up the parser
	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
	extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK

	if setting.Markdown.EnableHardLineBreak {
		extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
	}

	body = blackfriday.Markdown(body, renderer, extensions)
	return body
}
Beispiel #6
0
// Markdown renders GitHub Flavored Markdown text.
func Markdown(text []byte) []byte {
	htmlFlags := 0
	renderer := &renderer{Html: blackfriday.HtmlRenderer(htmlFlags, "", "").(*blackfriday.Html)}

	// Parser extensions for GitHub Flavored Markdown.
	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
	//extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK

	unsanitized := blackfriday.Markdown(text, renderer, extensions)

	// GitHub Flavored Markdown-like sanitization policy.
	p := bluemonday.UGCPolicy()
	p.AllowAttrs("class").Matching(bluemonday.SpaceSeparatedTokens).OnElements("div", "span")
	p.AllowAttrs("class", "name").Matching(bluemonday.SpaceSeparatedTokens).OnElements("a")
	p.AllowAttrs("rel").Matching(regexp.MustCompile(`^nofollow$`)).OnElements("a")
	p.AllowAttrs("aria-hidden").Matching(regexp.MustCompile(`^true$`)).OnElements("a")
	p.AllowDataURIImages()

	return p.SanitizeBytes(unsanitized)
}
Beispiel #7
0
// RenderPost runs the input source through the blackfriday library.
func RenderPost(post *Post, input []byte, page PageParams) ([]byte, error) {
	tpl, err := page.getTemplate("post")
	if err != nil {
		return nil, err
	}

	content := blackfriday.Markdown(
		input,
		blackfriday.HtmlRenderer(page.Blog.GetMarkdownHTMLOptions(), "", ""),
		page.Blog.GetMarkdownExtensions())

	page.Title = post.Title
	params := PostPageParams{
		Post:       post,
		Content:    string(content),
		PageParams: page,
	}

	buf := new(bytes.Buffer)
	if err := tpl.Execute(buf, params); err != nil {
		return nil, err
	}

	return wrapPage(buf.Bytes(), params.PageParams)
}
Beispiel #8
0
func RenderMarkdown(input []byte) []byte {
	// set up the HTML renderer
	htmlFlags := 0
	htmlFlags |= blackfriday.HTML_USE_XHTML
	htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
	htmlFlags |= blackfriday.HTML_FOOTNOTE_RETURN_LINKS
	renderer := &renderer{
		Html: blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", blackfriday.HtmlRendererParameters{
			FootnoteReturnLinkContents: "↩",
		}).(*blackfriday.Html),
	}

	// set up the parser
	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
	extensions |= blackfriday.EXTENSION_HEADER_IDS
	extensions |= blackfriday.EXTENSION_FOOTNOTES

	return blackfriday.Markdown(input, renderer, extensions)
}
// MarkdownToTextNoMetadata is the same as MarkdownToText only skipping the
// detection and parsing of any leading metadata. If opts is nil the defaults
// will be used.
func MarkdownToTextNoMetadata(markdown []byte, opts *Options) []byte {
	opts = resolveOpts(opts)
	rend := &renderer{
		width:             opts.Width,
		color:             opts.Color,
		tableAlignOptions: opts.TableAlignOptions,
		headerPrefix:      opts.HeaderPrefix,
		headerSuffix:      opts.HeaderSuffix,
	}
	markdown = bytes.Replace(markdown, []byte("\n///\n"), []byte(""), -1)
	txt := blackfriday.Markdown(markdown, rend,
		blackfriday.EXTENSION_NO_INTRA_EMPHASIS|
			blackfriday.EXTENSION_TABLES|
			blackfriday.EXTENSION_FENCED_CODE|
			blackfriday.EXTENSION_AUTOLINK|
			blackfriday.EXTENSION_STRIKETHROUGH|
			blackfriday.EXTENSION_DEFINITION_LISTS)
	for rend.level > 0 {
		txt = append(txt, markIndentStop)
		rend.level--
	}
	if len(txt) > 0 {
		txt = bytes.Replace(txt, []byte(" \n"), []byte(" "), -1)
		txt = bytes.Replace(txt, []byte("\n"), []byte(" "), -1)
		txt = reflow(txt, opts.Indent1, opts.Indent2, rend.width)
		txt = bytes.Replace(txt, []byte{markNBSP}, []byte(" "), -1)
		txt = bytes.Replace(txt, []byte{markLineBreak}, []byte("\n"), -1)
	}
	return txt
}
Beispiel #10
0
//渲染评论HTML代码
func (cl CommentNode) renderItemBegin(b *bytes.Buffer, sortType string) {
	if cl.Status == 2 {
		cl.Content = "[已删除]"
	}
	b.WriteString(fmt.Sprintf(`<div id="cm-%d" class="cm" data-id="%v">
<div class="vt">
 <a class="icon-thumbs-up up" href="javascript:"></a>
 <a class="icon-thumbs-down down" href="javascript:"></a>
</div>
<div class="ct">
 <div class="uif">
   <a class="ep" href="javascript:">[–]</a>
   <a href="/user/%v">%v</a>
   <i class="v" title="↑%v ↓%v">%v分</i> <i class="t">%v</i>
 </div>
 <div class="tx">%s</div>
 <div class="ed">
   <a href="/link/permacoment/%v/%v/?cm_order=%s" class="cbtn">查看</a>
   <a href="javascript:" class="cbtn rp">回复</a>
 </div>`, cl.Id, cl.Id,
		cl.UserId, cl.UserName,
		cl.VoteUp, cl.VoteDown,
		cl.VoteUp-cl.VoteDown,
		cl.SinceTime(), blackfriday.Markdown([]byte(cl.Content), renderer, extensions), //strings.Replace(cl.Content, "\n", "<br/>", -1),
		cl.LinkId, cl.Id, sortType))
}
Beispiel #11
0
// Renders markdown in submitted messages.
func Render(raw []byte) []byte {
	htmlFlags := 0
	htmlFlags |= blackfriday.HTML_USE_XHTML
	htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
	renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")

	// set up the parser
	extensions := 0
	extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
	extensions |= blackfriday.EXTENSION_TABLES
	extensions |= blackfriday.EXTENSION_FENCED_CODE
	extensions |= blackfriday.EXTENSION_STRIKETHROUGH
	extensions |= blackfriday.EXTENSION_SPACE_HEADERS

	escaped := new(bytes.Buffer)
	template.HTMLEscape(escaped, raw)
	rendered := blackfriday.Markdown(escaped.Bytes(), renderer, extensions)
	enabled := true
	fmt.Println("Raw message:")
	fmt.Println(string(raw))
	if enabled {
		fmt.Println("Rendered message:")
		fmt.Println(string(rendered))
		return rendered
	}
	return raw
}
Beispiel #12
0
func RenderMarkdown(rawBytes []byte, urlPrefix string) []byte {
	// slow!
	//body := RenderSpecialLink(rawBytes, urlPrefix)
	body := rawBytes
	htmlFlags := 0
	// htmlFlags |= gfm.HTML_USE_XHTML
	// htmlFlags |= gfm.HTML_USE_SMARTYPANTS
	// htmlFlags |= gfm.HTML_SMARTYPANTS_FRACTIONS
	// htmlFlags |= gfm.HTML_SMARTYPANTS_LATEX_DASHES
	// htmlFlags |= gfm.HTML_SKIP_HTML
	htmlFlags |= gfm.HTML_SKIP_STYLE
	//htmlFlags |= gfm.HTML_SKIP_SCRIPT
	htmlFlags |= gfm.HTML_GITHUB_BLOCKCODE
	htmlFlags |= gfm.HTML_OMIT_CONTENTS
	// htmlFlags |= gfm.HTML_COMPLETE_PAGE
	renderer := &CustomRender{
		Renderer:  gfm.HtmlRenderer(htmlFlags, "", ""),
		urlPrefix: urlPrefix,
	}

	// set up the parser
	extensions := 0
	extensions |= gfm.EXTENSION_NO_INTRA_EMPHASIS
	extensions |= gfm.EXTENSION_TABLES
	extensions |= gfm.EXTENSION_FENCED_CODE
	extensions |= gfm.EXTENSION_AUTOLINK
	extensions |= gfm.EXTENSION_STRIKETHROUGH
	extensions |= gfm.EXTENSION_HARD_LINE_BREAK
	extensions |= gfm.EXTENSION_SPACE_HEADERS
	extensions |= gfm.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK

	body = gfm.Markdown(body, renderer, extensions)
	return body
}
Beispiel #13
0
func markdown(raw []byte) []byte {
	htmlFlags := 0
	htmlFlags |= blackfriday.HTML_USE_XHTML
	htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
	htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
	htmlFlags |= blackfriday.HTML_COMPLETE_PAGE

	renderer := &CustomRender{
		Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
	}

	// set up the parser
	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_HARD_LINE_BREAK
	extensions |= blackfriday.EXTENSION_SPACE_HEADERS
	extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK

	body := blackfriday.Markdown(raw, renderer, extensions)
	return body
}
Beispiel #14
0
func RenderRawMarkdown(body []byte, urlPrefix string) []byte {
	htmlFlags := 0
	// htmlFlags |= blackfriday.HTML_USE_XHTML
	// htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
	// htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	// htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
	// htmlFlags |= blackfriday.HTML_SKIP_HTML
	htmlFlags |= blackfriday.HTML_SKIP_STYLE
	// htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
	// htmlFlags |= blackfriday.HTML_GITHUB_BLOCKCODE
	htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
	// htmlFlags |= blackfriday.HTML_COMPLETE_PAGE
	renderer := &CustomRender{
		Renderer:  blackfriday.HtmlRenderer(htmlFlags, "", ""),
		urlPrefix: urlPrefix,
	}

	// set up the parser
	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
	extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK

	if setting.Markdown.EnableHardLineBreak {
		extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
	}

	body = blackfriday.Markdown(body, renderer, extensions)
	return body
}
Beispiel #15
0
// Markdown formats input using a plain-text renderer, and
// then returns up to the first `wordcount` words as a summary.
func Markdown(input []byte, wordcount int) []byte {
	words := bytes.Fields(blackfriday.Markdown(input, renderer{}, 0))
	if wordcount > len(words) {
		wordcount = len(words)
	}
	return bytes.Join(words[0:wordcount], []byte{' '})
}
Beispiel #16
0
func renderMarkdown(appDir string, path string, markdownContent string, jsonContent map[string]interface{}) {
	renderer, extensions := configureBlackFriday(path)
	content := blackfriday.Markdown([]byte(markdownContent), renderer, extensions)

	re2 := regexp.MustCompile("/content/")
	src := []byte(path)
	replacement := []byte("/views/")
	htmlPath := re2.ReplaceAll(src, replacement)
	template, _ := loadHTML(appDir, string(htmlPath))

	result, err := ParseAndInsert(appDir, string(content), jsonContent, template)
	if err != nil {
		log.Fatal(err)
	}

	re := regexp.MustCompile("/content/(.+)")
	if matches := re.FindStringSubmatch(path); matches != nil && len(matches) == 2 {
		oldPath := matches[1]
		//tmpPath is the expected location before some manipulation around the filename, e.g. convert
		// blah.fuel to blah.html
		tmpPath := fmt.Sprintf("%s/public/%s", appDir, oldPath)
		//convert from .fuel to .html
		newDir, newPath := convertFromFuelToHTML(tmpPath)
		//make the new dir in public
		os.MkdirAll(newDir, 0777)
		//output is a []byte -- write it to a file
		ioutil.WriteFile(newPath, []byte(result), 0644)
	}
}
Beispiel #17
0
func RenderMarkdown(mdStr string) string {
	htmlFlags := 0
	htmlFlags |= blackfriday.HTML_USE_XHTML
	// htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
	// htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	// htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
	htmlFlags |= blackfriday.HTML_SKIP_HTML
	htmlFlags |= blackfriday.HTML_SKIP_STYLE
	htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
	htmlFlags |= blackfriday.HTML_GITHUB_BLOCKCODE
	htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
	htmlFlags |= blackfriday.HTML_COMPLETE_PAGE
	renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")

	// set up the parser
	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_HARD_LINE_BREAK
	extensions |= blackfriday.EXTENSION_SPACE_HEADERS
	extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK

	body := blackfriday.Markdown([]byte(mdStr), renderer, extensions)

	return string(body)
}
Beispiel #18
0
func RenderMarkdownSafe(input []byte) []byte {
	// set up the HTML renderer
	flags := 0
	flags |= blackfriday.HTML_USE_XHTML
	flags |= blackfriday.HTML_USE_SMARTYPANTS
	flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
	flags |= blackfriday.HTML_SKIP_HTML
	flags |= blackfriday.HTML_SKIP_SCRIPT
	flags |= blackfriday.HTML_SKIP_STYLE
	// flags |= blackfriday.HTML_TOC
	// flags |= blackfriday.HTML_GITHUB_BLOCKCODE
	renderer := blackfriday.HtmlRenderer(flags, "", "")

	// set up the parser
	ext := 0
	ext |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
	ext |= blackfriday.EXTENSION_TABLES
	ext |= blackfriday.EXTENSION_FENCED_CODE
	ext |= blackfriday.EXTENSION_AUTOLINK
	ext |= blackfriday.EXTENSION_STRIKETHROUGH
	ext |= blackfriday.EXTENSION_SPACE_HEADERS
	ext |= blackfriday.EXTENSION_HARD_LINE_BREAK
	ext |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK

	return blackfriday.Markdown(input, renderer, ext)
}
Beispiel #19
0
func (p *Post) html() (T.HTML, error) {
	body, err := p.withImages()
	if err != nil {
		return "", err
	}
	extensions := md.EXTENSION_SPACE_HEADERS | md.EXTENSION_TABLES | md.EXTENSION_NO_INTRA_EMPHASIS | md.EXTENSION_STRIKETHROUGH | md.EXTENSION_FOOTNOTES
	renderer := md.HtmlRenderer(0, "", "")
	return T.HTML(md.Markdown([]byte(body), renderer, extensions)), nil
}
Beispiel #20
0
/*
Process a subset of markdown elements and prints them with flying colours!
*/
func Markdown(input []byte) []byte {
	tr := NewTerminalRenderer()
	ret := blackfriday.Markdown(input, tr, blackfriday.EXTENSION_STRIKETHROUGH)
	//get rid of \n at the end of the text
	for len(ret) > 0 && ret[len(ret)-1] == '\n' {
		ret = ret[:len(ret)-1]
	}
	return ret
}
Beispiel #21
0
func ToHtml(content string) template.HTML {
	htmlFlags := 0
	htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS

	renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")

	html := blackfriday.Markdown([]byte(content), renderer, enabled_md_extensions)
	return template.HTML(bluemonday.UGCPolicy().SanitizeBytes(html))
}
Beispiel #22
0
// Process processes the contents of a page in b. It parses the metadata
// (if any) and uses the template (if found).
func (md Markdown) Process(c *Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
	var metadata = newMetadata()
	var markdown []byte
	var err error

	// find parser compatible with page contents
	parser := findParser(b)

	if parser == nil {
		// if not found, assume whole file is markdown (no front matter)
		markdown = b
	} else {
		// if found, assume metadata present and parse.
		markdown, err = parser.Parse(b)
		if err != nil {
			return nil, err
		}
		metadata = parser.Metadata()
	}

	// if template is not specified, check if Default template is set
	if metadata.Template == "" {
		if _, ok := c.Templates[DefaultTemplate]; ok {
			metadata.Template = DefaultTemplate
		}
	}

	// if template is set, load it
	var tmpl []byte
	if metadata.Template != "" {
		if t, ok := c.Templates[metadata.Template]; ok {
			tmpl, err = ioutil.ReadFile(t)
		}
		if err != nil {
			return nil, err
		}
	}

	// process markdown
	extns := blackfriday.EXTENSION_TABLES | blackfriday.EXTENSION_FENCED_CODE | blackfriday.EXTENSION_STRIKETHROUGH | blackfriday.EXTENSION_DEFINITION_LISTS
	markdown = blackfriday.Markdown(markdown, c.Renderer, extns)

	// set it as body for template
	metadata.Variables["body"] = string(markdown)
	title := metadata.Title
	if title == "" {
		title = filepath.Base(requestPath)
		var extension = filepath.Ext(requestPath)
		title = title[0 : len(title)-len(extension)]
	}
	metadata.Variables["title"] = title

	return md.processTemplate(c, requestPath, tmpl, metadata, ctx)
}
Beispiel #23
0
// Markdown returns the HTML contents of the markdown contained in filename
// (relative to the site root).
func (c Context) Markdown(filename string) (string, error) {
	body, err := c.Include(filename)
	if err != nil {
		return "", err
	}
	renderer := blackfriday.HtmlRenderer(0, "", "")
	extns := blackfriday.EXTENSION_TABLES | blackfriday.EXTENSION_FENCED_CODE | blackfriday.EXTENSION_STRIKETHROUGH | blackfriday.EXTENSION_DEFINITION_LISTS
	markdown := blackfriday.Markdown([]byte(body), renderer, extns)

	return string(markdown), nil
}
Beispiel #24
0
func main() {
	// read the input
	var input []byte
	var err os.Error
	switch len(os.Args) {
	case 1:
		if input, err = ioutil.ReadAll(os.Stdin); err != nil {
			fmt.Fprintln(os.Stderr, "Error reading from Stdin:", err)
			os.Exit(-1)
		}
	case 2, 3:
		if input, err = ioutil.ReadFile(os.Args[1]); err != nil {
			fmt.Fprintln(os.Stderr, "Error reading from", os.Args[1], ":", err)
			os.Exit(-1)
		}
	default:
		fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "[inputfile [outputfile]]")
		os.Exit(-1)
	}

	// set up options
	var extensions uint32
	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

	html_flags := 0
	html_flags |= blackfriday.HTML_USE_XHTML
	// note: uncomment the following line to enable smartypants
	// it is commented out by default so that markdown
	// compatibility tests pass
	//html_flags |= blackfriday.HTML_USE_SMARTYPANTS
	html_flags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	html_flags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES

	// render the data
	output := blackfriday.Markdown(input, blackfriday.HtmlRenderer(html_flags), extensions)

	// output the result
	if len(os.Args) == 3 {
		if err = ioutil.WriteFile(os.Args[2], output, 0644); err != nil {
			fmt.Fprintln(os.Stderr, "Error writing to", os.Args[2], ":", err)
			os.Exit(-1)
		}
	} else {
		if _, err = os.Stdout.Write(output); err != nil {
			fmt.Fprintln(os.Stderr, "Error writing to Stdout:", err)
			os.Exit(-1)
		}
	}
}
Beispiel #25
0
func getHtmlPageData(title string, md string) *PageData {
	mdData := MarkdownData{}
	pageData := PageData{Title: "Walking Noticeboard"}
	mdData.Title = title
	mdData.MdContent = md

	htmlRenderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
	mdData.HtmlContent = template.HTML(blackfriday.Markdown([]byte(md), htmlRenderer, htmlExt))
	pageData.MarkDown = mdData

	return &pageData
}
Beispiel #26
0
func markdownFormatter(formatter *Formatter, stream io.Reader, args ...string) (string, error) {
	buf := &bytes.Buffer{}
	io.Copy(buf, stream)
	md := blackfriday.Markdown(buf.Bytes(), mkdHtmlRenderer,
		blackfriday.EXTENSION_NO_INTRA_EMPHASIS|
			blackfriday.EXTENSION_TABLES|
			blackfriday.EXTENSION_AUTOLINK|
			blackfriday.EXTENSION_FENCED_CODE|
			blackfriday.EXTENSION_HEADER_IDS|
			blackfriday.EXTENSION_LAX_HTML_BLOCKS)
	return sanitationPolicy.Sanitize(string(md)), nil
}
Beispiel #27
0
func getLongDescriptionForGopkg(gopkg string) (string, error) {
	if !strings.HasPrefix(gopkg, "github.com/") {
		return "", nil
	}
	resp, err := http.Get("https://api.github.com/repos/" + gopkg[len("github.com/"):] + "/readme")
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("unexpected HTTP status: got %d, want %d", resp.StatusCode, http.StatusOK)
	}

	var rr readmeReply
	if err := json.NewDecoder(resp.Body).Decode(&rr); err != nil {
		return "", err
	}

	if rr.Encoding != "base64" {
		return "", fmt.Errorf("unexpected encoding: got %q, want %q", rr.Encoding, "base64")
	}

	content, err := base64.StdEncoding.DecodeString(rr.Content)
	if err != nil {
		return "", err
	}

	// Supported filename suffixes are from
	// https://github.com/github/markup/blob/master/README.md
	// NOTE(stapelberg): Ideally, we’d use https://github.com/github/markup
	// itself to render to HTML, then convert HTML to plaintext. That sounds
	// fairly involved, but it’d be the most correct solution to the problem at
	// hand. Our current code just knows markdown, which is good enough since
	// most (Go?) projects in fact use markdown for their README files.
	if !strings.HasSuffix(rr.Name, "md") &&
		!strings.HasSuffix(rr.Name, "markdown") &&
		!strings.HasSuffix(rr.Name, "mdown") &&
		!strings.HasSuffix(rr.Name, "mkdn") {
		return strings.TrimSpace(string(content)), nil
	}

	output := blackfriday.Markdown(content, &TextRenderer{}, 0)
	// Shell out to fmt(1) to line-wrap the output.
	cmd := exec.Command("fmt")
	cmd.Stdin = bytes.NewBuffer(output)
	out, err := cmd.Output()
	if err != nil {
		return "", err
	}
	return strings.TrimSpace(string(out)), nil
}
Beispiel #28
0
func (g *Generator) markdown(name string, argsv ...string) string {
	// find template with correct name
	t := g.Master.Lookup(name)
	if t == nil {
		abort("cannot find template '%s' (used as markdown function argument)", name)
	}

	// create new scope and execute the template
	args := g.ParseArgs(argsv)
	g.PushScope(args)
	defer g.PopScope()

	buf := bytes.NewBuffer(nil)
	err := t.Execute(buf, nil)
	if err != nil {
		abort("ff%s", err)
	}

	// set up the HTML renderer
	htmlFlags := 0
	htmlFlags |= blackfriday.HTML_USE_XHTML
	htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
	htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
	htmlFlags |= blackfriday.HTML_GITHUB_BLOCKCODE

	// set up the parser
	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
	extensions |= blackfriday.EXTENSION_FOOTNOTES

	if str2bool(args["toc"]) {
		htmlFlags |= blackfriday.HTML_TOC
	}

	// convert markdown to html
	basename := path.Base(name)
	extension := path.Ext(basename)
	mdname := basename[0 : len(basename)-len(extension)]

	renderer := blackfriday.HtmlRenderer(htmlFlags, mdname, "")

	html := blackfriday.Markdown(buf.Bytes(), renderer, extensions)

	return string(html)
}
Beispiel #29
0
func renderMarkdown(title string, input []byte) []byte {
	var renderer md.Renderer
	htmlFlags := 0
	extensions := 0
	css := "../stylesheets/styles.css"
	htmlFlags |= md.HTML_USE_XHTML
	htmlFlags |= md.HTML_USE_SMARTYPANTS
	htmlFlags |= md.HTML_SMARTYPANTS_FRACTIONS
	htmlFlags |= md.HTML_COMPLETE_PAGE
	renderer = md.HtmlRenderer(htmlFlags, title, css)
	return md.Markdown(input, renderer, extensions)

}
Beispiel #30
0
func Render(doc []byte) []byte {
	renderer := RoffRenderer(0)
	extensions := 0
	extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
	extensions |= blackfriday.EXTENSION_TABLES
	extensions |= blackfriday.EXTENSION_FENCED_CODE
	extensions |= blackfriday.EXTENSION_AUTOLINK
	extensions |= blackfriday.EXTENSION_SPACE_HEADERS
	extensions |= blackfriday.EXTENSION_FOOTNOTES
	extensions |= blackfriday.EXTENSION_TITLEBLOCK

	return blackfriday.Markdown(doc, renderer, extensions)
}