Permalink
Browse files

Implement inline HTML

Implement extensions

Clean up some rules
  • Loading branch information...
1 parent 0b75cec commit 32afcd5332b8ac7bfe227295b0d1f79c2c61def7 @drbrain drbrain committed Oct 30, 2011
Showing with 190 additions and 31 deletions.
  1. +167 −30 pegdown.kpeg
  2. +23 −1 test/test_pegdown.rb
View
@@ -1,6 +1,38 @@
%% name = Pegdown
%% {
+
+ EXTENSIONS = []
+
+ def self.extension name
+ EXTENSIONS << name
+
+ eval <<-RUBY
+ def #{name}?
+ extension? __method__
+ end
+
+ def #{name}= enable
+ extension __method__, enable
+ end
+ RUBY
+ end
+
+ ##
+ # Allow HTML
+
+ extension :html
+
+ ##
+ # Use the notes extension?
+
+ extension :notes
+
+ ##
+ # Use the smart typography extension?
+
+ extension :smart_typography
+
def self.parse text
parser = new text
@@ -9,9 +41,37 @@
parser.result
end
- def extension code
- false
+ def initialize extensions = [], debug = false
+ @extensions = extensions
+ @debug = debug
+ end
+
+ def extension? name
+ name = name.to_s.delete('?').intern
+
+ @extensions.include? name
+ end
+
+ def extension name, enable
+ name = name.to_s.delete('=').intern
+
+ if enable then
+ @extensions |= [name]
+ else
+ @extensions -= [name]
+ end
+ end
+
+ alias peg_parse parse
+
+ def parse markdown
+ setup_parser markdown, @debug
+
+ peg_parse
+
+ result
end
+
}
root = Doc
@@ -319,12 +379,76 @@ HtmlBlock = < ( HtmlBlockInTags | HtmlComment | HtmlBlockSelfClosing ) >
HtmlBlockSelfClosing = "<" Spnl HtmlBlockType Spnl HtmlAttribute* "/" Spnl ">"
-HtmlBlockType = "address" | "blockquote" | "center" | "dir" | "div" | "dl" | "fieldset" | "form" | "h1" | "h2" | "h3" |
- "h4" | "h5" | "h6" | "hr" | "isindex" | "menu" | "noframes" | "noscript" | "ol" | "p" | "pre" | "table" |
- "ul" | "dd" | "dt" | "frameset" | "li" | "tbody" | "td" | "tfoot" | "th" | "thead" | "tr" | "script" |
- "ADDRESS" | "BLOCKQUOTE" | "CENTER" | "DIR" | "DIV" | "DL" | "FIELDSET" | "FORM" | "H1" | "H2" | "H3" |
- "H4" | "H5" | "H6" | "HR" | "ISINDEX" | "MENU" | "NOFRAMES" | "NOSCRIPT" | "OL" | "P" | "PRE" | "TABLE" |
- "UL" | "DD" | "DT" | "FRAMESET" | "LI" | "TBODY" | "TD" | "TFOOT" | "TH" | "THEAD" | "TR" | "SCRIPT"
+HtmlBlockType = "ADDRESS" |
+ "BLOCKQUOTE" |
+ "CENTER" |
+ "DD" |
+ "DIR" |
+ "DIV" |
+ "DL" |
+ "DT" |
+ "FIELDSET" |
+ "FORM" |
+ "FRAMESET" |
+ "H1" |
+ "H2" |
+ "H3" |
+ "H4" |
+ "H5" |
+ "H6" |
+ "HR" |
+ "ISINDEX" |
+ "LI" |
+ "MENU" |
+ "NOFRAMES" |
+ "NOSCRIPT" |
+ "OL" |
+ "P" |
+ "PRE" |
+ "SCRIPT" |
+ "TABLE" |
+ "TBODY" |
+ "TD" |
+ "TFOOT" |
+ "TH" |
+ "THEAD" |
+ "TR" |
+ "UL" |
+ "address" |
+ "blockquote" |
+ "center" |
+ "dd" |
+ "dir" |
+ "div" |
+ "dl" |
+ "dt" |
+ "fieldset" |
+ "form" |
+ "frameset" |
+ "h1" |
+ "h2" |
+ "h3" |
+ "h4" |
+ "h5" |
+ "h6" |
+ "hr" |
+ "isindex" |
+ "li" |
+ "menu" |
+ "noframes" |
+ "noscript" |
+ "ol" |
+ "p" |
+ "pre" |
+ "script" |
+ "table" |
+ "tbody" |
+ "td" |
+ "tfoot" |
+ "th" |
+ "thead" |
+ "tr" |
+ "ul"
StyleOpen = "<" Spnl ("style" | "STYLE") Spnl HtmlAttribute* ">"
StyleClose = "<" Spnl "/" ("style" | "STYLE") Spnl ">"
@@ -372,7 +496,7 @@ Str = StartList:a < NormalChar+ > { a = text } # raise " a = cons(mk_str(yytext)
StrChunk = < (NormalChar | "_"+ &Alphanumeric)+ > { raise " $$ = mk_str(yytext); " } |
AposChunk
-AposChunk = &{ extension(:EXT_SMART) } "'" &Alphanumeric
+AposChunk = &{ smart_typography? } "'" &Alphanumeric
{ raise " $$ = mk_element(APOSTROPHE); " }
EscapedChar = "\\" !Newline < /[\`|*_{}\[\]()#+.!><-]/ >
@@ -521,7 +645,7 @@ Reference = NonindentSpace !"[]" Label:l ":" Spnl RefSrc:s RefTitle:t BlankLine+
free(l);
$$->key = REFERENCE; '}
-Label = "[" ( !"^" &{ raise " extension(EXT_NOTES) " } | &. &{ raise "!extension(EXT_NOTES) " } )
+Label = "[" ( !"^" &{ raise " notes? " } | &. &{ raise "!notes? " } )
StartList:a
( !"]" Inline { raise " a = cons($$, a); " } )*
"]"
@@ -552,22 +676,35 @@ Ticks3 = "```" !"`"
Ticks4 = "````" !"`"
Ticks5 = "`````" !"`"
-Code = ( Ticks1 Sp < ( ( !"`" Nonspacechar )+ | !Ticks1 "`"+ | !( Sp Ticks1 ) ( Spacechar | Newline !BlankLine ) )+ > Sp Ticks1
- | Ticks2 Sp < ( ( !"`" Nonspacechar )+ | !Ticks2 "`"+ | !( Sp Ticks2 ) ( Spacechar | Newline !BlankLine ) )+ > Sp Ticks2
- | Ticks3 Sp < ( ( !"`" Nonspacechar )+ | !Ticks3 "`"+ | !( Sp Ticks3 ) ( Spacechar | Newline !BlankLine ) )+ > Sp Ticks3
- | Ticks4 Sp < ( ( !"`" Nonspacechar )+ | !Ticks4 "`"+ | !( Sp Ticks4 ) ( Spacechar | Newline !BlankLine ) )+ > Sp Ticks4
- | Ticks5 Sp < ( ( !"`" Nonspacechar )+ | !Ticks5 "`"+ | !( Sp Ticks5 ) ( Spacechar | Newline !BlankLine ) )+ > Sp Ticks5
+Code = ( Ticks1 Sp < (
+ ( !"`" Nonspacechar )+ | !Ticks1 "`"+ |
+ !( Sp Ticks1 ) ( Spacechar | Newline !BlankLine )
+ )+ > Sp Ticks1 |
+ Ticks2 Sp < (
+ ( !"`" Nonspacechar )+ |
+ !Ticks2 "`"+ |
+ !( Sp Ticks2 ) ( Spacechar | Newline !BlankLine )
+ )+ > Sp Ticks2 |
+ Ticks3 Sp < (
+ ( !"`" Nonspacechar )+ |
+ !Ticks3 "`"+ |
+ !( Sp Ticks3 ) ( Spacechar | Newline !BlankLine )
+ )+ > Sp Ticks3 |
+ Ticks4 Sp < (
+ ( !"`" Nonspacechar )+ |
+ !Ticks4 "`"+ |
+ !( Sp Ticks4 ) ( Spacechar | Newline !BlankLine )
+ )+ > Sp Ticks4 |
+ Ticks5 Sp < (
+ ( !"`" Nonspacechar )+ |
+ !Ticks5 "`"+ |
+ !( Sp Ticks5 ) ( Spacechar | Newline !BlankLine )
+ )+ > Sp Ticks5
)
{ raise " $$ = mk_str(yytext); $$->key = CODE; " }
-RawHtml = < (HtmlComment | HtmlBlockScript | HtmlTag) >
- { raise 'if (extension(EXT_FILTER_HTML)) {
- $$ = mk_list(LIST, NULL);
- } else {
- $$ = mk_str(yytext);
- $$->key = HTML;
- }'
- }
+RawHtml = < (HtmlComment | HtmlBlockScript | HtmlTag) >
+ { if html? then text else '' end }
BlankLine = Sp Newline { "\n" }
@@ -584,7 +721,7 @@ Spnl = Sp (Newline Sp)?
SpecialChar = "*" | "_" | "`" | "&" | "[" | "]" | "(" | ")" | "<" | "!" | "#" | "\\" | "'" | "\"" | ExtendedSpecialChar
NormalChar = !( SpecialChar | Spacechar | Newline ) .
NonAlphanumeric = /[\000-\057\072-\100\133-\140\173-\177]/
-Alphanumeric = /[0-9A-Za-z]/ | "\200" | "\201" | "\202" | "\203" | "\204" | "\205" | "\206" | "\207" | "\210" | "\211" | "\212" | "\213" | "\214" | "\215" | "\216" | "\217" | "\220" | "\221" | "\222" | "\223" | "\224" | "\225" | "\226" | "\227" | "\230" | "\231" | "\232" | "\233" | "\234" | "\235" | "\236" | "\237" | "\240" | "\241" | "\242" | "\243" | "\244" | "\245" | "\246" | "\247" | "\250" | "\251" | "\252" | "\253" | "\254" | "\255" | "\256" | "\257" | "\260" | "\261" | "\262" | "\263" | "\264" | "\265" | "\266" | "\267" | "\270" | "\271" | "\272" | "\273" | "\274" | "\275" | "\276" | "\277" | "\300" | "\301" | "\302" | "\303" | "\304" | "\305" | "\306" | "\307" | "\310" | "\311" | "\312" | "\313" | "\314" | "\315" | "\316" | "\317" | "\320" | "\321" | "\322" | "\323" | "\324" | "\325" | "\326" | "\327" | "\330" | "\331" | "\332" | "\333" | "\334" | "\335" | "\336" | "\337" | "\340" | "\341" | "\342" | "\343" | "\344" | "\345" | "\346" | "\347" | "\350" | "\351" | "\352" | "\353" | "\354" | "\355" | "\356" | "\357" | "\360" | "\361" | "\362" | "\363" | "\364" | "\365" | "\366" | "\367" | "\370" | "\371" | "\372" | "\373" | "\374" | "\375" | "\376" | "\377"
+Alphanumeric = /[0-9A-Za-z\200-\377]/
AlphanumericAscii = /[A-Za-z0-9]/
Digit = [0-9]
BOM = "\357\273\277"
@@ -611,10 +748,10 @@ SkipBlock = ( !BlankLine RawLine )+ BlankLine*
# Syntax extensions
-ExtendedSpecialChar = &{ extension(:EXT_SMART) } ("." | "-" | "'" | "\"")
- | &{ extension(:EXT_NOTES) } ( "^" )
+ExtendedSpecialChar = &{ smart_typography? } ("." | "-" | "'" | "\"")
+ | &{ notes? } ( "^" )
-Smart = &{ extension(:EXT_SMART) }
+Smart = &{ smart_typography? }
( Ellipsis | Dash | SingleQuoted | DoubleQuoted | Apostrophe )
Apostrophe = "'"
@@ -651,7 +788,7 @@ DoubleQuoted = DoubleQuoteStart
DoubleQuoteEnd
{ raise " $$ = mk_list(DOUBLEQUOTED, a); " }
-NoteReference = &{ extension(:EXT_NOTES) }
+NoteReference = &{ notes? }
RawNoteReference:ref
{ raise 'element *match;
if (find_note(&match, ref->contents.str)) {
@@ -671,7 +808,7 @@ NoteReference = &{ extension(:EXT_NOTES) }
RawNoteReference = "[^" < ( !Newline !"]" . )+ > "]"
{ raise " $$ = mk_str(yytext); " }
-Note = &{ extension(:EXT_NOTES) }
+Note = &{ notes? }
NonindentSpace ref:rawNoteReference ":" Sp
StartList:a
( RawNoteBlock { raise " a = cons($$, a); " } )
@@ -680,7 +817,7 @@ Note = &{ extension(:EXT_NOTES) }
$$->contents.str = strdup(ref->contents.str); '
}
-InlineNote = &{ extension(:EXT_NOTES) }
+InlineNote = &{ notes? }
"^["
StartList:a
( !"]" Inline { raise " a = cons($$, a); " } )+
View
@@ -10,6 +10,8 @@ class TestPegdown < MiniTest::Unit::TestCase
def setup
@RM = RDoc::Markup
+
+ @parser = Pegdown.new
end
def mu_pp obj
@@ -105,6 +107,26 @@ def test_parse_heading_setext_equals
assert_equal expected, doc
end
+ def test_parse_html_address
+ @parser.html = true
+
+ doc = parse "<address>Links here</address>"
+
+ expected = @RM::Document.new(
+ @RM::Paragraph.new("<address>Links here</address>"))
+
+ assert_equal expected, doc
+ end
+
+ def test_parse_html_address_no
+ doc = parse "<address>Links here</address>"
+
+ expected = @RM::Document.new(
+ @RM::Paragraph.new("Links here"))
+
+ assert_equal expected, doc
+ end
+
def test_parse_list_bullet
doc = parse <<-MD
* one
@@ -276,7 +298,7 @@ def test_parse_verbatim
end
def parse text
- Pegdown.parse text
+ @parser.parse text
end
end

0 comments on commit 32afcd5

Please sign in to comment.