Ejemplo n.º 1
0
// GetHtmlRenderer creates a new Renderer with the given configuration.
func GetHTMLRenderer(defaultFlags int, ctx *RenderingContext) blackfriday.Renderer {
	renderParameters := blackfriday.HtmlRendererParameters{
		FootnoteAnchorPrefix:       viper.GetString("FootnoteAnchorPrefix"),
		FootnoteReturnLinkContents: viper.GetString("FootnoteReturnLinkContents"),
	}

	b := len(ctx.DocumentID) != 0

	if b && !ctx.getConfig().PlainIDAnchors {
		renderParameters.FootnoteAnchorPrefix = ctx.DocumentID + ":" + renderParameters.FootnoteAnchorPrefix
		renderParameters.HeaderIDSuffix = ":" + ctx.DocumentID
	}

	htmlFlags := defaultFlags
	htmlFlags |= blackfriday.HTML_USE_XHTML
	htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
	htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
	htmlFlags |= blackfriday.HTML_FOOTNOTE_RETURN_LINKS

	if ctx.getConfig().AngledQuotes {
		htmlFlags |= blackfriday.HTML_SMARTYPANTS_ANGLED_QUOTES
	}

	if ctx.getConfig().Fractions {
		htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	}

	return blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters)
}
Ejemplo n.º 2
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)
}
Ejemplo n.º 3
0
func (instance *Renderer) renderMarkdownWithContext(markup string, context Describeable, headerTypeStart int, headerIDPrefix string) (template.HTML, error) {
	var err error

	markup = headerPrefixPattern.ReplaceAllString(markup, "$1"+strings.Repeat("#", headerTypeStart))
	markup = refPropertyPattern.ReplaceAllStringFunc(markup, func(inline string) string {
		match := refPropertyPattern.FindStringSubmatch(inline)
		ref := match[1]
		idType := instance.resolveRef(ref, context)
		element, pErr := instance.PickedDefinitions.GetSourceElementBy(idType)
		if pErr != nil {
			err = pErr
			return inline
		}
		if element != nil {
			targetType := instance.getDisplayIDOf(element)
			display := strings.TrimSpace(match[2])
			if len(display) <= 0 {
				display = targetType
			}
			return "[``" + display + "``](#configuration.dataType." + targetType + ")"
		}
		err = errors.New("Unknonwn reference: %v", ref)
		return markup
	})
	if err != nil {
		return "", err
	}
	prefix := ""
	if len(headerIDPrefix) > 0 {
		prefix = headerIDPrefix + "."
	}
	renderer := blackfriday.HtmlRendererWithParameters(blackfriday.HTML_USE_XHTML|
		blackfriday.HTML_USE_SMARTYPANTS|
		blackfriday.HTML_SMARTYPANTS_FRACTIONS|
		blackfriday.HTML_SMARTYPANTS_DASHES|
		blackfriday.HTML_SMARTYPANTS_LATEX_DASHES,
		"",
		"",
		blackfriday.HtmlRendererParameters{
			HeaderIDPrefix: prefix,
		},
	)
	html := blackfriday.MarkdownOptions([]byte(markup), renderer, blackfriday.Options{
		Extensions: blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
			blackfriday.EXTENSION_TABLES |
			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 |
			blackfriday.EXTENSION_AUTO_HEADER_IDS,
	})
	return template.HTML(strings.TrimSpace(string(html))), nil
}
Ejemplo n.º 4
0
// Render a markdown into html
func Render(markdown string) []byte {

	params := blackfriday.HtmlRendererParameters{
		BlockRenderer: blockRenderer,
	}

	renderer := blackfriday.HtmlRendererWithParameters(commonHTMLFlags, "", "", params)

	html := blackfriday.Markdown([]byte(markdown), renderer, commonExtensions)

	var out bytes.Buffer
	mustWriteString(&out, "<div class='markdown'>")
	mustWrite(&out, []byte(html))
	mustWriteString(&out, "</div>")
	return out.Bytes()
}
Ejemplo n.º 5
0
// getHTMLRenderer creates a new Blackfriday HTML Renderer with the given configuration.
func getHTMLRenderer(defaultFlags int, ctx *RenderingContext) blackfriday.Renderer {
	renderParameters := blackfriday.HtmlRendererParameters{
		FootnoteAnchorPrefix:       viper.GetString("FootnoteAnchorPrefix"),
		FootnoteReturnLinkContents: viper.GetString("FootnoteReturnLinkContents"),
	}

	b := len(ctx.DocumentID) != 0

	if b && !ctx.getConfig().PlainIDAnchors {
		renderParameters.FootnoteAnchorPrefix = ctx.DocumentID + ":" + renderParameters.FootnoteAnchorPrefix
		renderParameters.HeaderIDSuffix = ":" + ctx.DocumentID
	}

	htmlFlags := defaultFlags
	htmlFlags |= blackfriday.HTML_USE_XHTML
	htmlFlags |= blackfriday.HTML_FOOTNOTE_RETURN_LINKS

	if ctx.getConfig().Smartypants {
		htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
	}

	if ctx.getConfig().AngledQuotes {
		htmlFlags |= blackfriday.HTML_SMARTYPANTS_ANGLED_QUOTES
	}

	if ctx.getConfig().Fractions {
		htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
	}

	if ctx.getConfig().HrefTargetBlank {
		htmlFlags |= blackfriday.HTML_HREF_TARGET_BLANK
	}

	if ctx.getConfig().SmartDashes {
		htmlFlags |= blackfriday.HTML_SMARTYPANTS_DASHES
	}

	if ctx.getConfig().LatexDashes {
		htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
	}

	return &HugoHTMLRenderer{
		FileResolver: ctx.FileResolver,
		LinkResolver: ctx.LinkResolver,
		Renderer:     blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters),
	}
}
Ejemplo n.º 6
0
func GetHtmlRenderer(defaultFlags int, footnoteref string) blackfriday.Renderer {
	renderParameters := blackfriday.HtmlRendererParameters{
		FootnoteAnchorPrefix:       viper.GetString("FootnoteAnchorPrefix"),
		FootnoteReturnLinkContents: viper.GetString("FootnoteReturnLinkContents"),
	}

	if len(footnoteref) != 0 {
		renderParameters.FootnoteAnchorPrefix = footnoteref + ":" +
			renderParameters.FootnoteAnchorPrefix
	}

	htmlFlags := defaultFlags
	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

	return blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters)
}