package main import ( "bytes" "fmt" "html" "unicode" ) const ( MKDA_NOT_AUTOLINK = iota MKDA_NORMAL MKDA_EMAIL ) const ( MKDEXT_NO_INTRA_EMPHASIS = 1 << iota MKDEXT_TABLES MKDEXT_FENCED_CODE MKDEXT_AUTOLINK MKDEXT_STRIKETHROUGH MKDEXT_LAX_HTML_BLOCKS MKDEXT_SPACE_HEADERS ) const ( _ = iota MKD_LIST_ORDERED MKD_LI_BLOCK //
0 {
ob.WriteByte(' ')
}
attr_escape(ob, []byte(lang[org:]))
}
}
ob.WriteString("\">")
} else {
ob.WriteString("")
}
if len(text) > 0 {
attr_escape(ob, text)
}
ob.WriteString("
\n")
}
func rndr_table(ob *bytes.Buffer, header []byte, body []byte, opaque interface{}) {
if ob.Len() > 0 {
ob.WriteByte('\n')
}
ob.WriteString("\n")
ob.Write(header)
ob.WriteString("\n\n")
ob.Write(body)
ob.WriteString("\n
")
}
func rndr_tablerow(ob *bytes.Buffer, text []byte, opaque interface{}) {
if ob.Len() > 0 {
ob.WriteByte('\n')
}
ob.WriteString("\n")
ob.Write(text)
ob.WriteString("\n ")
}
func rndr_tablecell(ob *bytes.Buffer, text []byte, align int, opaque interface{}) {
if ob.Len() > 0 {
ob.WriteByte('\n')
}
switch align {
case MKD_TABLE_ALIGN_L:
ob.WriteString("")
case MKD_TABLE_ALIGN_R:
ob.WriteString(" ")
case MKD_TABLE_ALIGN_CENTER:
ob.WriteString(" ")
default:
ob.WriteString(" ")
}
ob.Write(text)
ob.WriteString(" ")
}
func rndr_list(ob *bytes.Buffer, text []byte, flags int, opaque interface{}) {
if ob.Len() > 0 {
ob.WriteByte('\n')
}
if flags&MKD_LIST_ORDERED != 0 {
ob.WriteString("\n")
} else {
ob.WriteString("\n")
}
ob.Write(text)
if flags&MKD_LIST_ORDERED != 0 {
ob.WriteString("
\n")
} else {
ob.WriteString("\n")
}
}
func rndr_listitem(ob *bytes.Buffer, text []byte, flags int, opaque interface{}) {
ob.WriteString("")
size := len(text)
for size > 0 && text[size-1] == '\n' {
size--
}
ob.Write(text[:size])
ob.WriteString(" \n")
}
func rndr_paragraph(ob *bytes.Buffer, text []byte, opaque interface{}) {
options := opaque.(*html_renderopts)
i := 0
if ob.Len() > 0 {
ob.WriteByte('\n')
}
if len(text) == 0 {
return
}
for i < len(text) && unicode.IsSpace(int(text[i])) {
i++
}
if i == len(text) {
return
}
ob.WriteString("")
if options.flags&HTML_HARD_WRAP != 0 {
for i < len(text) {
org := i
for i < len(text) && text[i] != '\n' {
i++
}
if i > org {
ob.Write(text[org:i])
}
if i >= len(text) {
break
}
ob.WriteString("
")
ob.WriteString(options.close_tag)
i++
}
} else {
ob.Write(text[i:])
}
ob.WriteString("
\n")
}
func main() {
ob := bytes.NewBuffer(nil)
input := "##Header##\n"
input += "\n"
input += "----------\n"
input += "\n"
input += "Underlined header\n"
input += "-----------------\n"
input += "\n"
input += "Some block html\n"
input += "
\n"
input += "\n"
input += "Score | Grade\n"
input += "------|------\n"
input += "94 | A\n"
input += "85 | B\n"
input += "74 | C\n"
input += "65 | D\n"
input += "\n"
input += "``` go\n"
input += "func fib(n int) int {\n"
input += " if n <= 1 {\n"
input += " return n\n"
input += " }\n"
input += " return n * fib(n-1)\n"
input += "}\n"
input += "```\n"
input += "\n"
input += "> A blockquote\n"
input += "> or something like that\n"
input += "> With a table | of two columns\n"
input += "> -------------|---------------\n"
input += "> key | value \n"
input += "\n"
input += "\n"
input += "Some **bold** Some *italic* and [a link][1] \n"
input += "\n"
input += "A little code sample\n"
input += "\n"
input += " \n"
input += " Web Page Title \n"
input += " \n"
input += "\n"
input += "A picture\n"
input += "\n"
input += "![alt text][2]\n"
input += "\n"
input += "A list\n"
input += "\n"
input += "- apples\n"
input += "- oranges\n"
input += "- eggs\n"
input += "\n"
input += "A numbered list\n"
input += "\n"
input += "1. a\n"
input += "2. b\n"
input += "3. c\n"
input += "\n"
input += "A little quote\n"
input += "\n"
input += "> It is now time for all good men to come to the aid of their country. \n"
input += "\n"
input += "A final paragraph.\n"
input += "\n"
input += " [1]: http://www.google.com\n"
input += " [2]: http://www.google.com/intl/en_ALL/images/logo.gif\n"
ib := []byte(input)
rndrer := new(mkd_renderer)
rndrer.blockcode = rndr_blockcode
rndrer.blockhtml = rndr_raw_block
rndrer.header = rndr_header
rndrer.hrule = rndr_hrule
rndrer.list = rndr_list
rndrer.listitem = rndr_listitem
rndrer.paragraph = rndr_paragraph
rndrer.table = rndr_table
rndrer.table_row = rndr_tablerow
rndrer.table_cell = rndr_tablecell
rndrer.opaque = &html_renderopts{close_tag: " />"}
var extensions uint32 = MKDEXT_FENCED_CODE | MKDEXT_TABLES
Ups_markdown(ob, ib, rndrer, extensions)
fmt.Print(ob.String())
}
func Ups_markdown(ob *bytes.Buffer, ib []byte, rndrer *mkd_renderer, extensions uint32) {
/* filling the render structure */
if rndrer == nil {
return
}
rndr := &render{*rndrer, extensions}
parse_block(ob, rndr, ib)
}