lib/arti_mark/parser.kpeg in arti_mark-0.1.beta3 vs lib/arti_mark/parser.kpeg in arti_mark-0.1.beta5
- old
+ new
@@ -36,21 +36,34 @@
blockhead = lh - command:command - '{' - nl empty_line* { command }
blockend = lh - '}' - le empty_line*
blockbody = (!blockend block)+:body { body }
explicit_block = < blockhead:head - blockbody:body - blockend > { create_item(:block, head, body, raw: text) }
+# preformatted block
+preformatted_command = command:command &{ ['pre', 'precode'].include? command[:name] }
+preformatted_command_head = lh - preformatted_command:command - '<<' &/[\w0-9]/ { command }
+preformat_end(start) = lh word:delimiter &{ delimiter == start }
+preformatted_block = < lh - preformatted_command_head:command !nl word:delimiter nl (!preformat_end(delimiter) (lh charstring nl))+:content preformat_end(delimiter) > { create_item(:preformatted, command, content, raw: text) }
+
# inline command
inline = img_inline | common_inline
common_inline = < '[' command:c '{' documentcontent_except('}'):content '}' ']' > { create_item(:inline, c, content, raw: text) }
img_command = command:c &{ c[:name] == 'img' && c[:args].size == 2}
img_inline = < '[' img_command:c ']' > { create_item(:inline, c, nil, raw: text) }
# special line commands
+commandname_for_special_line_command = newpage_command | explicit_paragraph_command
+
+# newpage
+newpage_command = command:command &{ command[:name] == 'newpage' }
+newpage = < lh - newpage_command:c ':' documentcontent?:content - nl > { create_item(:newpage, c, content, raw:text) }
+
# explicit paragraph
explicit_paragraph_command = command:c &{ c[:name] == 'p' }
explicit_paragraph = < lh - explicit_paragraph_command:c ':' documentcontent?:content le empty_line*> { create_item(:paragraph, c, content, raw:text) }
+
# unordered list
unordered_list = < unordered_item+:items > { create_item(:ul, nil, items, raw: text) }
unordered_item = < lh '*:' documentcontent:content le > { create_item(:li, nil, content, raw: text) }
# ordered list
@@ -63,33 +76,48 @@
items_list = unordered_list | ordered_list | definition_list
# generic line command
-line_command = < lh - !explicit_paragraph_command command:c ':' documentcontent?:content - le empty_line* > { create_item(:line_command, c, content, raw: text) }
+line_command = < lh - !commandname_for_special_line_command command:c ':' documentcontent?:content - le empty_line* > { create_item(:line_command, c, content, raw: text) }
# blocks
-line_block = items_list | line_command
-block = (line_block | explicit_block | paragraph_group):block empty_line* {block}
+line_block = items_list | line_command
+block = (preformatted_block | headed_section | line_block | explicit_block | paragraph_group ):block empty_line* {block}
block_delimiter = blockhead | blockend
-paragraph_delimiter = block_delimiter | line_block
+paragraph_delimiter = block_delimiter | preformatted_command_head | line_block | newpage | headed_start
+# markdown-style headings
+h_start_mark(n) = < '='+ ':' > &{ text.length - 1 == n }
+h_markup_terminator(n) = lh - < '='+ ':' > &{ text.length - 1 <= n }
+h_start(n) = lh - h_start_mark(n) charstring:s le { { level: n, heading: s } }
+h_section(n) = < h_start(n):h (!h_markup_terminator(n) !eof block)+:content > { create_item(:h_section, h, content, raw: text) }
+
+headed_start = h_start(1) | h_start(2) | h_start(3) | h_start(4) | h_start(5) | h_start(6)
+headed_section = h_section(1) | h_section(2)| h_section(3)| h_section(4)| h_section(5) | h_section(6)
+
#header
stylesheets = < lh - 'stylesheets:' !le charstring:s nl > { create_item(:stylesheets, {:stylesheets => s.split(',').map(&:strip)}, nil, raw:text) }
title = < lh - 'title:' !le charstring:t nl > { create_item(:title, {:title => t }, nil, raw:text) }
lang = < lh - 'lang:' !le charstring:l nl > { create_item(:lang, {:lang => l }, nil, raw:text) }
-header = (stylesheets | title | lang) empty_line*
+paragraph_style = < lh - 'paragraph-style:' !le charstring:l nl > { create_item(:paragraph_style, {:paragraph_style => l }, nil, raw:text) }
+header = (stylesheets | title | lang | paragraph_style ) empty_line*
# texts
char = < /[[:print:]]/ > { text }
charstring = < char* > { text }
char_except(e) = char:c &{ c != e }
charstring_except(e) = < char_except(e)* > { text }
documentcontent_except(e) = (inline | !inline char_except(e))+:content ~parse_text(content)
documentcontent = (inline | !inline char)+:content ~parse_text(content)
documentline = lh documentcontent:content le { content }
+#page
+headers = header*:headers { create_item(:headers, nil, headers) }
+page = headers:headers - (!newpage block)*:blocks { create_item(:page, nil, [headers] + blocks.select{ |x| !x.nil?}) }
+newpaged_page = newpage:newpage page:page { page[:children] = page[:children].unshift newpage; page }
#root
-root = header*:headers - block*:blocks - eof_comment? eof { headers.concat blocks.select{ |x| !x.nil?} }
+root = page:page newpaged_page*:pages - eof_comment? eof { [ page ] + pages }
+