diff --git a/block.go b/block.go index b5b0841..281eb99 100644 --- a/block.go +++ b/block.go @@ -63,7 +63,7 @@ func (p *parser) block(out *bytes.Buffer, data []byte) { // % stuff // % more stuff // % even more stuff - if p.flags&EXTENSION_TITLEBLOCK != 0 { + if p.flags&Titleblock != 0 { if data[0] == '%' { if i := p.titleBlock(out, data, true); i > 0 { data = data[i:] @@ -101,7 +101,7 @@ func (p *parser) block(out *bytes.Buffer, data []byte) { // return n * fact(n-1) // } // ``` - if p.flags&EXTENSION_FENCED_CODE != 0 { + if p.flags&FencedCode != 0 { if i := p.fencedCode(out, data, true); i > 0 { data = data[i:] continue @@ -139,7 +139,7 @@ func (p *parser) block(out *bytes.Buffer, data []byte) { // ------|-----|--------- // Bob | 31 | 555-1234 // Alice | 27 | 555-4321 - if p.flags&EXTENSION_TABLES != 0 { + if p.flags&Tables != 0 { if i := p.table(out, data); i > 0 { data = data[i:] continue @@ -162,7 +162,7 @@ func (p *parser) block(out *bytes.Buffer, data []byte) { // 1. Item 1 // 2. Item 2 if p.oliPrefix(data) > 0 { - data = data[p.list(out, data, LIST_TYPE_ORDERED):] + data = data[p.list(out, data, ListTypeOrdered):] continue } @@ -174,9 +174,9 @@ func (p *parser) block(out *bytes.Buffer, data []byte) { // // Term 2 // : Definition c - if p.flags&EXTENSION_DEFINITION_LISTS != 0 { + if p.flags&DefinitionLists != 0 { if p.dliPrefix(data) > 0 { - data = data[p.list(out, data, LIST_TYPE_DEFINITION):] + data = data[p.list(out, data, ListTypeDefinition):] continue } } @@ -194,7 +194,7 @@ func (p *parser) isPrefixHeader(data []byte) bool { return false } - if p.flags&EXTENSION_SPACE_HEADERS != 0 { + if p.flags&SpaceHeaders != 0 { level := 0 for level < 6 && data[level] == '#' { level++ @@ -215,7 +215,7 @@ func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int { end := skipUntilChar(data, i, '\n') skip := end id := "" - if p.flags&EXTENSION_HEADER_IDS != 0 { + if p.flags&HeaderIDs != 0 { j, k := 0, 0 // find start/end of header id for j = i; j < end-1 && (data[j] != '{' || data[j+1] != '#'); j++ { @@ -242,7 +242,7 @@ func (p *parser) prefixHeader(out *bytes.Buffer, data []byte) int { end-- } if end > i { - if id == "" && p.flags&EXTENSION_AUTO_HEADER_IDS != 0 { + if id == "" && p.flags&AutoHeaderIDs != 0 { id = sanitized_anchor_name.Create(string(data[i:end])) } work := func() bool { @@ -484,7 +484,7 @@ func (p *parser) htmlFindEnd(tag string, data []byte) int { return i } - if p.flags&EXTENSION_LAX_HTML_BLOCKS != 0 { + if p.flags&LaxHTMLBlocks != 0 { return i } if skip = p.isEmpty(data[i:]); skip == 0 { @@ -767,7 +767,7 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns if data[i] == ':' { i++ - columns[col] |= TABLE_ALIGNMENT_LEFT + columns[col] |= TableAlignmentLeft dashes++ } for data[i] == '-' { @@ -776,7 +776,7 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns } if data[i] == ':' { i++ - columns[col] |= TABLE_ALIGNMENT_RIGHT + columns[col] |= TableAlignmentRight dashes++ } for data[i] == ' ' { @@ -913,7 +913,7 @@ func (p *parser) quote(out *bytes.Buffer, data []byte) int { // fenced code and if one's found, incorporate it altogether, // irregardless of any contents inside it for data[end] != '\n' { - if p.flags&EXTENSION_FENCED_CODE != 0 { + if p.flags&FencedCode != 0 { if i := p.fencedCode(out, data[end:], false); i > 0 { // -1 to compensate for the extra end++ after the loop: end += i - 1 @@ -1049,18 +1049,18 @@ func (p *parser) dliPrefix(data []byte) int { } // parse ordered or unordered list block -func (p *parser) list(out *bytes.Buffer, data []byte, flags int) int { +func (p *parser) list(out *bytes.Buffer, data []byte, flags ListType) int { i := 0 - flags |= LIST_ITEM_BEGINNING_OF_LIST + flags |= ListItemBeginningOfList work := func() bool { for i < len(data) { skip := p.listItem(out, data[i:], &flags) i += skip - if skip == 0 || flags&LIST_ITEM_END_OF_LIST != 0 { + if skip == 0 || flags&ListItemEndOfList != 0 { break } - flags &= ^LIST_ITEM_BEGINNING_OF_LIST + flags &= ^ListItemBeginningOfList } return true } @@ -1071,7 +1071,7 @@ func (p *parser) list(out *bytes.Buffer, data []byte, flags int) int { // Parse a single list item. // Assumes initial prefix is already removed if this is a sublist. -func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *int) int { +func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *ListType) int { // keep track of the indentation of the first line itemIndent := 0 for itemIndent < 3 && data[itemIndent] == ' ' { @@ -1086,13 +1086,13 @@ func (p *parser) listItem(out *bytes.Buffer, data []byte, flags *int) int { i = p.dliPrefix(data) // reset definition term flag if i > 0 { - *flags &= ^LIST_TYPE_TERM + *flags &= ^ListTypeTerm } } if i == 0 { // if in defnition list, set term flag and continue - if *flags&LIST_TYPE_DEFINITION != 0 { - *flags |= LIST_TYPE_TERM + if *flags&ListTypeDefinition != 0 { + *flags |= ListTypeTerm } else { return 0 } @@ -1153,7 +1153,7 @@ gatherlines: p.dliPrefix(chunk) > 0: if containsBlankLine { - *flags |= LIST_ITEM_CONTAINS_BLOCK + *flags |= ListItemContainsBlock } // to be a nested list, it must be indented more @@ -1172,16 +1172,16 @@ gatherlines: // if the header is not indented, it is not nested in the list // and thus ends the list if containsBlankLine && indent < 4 { - *flags |= LIST_ITEM_END_OF_LIST + *flags |= ListItemEndOfList break gatherlines } - *flags |= LIST_ITEM_CONTAINS_BLOCK + *flags |= ListItemContainsBlock // anything following an empty line is only part // of this item if it is indented 4 spaces // (regardless of the indentation of the beginning of the item) case containsBlankLine && indent < 4: - if *flags&LIST_TYPE_DEFINITION != 0 && i < len(data)-1 { + if *flags&ListTypeDefinition != 0 && i < len(data)-1 { // is the next item still a part of this list? next := i for data[next] != '\n' { @@ -1191,17 +1191,17 @@ gatherlines: next++ } if i < len(data)-1 && data[i] != ':' && data[next] != ':' { - *flags |= LIST_ITEM_END_OF_LIST + *flags |= ListItemEndOfList } } else { - *flags |= LIST_ITEM_END_OF_LIST + *flags |= ListItemEndOfList } break gatherlines // a blank line means this should be parsed as a block case containsBlankLine: raw.WriteByte('\n') - *flags |= LIST_ITEM_CONTAINS_BLOCK + *flags |= ListItemContainsBlock } // if this line was preceeded by one or more blanks, @@ -1222,7 +1222,7 @@ gatherlines: // render the contents of the list item var cooked bytes.Buffer - if *flags&LIST_ITEM_CONTAINS_BLOCK != 0 && *flags&LIST_TYPE_TERM == 0 { + if *flags&ListItemContainsBlock != 0 && *flags&ListTypeTerm == 0 { // intermediate render of block item, except for definition term if sublist > 0 { p.block(&cooked, rawBytes[:sublist]) @@ -1296,9 +1296,9 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int { // did we find a blank line marking the end of the paragraph? if n := p.isEmpty(current); n > 0 { // did this blank line followed by a definition list item? - if p.flags&EXTENSION_DEFINITION_LISTS != 0 { + if p.flags&DefinitionLists != 0 { if i < len(data)-1 && data[i+1] == ':' { - return p.list(out, data[prev:], LIST_TYPE_DEFINITION) + return p.list(out, data[prev:], ListTypeDefinition) } } @@ -1331,7 +1331,7 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int { }(out, p, data[prev:eol]) id := "" - if p.flags&EXTENSION_AUTO_HEADER_IDS != 0 { + if p.flags&AutoHeaderIDs != 0 { id = sanitized_anchor_name.Create(string(data[prev:eol])) } @@ -1346,7 +1346,7 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int { } // if the next line starts a block of HTML, then the paragraph ends here - if p.flags&EXTENSION_LAX_HTML_BLOCKS != 0 { + if p.flags&LaxHTMLBlocks != 0 { if data[i] == '<' && p.html(out, current, false) > 0 { // rewind to before the HTML block p.renderParagraph(out, data[:i]) @@ -1361,7 +1361,7 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int { } // if there's a fenced code block, paragraph is over - if p.flags&EXTENSION_FENCED_CODE != 0 { + if p.flags&FencedCode != 0 { if p.fencedCode(out, current, false) > 0 { p.renderParagraph(out, data[:i]) return i @@ -1369,14 +1369,14 @@ func (p *parser) paragraph(out *bytes.Buffer, data []byte) int { } // if there's a definition list item, prev line is a definition term - if p.flags&EXTENSION_DEFINITION_LISTS != 0 { + if p.flags&DefinitionLists != 0 { if p.dliPrefix(current) != 0 { - return p.list(out, data[prev:], LIST_TYPE_DEFINITION) + return p.list(out, data[prev:], ListTypeDefinition) } } // if there's a list after this, paragraph is over - if p.flags&EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK != 0 { + if p.flags&NoEmptyLineBeforeBlock != 0 { if p.uliPrefix(current) != 0 || p.oliPrefix(current) != 0 || p.quotePrefix(current) != 0 || diff --git a/block_test.go b/block_test.go index b33c257..340e507 100644 --- a/block_test.go +++ b/block_test.go @@ -18,35 +18,27 @@ import ( "testing" ) -func runMarkdownBlockWithRenderer(input string, extensions int, renderer Renderer) string { +func runMarkdownBlockWithRenderer(input string, extensions Extensions, renderer Renderer) string { return string(Markdown([]byte(input), renderer, extensions)) } -func runMarkdownBlock(input string, extensions int) string { - htmlFlags := 0 - htmlFlags |= HTML_USE_XHTML - - renderer := HtmlRenderer(htmlFlags, "", "") - +func runMarkdownBlock(input string, extensions Extensions) string { + renderer := HtmlRenderer(UseXHTML, "", "") return runMarkdownBlockWithRenderer(input, extensions, renderer) } -func runnerWithRendererParameters(parameters HtmlRendererParameters) func(string, int) string { - return func(input string, extensions int) string { - htmlFlags := 0 - htmlFlags |= HTML_USE_XHTML - - renderer := HtmlRendererWithParameters(htmlFlags, "", "", parameters) - +func runnerWithRendererParameters(parameters HtmlRendererParameters) func(string, Extensions) string { + return func(input string, extensions Extensions) string { + renderer := HtmlRendererWithParameters(UseXHTML, "", "", parameters) return runMarkdownBlockWithRenderer(input, extensions, renderer) } } -func doTestsBlock(t *testing.T, tests []string, extensions int) { +func doTestsBlock(t *testing.T, tests []string, extensions Extensions) { doTestsBlockWithRunner(t, tests, extensions, runMarkdownBlock) } -func doTestsBlockWithRunner(t *testing.T, tests []string, extensions int, runner func(string, int) string) { +func doTestsBlockWithRunner(t *testing.T, tests []string, extensions Extensions, runner func(string, Extensions) string) { // catch and report panics var candidate string defer func() { @@ -203,7 +195,7 @@ func TestPrefixHeaderSpaceExtension(t *testing.T) { "
List
\n\nNested list
\n\n" + "List
\n\nNested list
\n\n" + "Text 2
\n", } - doTestsBlock(t, tests, EXTENSION_DEFINITION_LISTS) + doTestsBlock(t, tests, DefinitionLists) } func TestPreformattedHtml(t *testing.T) { @@ -977,7 +969,7 @@ func TestPreformattedHtmlLax(t *testing.T) { "Paragraph\n\nParagraph
\n\nAnd here?
\n", } - doTestsBlock(t, tests, EXTENSION_LAX_HTML_BLOCKS) + doTestsBlock(t, tests, LaxHTMLBlocks) } func TestFencedCodeBlock(t *testing.T) { @@ -1063,7 +1055,7 @@ func TestFencedCodeBlock(t *testing.T) { "Some text before a fenced code block\n``` oz\ncode blocks breakup paragraphs\n```\nSome text in between\n``` oz\nmultiple code blocks work okay\n```\nAnd some text after a fenced code block", "Some text before a fenced code block
\n\ncode blocks breakup paragraphs\n\n\nSome text in between
\n\nmultiple code blocks work okay\n\n\nAnd some text after a fenced code block
\n", } - doTestsBlock(t, tests, EXTENSION_FENCED_CODE) + doTestsBlock(t, tests, FencedCode) } func TestFencedCodeInsideBlockquotes(t *testing.T) { @@ -1175,7 +1167,7 @@ okay tests = append(tests, forms[0], want) tests = append(tests, forms[1], want) - doTestsBlock(t, tests, EXTENSION_FENCED_CODE) + doTestsBlock(t, tests, FencedCode) } func TestTable(t *testing.T) { @@ -1222,7 +1214,7 @@ func TestTable(t *testing.T) { "a|b\\|c|d\n---|---|---\nf|g\\|h|i\n", "| a | \nb|c | \nd | \n
|---|---|---|
| f | \ng|h | \ni | \n
List
\n\nnormal text
\n\n``` oz\n\n\nleading spaces
\n\n```\n\n",
}
- doTestsBlock(t, tests, EXTENSION_FENCED_CODE|EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK)
+ doTestsBlock(t, tests, FencedCode|NoEmptyLineBeforeBlock)
}
func TestTitleBlock_EXTENSION_TITLEBLOCK(t *testing.T) {
@@ -1514,7 +1506,7 @@ func TestTitleBlock_EXTENSION_TITLEBLOCK(t *testing.T) {
"Yep, more here too\n" +
"",
}
- doTestsBlock(t, tests, EXTENSION_TITLEBLOCK)
+ doTestsBlock(t, tests, Titleblock)
}
func TestBlockComments(t *testing.T) {
diff --git a/html.go b/html.go
index 7d37183..d4c7efb 100644
--- a/html.go
+++ b/html.go
@@ -79,7 +79,7 @@ type HtmlRendererParameters struct {
//
// Do not create this directly, instead use the HtmlRenderer function.
type Html struct {
- flags int // HTML_* options
+ flags HtmlFlags
closeTag string // how to end singleton tags: either " />" or ">"
title string // document title
css string // optional css file url (used with HTML_COMPLETE_PAGE)
@@ -106,19 +106,19 @@ const (
// HtmlRenderer creates and configures an Html object, which
// satisfies the Renderer interface.
//
-// flags is a set of HTML_* options ORed together.
+// flags is a set of HtmlFlags ORed together.
// title is the title of the document, and css is a URL for the document's
// stylesheet.
// title and css are only used when HTML_COMPLETE_PAGE is selected.
-func HtmlRenderer(flags int, title string, css string) Renderer {
+func HtmlRenderer(flags HtmlFlags, title string, css string) Renderer {
return HtmlRendererWithParameters(flags, title, css, HtmlRendererParameters{})
}
-func HtmlRendererWithParameters(flags int, title string,
+func HtmlRendererWithParameters(flags HtmlFlags, title string,
css string, renderParameters HtmlRendererParameters) Renderer {
// configure the rendering engine
closeTag := htmlClose
- if flags&HTML_USE_XHTML != 0 {
+ if flags&UseXHTML != 0 {
closeTag = xhtmlClose
}
@@ -190,7 +190,7 @@ func entityEscapeWithSkip(out *bytes.Buffer, src []byte, skipRanges [][]int) {
attrEscape(out, src[end:])
}
-func (options *Html) GetFlags() int {
+func (options *Html) GetFlags() HtmlFlags {
return options.flags
}
@@ -206,7 +206,7 @@ func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id s
marker := out.Len()
doubleSpace(out)
- if id == "" && options.flags&HTML_TOC != 0 {
+ if id == "" && options.flags&Toc != 0 {
id = fmt.Sprintf("toc_%d", options.headerCount)
}
@@ -233,7 +233,7 @@ func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id s
}
// are we building a table of contents?
- if options.flags&HTML_TOC != 0 {
+ if options.flags&Toc != 0 {
options.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id)
}
@@ -241,7 +241,7 @@ func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id s
}
func (options *Html) BlockHtml(out *bytes.Buffer, text []byte) {
- if options.flags&HTML_SKIP_HTML != 0 {
+ if options.flags&SkipHTML != 0 {
return
}
@@ -314,11 +314,11 @@ func (options *Html) TableRow(out *bytes.Buffer, text []byte) {
func (options *Html) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
doubleSpace(out)
switch align {
- case TABLE_ALIGNMENT_LEFT:
+ case TableAlignmentLeft:
out.WriteString("