markdown/markdown.go

61 lines
2.1 KiB
Go
Raw Normal View History

2011-06-28 02:11:32 +00:00
// Copyright © 2011 Russ Ross <russ@russross.com>.
// Distributed under the Simplified BSD License.
2018-01-25 21:01:19 +00:00
package markdown
2011-05-24 22:14:35 +00:00
import (
2016-07-05 04:32:16 +00:00
"io"
2011-05-24 22:14:35 +00:00
)
2017-07-29 08:59:03 +00:00
// Version string of the package. Appears in the rendered document when
// CompletePage flag is on.
const Version = "2.0"
// Renderer is an interface for implementing custom renderers.
2011-07-07 17:56:45 +00:00
//
// This package provides HTMLRenderer for markdown => HTML conversion.
2011-06-29 17:13:17 +00:00
type Renderer interface {
// RenderNode is the main rendering method. It will be called once for
// every leaf node and twice for every non-leaf node (first with
2017-07-30 16:50:50 +00:00
// entering=true, then with entering=false). The method should write its
// rendition of the node to writer w.
2016-07-05 04:32:16 +00:00
RenderNode(w io.Writer, node *Node, entering bool) WalkStatus
// RenderHeader is a method that allows the renderer to produce some
// content preceding the main body of the output document. The header is
// understood in the broad sense here. For example, the default HTML
// renderer will write not only the HTML document preamble, but also the
// table of contents if it was requested.
//
// The method will be passed an entire document tree, in case a particular
// implementation needs to inspect it to produce output.
//
// The output should be written to the supplied writer w. If your
// implementation has no header to write, supply an empty implementation.
RenderHeader(w io.Writer, ast *Node)
// RenderFooter is a symmetric counterpart of RenderHeader.
RenderFooter(w io.Writer, ast *Node)
2011-05-24 22:14:35 +00:00
}
2018-01-26 08:11:58 +00:00
// ToHTML converts a markdown text in input and converts it to HTML.
//
2018-01-26 08:11:58 +00:00
// You can optionally pass a parser and renderer, which allows to customize
// a parser, a render or provide a renderer other than HTMLRenderer.
//
2018-01-26 08:11:58 +00:00
// If you pass nil for both, we convert with CommonExtensions for
// the parser and HTMLRenderer with CommonHTMLFlags for renderer
2018-01-26 08:11:58 +00:00
func ToHTML(input []byte, parser *Parser, renderer Renderer) []byte {
if parser == nil {
parser = NewParserWithExtensions(CommonExtensions)
}
if renderer == nil {
params := HTMLRendererParameters{
Flags: CommonHTMLFlags,
}
renderer = NewHTMLRenderer(params)
}
2018-01-26 07:15:51 +00:00
parser.Parse(input)
2018-01-26 08:11:58 +00:00
return parser.Render(renderer)
2018-01-26 07:15:51 +00:00
}