replace extension flags by Extension, a struct of boolean values

This commit is contained in:
Michael Teichgräber 2010-11-24 19:56:25 +01:00
parent 071bf92c4f
commit c5747337a9
5 changed files with 33 additions and 34 deletions

View File

@ -26,12 +26,9 @@ func main() {
b, _ = ioutil.ReadAll(os.Stdin)
}
e := 0
if *optNotes {
e |= md.EXT_NOTES
}
if *optSmart {
e |= md.EXT_SMART
e := md.Extensions{
Notes: *optNotes,
Smart: *optSmart,
}
doc := md.Parse(string(b), e)

2
doc.go
View File

@ -13,7 +13,7 @@ Usage example:
func main() {
b, _ := ioutil.ReadAll(os.Stdin)
doc := md.Parse(string(b), md.EXT_SMART)
doc := md.Parse(string(b), md.Extensions{Smart: true})
w := bufio.NewWriter(os.Stdout)
doc.WriteHtml(w)

View File

@ -26,17 +26,19 @@ import (
)
// Markdown Extensions:
const (
EXT_SMART = 1 << iota
EXT_NOTES
EXT_FILTER_HTML
EXT_FILTER_STYLES
)
type Extensions struct {
Smart bool
Notes bool
FilterHTML bool
FilterStyles bool
Dlists bool
}
// Parse converts a Markdown document into a tree for later output processing.
func Parse(text string, extFlags int) *Doc {
func Parse(text string, ext Extensions) *Doc {
d := new(Doc)
d.syntaxExtensions = extFlags
d.extension = ext
d.parser = new(yyParser)
d.parser.Doc = d
@ -45,7 +47,7 @@ func Parse(text string, extFlags int) *Doc {
s := preformat(text)
d.parseRule(ruleReferences, s)
if extFlags&EXT_NOTES != 0 {
if ext.Notes {
d.parseRule(ruleNotes, s)
}
raw := d.parseMarkdown(s)

View File

@ -23,6 +23,12 @@ s,->contents.str,.contents.str,g
/EXT/ s,if extension,if p.extension,
/EXT/ s,{ *extension,{ p.extension,g
/EXT/ s,{ *!extension,{ !p.extension,g
/EXT/ {
s,extension.EXT_FILTER_HTML.,extension.FilterHTML,g
s,extension.EXT_FILTER_STYLES.,extension.FilterStyles,g
s,extension.EXT_SMART.,extension.Smart,g
s,extension.EXT_NOTES.,extension.Notes,g
}
s,{ *element \*[a-z]*\; *$,{,

View File

@ -87,12 +87,12 @@ const (
)
type Doc struct {
parser *yyParser
parser *yyParser
extension Extensions
tree *element /* Results of parse. */
references *element /* List of link references found. */
notes *element /* List of footnotes found. */
syntaxExtensions int /* Syntax extensions selected. */
}
%}
@ -370,7 +370,7 @@ HtmlBlockInTags = HtmlBlockOpenAddress (HtmlBlockInTags | !HtmlBlockCloseAddress
HtmlBlock = < ( HtmlBlockInTags | HtmlComment | HtmlBlockSelfClosing ) >
BlankLine+
{ if p.extension(EXT_FILTER_HTML) {
{ if p.extension.FilterHTML {
$$ = mk_list(LIST, nil)
} else {
$$ = mk_str(yytext)
@ -392,7 +392,7 @@ StyleClose = '<' Spnl '/' ("style" | "STYLE") Spnl '>'
InStyleTags = StyleOpen (!StyleClose .)* StyleClose
StyleBlock = < InStyleTags >
BlankLine*
{ if p.extension(EXT_FILTER_STYLES) {
{ if p.extension.FilterStyles {
$$ = mk_list(LIST, nil)
} else {
$$ = mk_str(yytext)
@ -565,7 +565,7 @@ Reference = NonindentSpace !"[]" l:Label ':' Spnl s:RefSrc Spnl t:RefTitle Blank
l = nil
$$.key = REFERENCE }
Label = '[' ( !'^' &{ p.extension(EXT_NOTES) } | &. &{ !p.extension(EXT_NOTES) } )
Label = '[' ( !'^' &{ p.extension.Notes } | &. &{ !p.extension.Notes } )
a:StartList
( !']' Inline { a = cons($$, a) } )*
']'
@ -606,7 +606,7 @@ Code = ( Ticks1 Sp < ( ( !'`' Nonspacechar )+ | !Ticks1 '`'+ | !( Sp Ticks1 ) (
{ $$ = mk_str(yytext); $$.key = CODE }
RawHtml = < (HtmlComment | HtmlTag) >
{ if p.extension(EXT_FILTER_HTML) {
{ if p.extension.FilterHTML {
$$ = mk_list(LIST, nil)
} else {
$$ = mk_str(yytext)
@ -653,10 +653,10 @@ SkipBlock = ( !BlankLine RawLine )+ BlankLine*
# Syntax extensions
ExtendedSpecialChar = &{ p.extension(EXT_SMART) } ('.' | '-' | '\'' | '"')
| &{ p.extension(EXT_NOTES) } ( '^' )
ExtendedSpecialChar = &{ p.extension.Smart } ('.' | '-' | '\'' | '"')
| &{ p.extension.Notes } ( '^' )
Smart = &{ p.extension(EXT_SMART) }
Smart = &{ p.extension.Smart }
( Ellipsis | Dash | SingleQuoted | DoubleQuoted | Apostrophe )
Apostrophe = '\''
@ -693,7 +693,7 @@ DoubleQuoted = DoubleQuoteStart
DoubleQuoteEnd
{ $$ = mk_list(DOUBLEQUOTED, a) }
NoteReference = &{ p.extension(EXT_NOTES) }
NoteReference = &{ p.extension.Notes }
ref:RawNoteReference
{
if match, ok := p.find_note(ref.contents.str); ok {
@ -708,7 +708,7 @@ NoteReference = &{ p.extension(EXT_NOTES) }
RawNoteReference = "[^" < ( !Newline !']' . )+ > ']'
{ $$ = mk_str(yytext) }
Note = &{ p.extension(EXT_NOTES) }
Note = &{ p.extension.Notes }
NonindentSpace ref:RawNoteReference ':' Sp
a:StartList
( RawNoteBlock { a = cons($$, a) } )
@ -717,7 +717,7 @@ Note = &{ p.extension(EXT_NOTES) }
$$.contents.str = ref.contents.str
}
InlineNote = &{ p.extension(EXT_NOTES) }
InlineNote = &{ p.extension.Notes }
"^["
a:StartList
( !']' Inline { a = cons($$, a) } )+
@ -828,12 +828,6 @@ func mk_link(label *element, url, title string) *element {
}
/* extension = returns true if extension is selected
*/
func (d *Doc) extension(ext int) bool {
return d.syntaxExtensions&ext != 0
}
/* match_inlines - returns true if inline lists match (case-insensitive...)
*/
func match_inlines(l1, l2 *element) bool {