Skip to content

Commit

Permalink
feat(parser/renderer): support list separation (#274)
Browse files Browse the repository at this point in the history
when a list item with an attribute has one
or more blank lines before the previous item,
then it is the first item of a new list

also works when a blank line is followed by a single comment line.

Fixes #263
Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Jan 19, 2019
1 parent 34598af commit d2945ab
Show file tree
Hide file tree
Showing 9 changed files with 46,568 additions and 37,630 deletions.
44 changes: 25 additions & 19 deletions pkg/parser/asciidoc-grammar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ DocumentElement <- !EOF // when reaching EOF, do not try to parse a new document
/ ListingBlock
/ ExampleBlock
/ CommentBlock
/ SingleLineComment
/ QuoteBlock
/ SidebarBlock
/ Table
Expand Down Expand Up @@ -369,10 +370,6 @@ Section2TitlePrefix <- "===" WS+

Section2Title <- Section2TitlePrefix elements:(TitleElements) id:(InlineElementID*) EOL {
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
}

Section2Element <- !Section1TitlePrefix !Section2TitlePrefix
Expand All @@ -389,10 +386,6 @@ Section3TitlePrefix <- "====" WS+

Section3Title <- Section3TitlePrefix elements:(TitleElements) id:(InlineElementID*) EOL {
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
return types.NewSectionTitle(elements.(types.InlineElements), id.([]interface{}))
}

Section3Element <- !Section1TitlePrefix !Section2TitlePrefix !Section3TitlePrefix
Expand Down Expand Up @@ -456,14 +449,27 @@ List <-
return types.NewList(elements.([]interface{}))
}

ListItems <- (OrderedListItem / UnorderedListItem / LabeledListItem)+
ListItems <- ListItem+

ListParagraph <- lines:(ListParagraphLine)+ {
return types.NewParagraph(lines.([]interface{}))
}
ListItem <-
// list items can be separated with one or more blank lines,
// but as soon as an element attribute is set after one or more blank lines,
// then the item begins a new list
(!BlankLine &ElementAttribute? / !BlankLine &SingleLineComment? / BlankLine+ !ElementAttribute)
attributes:(ElementAttribute)*
item:(OrderedListItem / UnorderedListItem / LabeledListItem) {
return types.WithAttributes(item, attributes.([]interface{}))
}

ListParagraph <- comment:(SingleLineComment) {
return comment, nil
} / lines:(ListParagraphLine)+ {
return types.NewParagraph(lines.([]interface{}))
}

ListParagraphLine <-
!BlankLine
!SingleLineComment
!OrderedListItemPrefix
!UnorderedListItemPrefix
!(LabeledListItemTerm LabeledListItemSeparator)
Expand All @@ -473,7 +479,7 @@ ListParagraphLine <-
line:(
elements:(InlineElement)+ linebreak:(LineBreak)? { // absorbs heading and trailing spaces
return types.NewInlineElements(append(elements.([]interface{}), linebreak))
}) EOL { // EOL may have been "swallowed" by 'linebreak rule'
}) EOL {
return line, nil
}

Expand All @@ -488,8 +494,8 @@ ContinuedDocumentElement<- ListItemContinuation element:DocumentElement{
// ------------------------------------------
// Ordered List Items
// ------------------------------------------
OrderedListItem <- attributes:(ElementAttribute)* prefix:(OrderedListItemPrefix) content:(OrderedListItemContent) BlankLine* {
return types.NewOrderedListItem(prefix.(types.OrderedListItemPrefix), content.([]interface{}), attributes.([]interface{}))
OrderedListItem <- prefix:(OrderedListItemPrefix) content:(OrderedListItemContent) {
return types.NewOrderedListItem(prefix.(types.OrderedListItemPrefix), content.([]interface{}))
}

OrderedListItemPrefix <- WS* prefix:(
Expand Down Expand Up @@ -526,7 +532,7 @@ OrderedListItemContent <- elements:(ListParagraph+ ContinuedDocumentElement*) {
// ------------------------------------------
// Unordered List Items
// ------------------------------------------
UnorderedListItem <- prefix:(UnorderedListItemPrefix) checkstyle:(UnorderedListItemCheckStyle)? content:(UnorderedListItemContent) BlankLine* {
UnorderedListItem <- prefix:(UnorderedListItemPrefix) checkstyle:(UnorderedListItemCheckStyle)? content:(UnorderedListItemContent) {
return types.NewUnorderedListItem(prefix.(types.UnorderedListItemPrefix), checkstyle, content.([]interface{}))
}

Expand Down Expand Up @@ -1010,7 +1016,7 @@ FencedBlock <- FencedBlockDelimiter content:(FencedBlockContent)* (FencedBlockDe
return types.NewDelimitedBlock(types.Fenced, content.([]interface{}), types.None)
}

FencedBlockContent <- List / BlockParagraph / BlankLine
FencedBlockContent <- BlankLine / List / BlockParagraph

// -------------------------------------------------------------------------------------
// Listing blocks
Expand Down Expand Up @@ -1041,7 +1047,7 @@ ListingBlockLineContent <- (Alphanums / Spaces / (!ListingBlockDelimiter !EOL .
// -------------------------------------------------------------------------------------
ExampleBlockDelimiter <- "====" WS* EOL

ExampleBlock <- ExampleBlockDelimiter content:(List / BlockParagraph / BlankLine)* (ExampleBlockDelimiter / EOF) {
ExampleBlock <- ExampleBlockDelimiter content:(BlankLine / List / BlockParagraph)* (ExampleBlockDelimiter / EOF) {
return types.NewDelimitedBlock(types.Example, content.([]interface{}), types.None)
}

Expand Down Expand Up @@ -1112,7 +1118,7 @@ SidebarBlock <- SidebarBlockDelimiter content:(SidebarBlockContent)* (SidebarBl
return types.NewDelimitedBlock(types.Sidebar, content.([]interface{}), types.None)
}

SidebarBlockContent <- List / BlockParagraph / BlankLine / NonSidebarBlock
SidebarBlockContent <- BlankLine / List / NonSidebarBlock / BlockParagraph

NonSidebarBlock <- !SidebarBlock content:(DelimitedBlock) {
return content, nil
Expand Down
Loading

0 comments on commit d2945ab

Please sign in to comment.