mark

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2015 License: MIT Imports: 6 Imported by: 17

README

Mark Test coverage Build status

A markdown processor written in Go. built for fun.

This project inspired from Rob Pike - Lexical Scanning talk and marked project.
Please note that this is a WIP project and any contribution is welcomed and appreciated, so feel free to take some task here.

Table of contents:

Get Started
Installation
$ go get github.com/a8m/mark
Examples

Add to your project:

import (
	"fmt"
	"github.com/a8m/mark"
)

func main() {
	html := mark.Render("I am using __markdown__.")
	fmt.Println(html)
	// <p>I am using <strong>markdown</strong>.</p>
}

or using mark-cli

$ echo 'hello __world__...' | mark-cli -smartypants
Documentation
Render

Staic rendering function.

html := mark.Render("I am using __markdown__.")
fmt.Println(html)
// <p>I am using <strong>markdown</strong>.</p>
Mark
New

New get string as an input, and mark.Options as configuration and return a new Mark.

m := mark.New("hello world...", &mark.Options{
    Smartypants: true,
})
fmt.Println(m.Render())
// <p>hello world…</p>
// Note: you can instantiate it like so: mark.New("...", nil) to get the default options.
Mark.AddRenderFn

AddRenderFn let you pass NodeType, and RenderFn function and override the default Node rendering.
To get all Nodes type and their fields/methods, see the full documentation: go-doc

m := mark.New("hello", nil)
m.AddRenderFn(mark.NodeParagraph, func(node mark.Node) (s string) {
    p, _ := node.(*mark.ParagraphNode)
    s += "<p class=\"mv-msg\">"
    for _, n := range p.Nodes {
        s += n.Render()
    }
    s += "</p>"
    return
})
fmt.Println(m.Render())
// <p class="mv-msg">hello</p>
Mark.Render

Parse and render input.

m := mark.New("hello", nil)
fmt.Println(m.Render())
// <p>hello</p>
Smartypants and Smartfractions

Mark also support smartypants and smartfractions rendering

func main() {
	opts := mark.DefaultOptions()
	opts.Smartypants = true
	opts.Fractions = true
	m := mark.New("'hello', 1/2 beer please...", opts)
	fmt.Println(m.Render())
	// ‘hello’, ½ beer please…
}
Todo
  • Backslash escaping
    • should ignore inside code spans
  • Expand documentation
  • Configuration options
    • gfm, table
    • heading(auto hashing)
License

MIT

Documentation

Index

Constants

View Source
const (
	Header = iota
	Data
)

Cell types

Variables

This section is empty.

Functions

func Render

func Render(input string) string

Staic render function

Types

type AlignType

type AlignType int

AlignType identifies the aligment-type of specfic cell.

const (
	None AlignType = iota
	Right
	Left
	Center
)

Alignment

func (AlignType) Align

func (t AlignType) Align() AlignType

Align returns itself and provides an easy default implementation for embedding in a Node.

type BlockQuoteNode

type BlockQuoteNode struct {
	NodeType
	Pos
	Nodes []Node
}

BlockQuote represent

func (*BlockQuoteNode) Render

func (n *BlockQuoteNode) Render() string

Render return the html representation of BlockQuote

type BrNode

type BrNode struct {
	NodeType
	Pos
}

BrNode represent a link-break element.

func (*BrNode) Render

func (n *BrNode) Render() string

Render return the html representation of line-break.

type CellNode

type CellNode struct {
	NodeType
	Pos
	AlignType
	Kind  int
	Nodes []Node
}

CellNode represent table-data/cell that holds simple text(may be emphasis) Note: the text in <th> elements are bold and centered by default.

func (*CellNode) Render

func (c *CellNode) Render() string

Render return the html reprenestation of table-cell

func (*CellNode) Style

func (c *CellNode) Style() string

Style return the cell-style based on alignment field

type CodeNode

type CodeNode struct {
	NodeType
	Pos
	Lang, Text string
}

Code holds CodeBlock node with specific lang field.

func (*CodeNode) Render

func (n *CodeNode) Render() string

Return the html representation of codeBlock

type DefLinkNode

type DefLinkNode struct {
	NodeType
	Pos
	Name, Href, Title string
}

DefLinkNode refresent single reference to link-definition

func (*DefLinkNode) Render

func (n *DefLinkNode) Render() string

Deflink have no representation(Transparent node)

type EmphasisNode

type EmphasisNode struct {
	NodeType
	Pos
	Style itemType
	Nodes []Node
}

EmphasisNode holds plain-text wrapped with style. (strong, em, del, code)

func (*EmphasisNode) Render

func (n *EmphasisNode) Render() string

Return the html representation of emphasis text.

func (*EmphasisNode) Tag

func (n *EmphasisNode) Tag() (s string)

Tag return the tagName based on the Style field.

type HTMLNode

type HTMLNode struct {
	NodeType
	Pos
	Src string
}

HTMLNode holds the raw html source.

func (*HTMLNode) Render

func (n *HTMLNode) Render() string

Render return the src of the HTMLNode

type HeadingNode

type HeadingNode struct {
	NodeType
	Pos
	Level int
	Text  string
}

Heading holds heaing element with specific level(1-6).

func (*HeadingNode) Render

func (n *HeadingNode) Render() string

Render return the html representation based on heading level.

type HrNode

type HrNode struct {
	NodeType
	Pos
}

HrNode represent horizontal rule

func (*HrNode) Render

func (n *HrNode) Render() string

Render return the html representation of hr.

type ImageNode

type ImageNode struct {
	NodeType
	Pos
	Title, Src, Alt string
}

ImageNode represent an image element with optional alt and title attributes.

func (*ImageNode) Render

func (n *ImageNode) Render() string

Render return the html representation on image node

type Lexer

type Lexer interface {
	// contains filtered or unexported methods
}

Lexer interface, used to composed it inside the parser

type LinkNode

type LinkNode struct {
	NodeType
	Pos
	Title, Href, Text string
}

Link holds a tag with optional title

func (*LinkNode) Render

func (n *LinkNode) Render() string

Return the html representation of link node

type ListItemNode

type ListItemNode struct {
	NodeType
	Pos
	Nodes []Node
}

ListItem represent single item in ListNode that may contains nested nodes.

func (*ListItemNode) Render

func (n *ListItemNode) Render() (s string)

Render return the html representation of list-item

type ListNode

type ListNode struct {
	NodeType
	Pos
	Ordered bool
	Items   []*ListItemNode
}

ListNode holds list items nodes in ordered or unordered states.

func (*ListNode) Render

func (n *ListNode) Render() (s string)

Render return the html representation of orderd(ol) or unordered(ul) list.

type Mark

type Mark struct {
	Input string
	// contains filtered or unexported fields
}

Mark

func New

func New(input string, opts *Options) *Mark

New return a new Mark

func (*Mark) AddRenderFn

func (m *Mark) AddRenderFn(typ NodeType, fn RenderFn)

AddRenderFn let you pass NodeType, and RenderFn function and override the default Node rendering

func (*Mark) Render

func (m *Mark) Render() string

parse and render input

type Node

type Node interface {
	Type() NodeType
	Render() string
}

A Node is an element in the parse tree.

type NodeType

type NodeType int

NodeType identifies the type of a parse tree node.

const (
	NodeText       NodeType = iota // A plain text
	NodeParagraph                  // A Paragraph
	NodeEmphasis                   // An emphasis(strong, em, ...)
	NodeHeading                    // A heading (h1, h2, ...)
	NodeBr                         // A link break
	NodeHr                         // A horizontal rule
	NodeImage                      // An image
	NodeRefImage                   // A image reference
	NodeList                       // A list of ListItems
	NodeListItem                   // A list item node
	NodeLink                       // A link(href)
	NodeRefLink                    // A link reference
	NodeDefLink                    // A link definition
	NodeTable                      // A table of NodeRows
	NodeRow                        // A row of NodeCells
	NodeCell                       // A table-cell(td)
	NodeCode                       // A code block(wrapped with pre)
	NodeBlockQuote                 // A blockquote
	NodeHTML                       // An inline HTML
)

func (NodeType) Type

func (t NodeType) Type() NodeType

Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes.

type Options

type Options struct {
	Gfm         bool
	Tables      bool
	Smartypants bool
	Fractions   bool
}

Mark options

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions return an options struct with default configuration it's means that only Gfm, and Tables set to true.

type ParagraphNode

type ParagraphNode struct {
	NodeType
	Pos
	Nodes []Node
}

ParagraphNode hold simple paragraph node contains text that may be emphasis.

func (*ParagraphNode) Render

func (n *ParagraphNode) Render() (s string)

Render return the html representation of ParagraphNode

type Pos

type Pos int

type position

type RefNode

type RefNode struct {
	NodeType
	Pos

	Text, Ref, Raw string
	// contains filtered or unexported fields
}

RefLink holds link with refrence to link definition

func (*RefNode) Render

func (n *RefNode) Render() string

rendering based type

type RenderFn

type RenderFn func(Node) string

Render function, used for overriding default rendering.

type RowNode

type RowNode struct {
	NodeType
	Pos
	Cells []*CellNode
}

RowNode represnt tr that holds list of cell-nodes

func (*RowNode) Render

func (r *RowNode) Render() string

Render return the html representation of table-row

type TableNode

type TableNode struct {
	NodeType
	Pos
	Rows []*RowNode
}

TableNode represent table element contains head and body

func (*TableNode) Render

func (n *TableNode) Render() string

Render return the html representation of a table

type TextNode

type TextNode struct {
	NodeType
	Pos
	Text string
}

TextNode holds plain text.

func (*TextNode) Render

func (n *TextNode) Render() string

Render return the string representation of TexNode

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL