diff options
| author | Shulhan <ms@kilabit.info> | 2022-09-05 00:25:40 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-09-05 00:25:40 +0700 |
| commit | 7eea9af824bcaee304a11d0048bdeb84fca9e96e (patch) | |
| tree | 9bc4f7c8711437d7d3b9c87e554e6392583a8a49 | |
| parent | 42cc6b8a786b02ee8147400a80f2ef4fb5379a01 (diff) | |
| download | asciidoctor-go-7eea9af824bcaee304a11d0048bdeb84fca9e96e.tar.xz | |
all: fix parsing list description inside include directive
Previously, given the following include statements in the main document
include::list_desc.adoc[]
include::list_desc.adoc[]
Where list_desc.adoc content is,
Item 00::
+
--
* Bullet 0
+
Description 0.
* Bullet 1
--
The first include is parsed correctly, but the second include is parsed
as is.
| -rw-r--r-- | document_parser.go | 121 | ||||
| -rw-r--r-- | testdata/_includes/list_desc_00.adoc | 9 | ||||
| -rw-r--r-- | testdata/_includes/list_desc_01.adoc | 9 | ||||
| -rw-r--r-- | testdata/include_test.txt | 54 | ||||
| -rw-r--r-- | testdata/test.adoc | 4 | ||||
| -rw-r--r-- | testdata/test.exp.html | 46 | ||||
| -rw-r--r-- | testdata/test.got.html | 46 |
7 files changed, 237 insertions, 52 deletions
diff --git a/document_parser.go b/document_parser.go index 2227ed2..9e78554 100644 --- a/document_parser.go +++ b/document_parser.go @@ -49,16 +49,13 @@ func parseSub(parentDoc *Document, content []byte) (subdoc *Document) { return subdoc } -func (docp *documentParser) consumeLinesUntil( - el *element, term int, terms []int, -) ( - line []byte, -) { +func (docp *documentParser) consumeLinesUntil(el *element, term int, terms []int) (line []byte) { var ( - elInclude *elementInclude - spaces []byte - t int + logp = `consumeLinesUntil` + elInclude *elementInclude + spaces []byte + t int ok bool allowComment bool ) @@ -68,7 +65,7 @@ func (docp *documentParser) consumeLinesUntil( allowComment = true } for { - spaces, line, ok = docp.line() + spaces, line, ok = docp.line(logp) if !ok { break } @@ -131,6 +128,8 @@ func (docp *documentParser) include(el *elementInclude) { var ( includedLines = bytes.Split(content, []byte("\n")) newLines = make([][]byte, 0, len(docp.lines)+len(includedLines)) + + line []byte ) // Do not add the "include" directive @@ -139,11 +138,17 @@ func (docp *documentParser) include(el *elementInclude) { newLines = append(newLines, includedLines...) newLines = append(newLines, docp.lines[docp.lineNum+1:]...) docp.lines = newLines + + if debug.Value == 2 { + for _, line = range includedLines { + fmt.Printf("%s\n", line) + } + } } // line return the next line in the content of raw document. // It will return ok as false if there are no more line. -func (docp *documentParser) line() (spaces, line []byte, ok bool) { +func (docp *documentParser) line(logp string) (spaces, line []byte, ok bool) { docp.prevKind = docp.kind if docp.lineNum >= len(docp.lines) { @@ -151,27 +156,29 @@ func (docp *documentParser) line() (spaces, line []byte, ok bool) { } line = docp.lines[docp.lineNum] + if debug.Value == 2 { + fmt.Printf("line %3d: %s: %s\n", docp.lineNum, logp, line) + } docp.lineNum++ docp.kind, spaces, line = whatKindOfLine(line) - if debug.Value == 2 { - fmt.Printf("line %3d: kind %3d: %s\n", docp.lineNum, docp.kind, line) - } return spaces, line, true } func (docp *documentParser) parseBlock(parent *element, term int) { var ( - el = &element{ + logp = `parseBlock` + el = &element{ kind: elKindUnknown, } - line []byte - ok bool + line []byte + isTerm bool + ok bool ) - for { + for !isTerm { if len(line) == 0 { - _, line, ok = docp.line() + _, line, ok = docp.line(logp) if !ok { return } @@ -179,7 +186,8 @@ func (docp *documentParser) parseBlock(parent *element, term int) { switch docp.kind { case term: - return + isTerm = true + continue case lineKindEmpty: line = nil continue @@ -234,7 +242,7 @@ func (docp *documentParser) parseBlock(parent *element, term int) { case lineKindInclude: var ( - elInclude = parseInclude(docp.doc, []byte(line)) + elInclude *elementInclude = parseInclude(docp.doc, []byte(line)) ) if elInclude == nil { @@ -566,6 +574,7 @@ func (docp *documentParser) parseHeader() { ) var ( + logp = `parseHeader` state int = stateBegin key string @@ -574,7 +583,7 @@ func (docp *documentParser) parseHeader() { ok bool ) for { - _, line, ok = docp.line() + _, line, ok = docp.line(logp) if !ok { return } @@ -620,12 +629,14 @@ func (docp *documentParser) parseHeader() { func (docp *documentParser) parseIgnoreCommentBlock() { var ( + logp = `parseIgnoreCommentBlock` + line []byte ok bool ) for { - _, line, ok = docp.line() + _, line, ok = docp.line(logp) if !ok { return } @@ -638,9 +649,14 @@ func (docp *documentParser) parseIgnoreCommentBlock() { // parseListBlock parse block after list continuation `+` until we found // empty line or non-list line. func (docp *documentParser) parseListBlock() (el *element, line []byte) { - var ok bool + var ( + logp = `parseListBlock` + + ok bool + ) + for { - _, line, ok = docp.line() + _, line, ok = docp.line(logp) if !ok { break } @@ -750,12 +766,11 @@ func (docp *documentParser) parseListBlock() (el *element, line []byte) { return el, line } -func (docp *documentParser) parseListDescription( - parent, el *element, line []byte, term int, -) ( - got []byte, -) { +// parseListDescription parse the list description item, line that end with +// "::" WSP, and its content. +func (docp *documentParser) parseListDescription(parent, el *element, line []byte, term int) (got []byte) { var ( + logp = `parseListDescription` list = &element{ elementAttribute: elementAttribute{ style: el.style, @@ -769,6 +784,8 @@ func (docp *documentParser) parseListDescription( }, kind: elKindListDescriptionItem, } + + ok bool ) listItem.parseListDescriptionItem(line) @@ -777,10 +794,9 @@ func (docp *documentParser) parseListDescription( parent.addChild(list) line = nil - var ok bool for { if len(line) == 0 { - _, line, ok = docp.line() + _, line, ok = docp.line(logp) if !ok { break } @@ -797,6 +813,20 @@ func (docp *documentParser) parseListDescription( line = nil continue } + if docp.kind == lineKindInclude { + var elInclude = parseInclude(docp.doc, line) + if elInclude == nil { + el.Write(line) + el.WriteByte('\n') + line = nil + continue + } + // Include the content of file into the current + // document. + docp.include(elInclude) + line = nil + continue + } if docp.kind == lineKindListContinue { var el *element el, line = docp.parseListBlock() @@ -916,12 +946,9 @@ func (docp *documentParser) parseListDescription( // parseListOrdered parser the content as list until it found line that is not // list-item. // On success it will return non-empty line and terminator character. -func (docp *documentParser) parseListOrdered( - parent *element, title string, line []byte, term int, -) ( - got []byte, -) { +func (docp *documentParser) parseListOrdered(parent *element, title string, line []byte, term int) (got []byte) { var ( + logp = `parseListOrdered` list = &element{ kind: elKindListOrdered, rawTitle: title, @@ -932,6 +959,7 @@ func (docp *documentParser) parseListOrdered( el *element parentListItem *element + ok bool ) listItem.parseListOrderedItem(line) @@ -939,11 +967,10 @@ func (docp *documentParser) parseListOrdered( list.addChild(listItem) parent.addChild(list) - var ok bool line = nil for { if len(line) == 0 { - _, line, ok = docp.line() + _, line, ok = docp.line(logp) if !ok { break } @@ -1138,12 +1165,9 @@ func (docp *documentParser) parseListOrdered( return line } -func (docp *documentParser) parseListUnordered( - parent, el *element, line []byte, term int, -) ( - got []byte, -) { +func (docp *documentParser) parseListUnordered(parent, el *element, line []byte, term int) (got []byte) { var ( + logp = `parseListUnordered` list = &element{ elementAttribute: elementAttribute{ roles: []string{classNameUlist}, @@ -1154,8 +1178,8 @@ func (docp *documentParser) parseListUnordered( listItem *element parentListItem *element - - role string + role string + ok bool ) if len(el.rawStyle) > 0 { @@ -1180,11 +1204,10 @@ func (docp *documentParser) parseListUnordered( } parent.addChild(list) - var ok bool line = nil for { if len(line) == 0 { - _, line, ok = docp.line() + _, line, ok = docp.line(logp) if !ok { break } @@ -1386,9 +1409,7 @@ func (docp *documentParser) parseListUnordered( return line } -func (docp *documentParser) parseParagraph( - parent, el *element, line []byte, term int, -) []byte { +func (docp *documentParser) parseParagraph(parent, el *element, line []byte, term int) []byte { el.kind = elKindParagraph el.Write(line) el.WriteByte('\n') diff --git a/testdata/_includes/list_desc_00.adoc b/testdata/_includes/list_desc_00.adoc new file mode 100644 index 0000000..fc4629b --- /dev/null +++ b/testdata/_includes/list_desc_00.adoc @@ -0,0 +1,9 @@ +Item 00:: ++ +-- +* Bullet 0 ++ +Description 0. + +* Bullet 1 +-- diff --git a/testdata/_includes/list_desc_01.adoc b/testdata/_includes/list_desc_01.adoc new file mode 100644 index 0000000..b6279da --- /dev/null +++ b/testdata/_includes/list_desc_01.adoc @@ -0,0 +1,9 @@ +Item 01:: ++ +-- +* Bullet 0 ++ +Description 0. + +* Bullet 1 +-- diff --git a/testdata/include_test.txt b/testdata/include_test.txt new file mode 100644 index 0000000..ba8a0bb --- /dev/null +++ b/testdata/include_test.txt @@ -0,0 +1,54 @@ +Test include directive with list description that contains open block. + +>>> list_description + +include::testdata/_includes/list_desc_00.adoc[] + +include::testdata/_includes/list_desc_01.adoc[] + +<<< list_description + +<div class="dlist"> +<dl> +<dt class="hdlist1">Item 00</dt> +<dd> +<div class="openblock"> +<div class="content"> +<div class="ulist"> +<ul> +<li> +<p>Bullet 0</p> +<div class="paragraph"> +<p>Description 0.</p> +</div> +</li> +<li> +<p>Bullet 1</p> +</li> +</ul> +</div> +</div> +</div> +</dd> +<dt class="hdlist1">Item 01</dt> +<dd> +<div class="openblock"> +<div class="content"> +<div class="ulist"> +<ul> +<li> +<p>Bullet 0</p> +<div class="paragraph"> +<p>Description 0.</p> +</div> +</li> +<li> +<p>Bullet 1</p> +</li> +</ul> +</div> +</div> +</div> +</dd> +</dl> +</div> diff --git a/testdata/test.adoc b/testdata/test.adoc index d8473c5..cece8d6 100644 --- a/testdata/test.adoc +++ b/testdata/test.adoc @@ -998,6 +998,10 @@ include::{includedir}/fragment1.adoc[] include::{sourcedir}/anchor.go[] ---- +include::_includes/list_desc_00.adoc[] + +include::_includes/list_desc_01.adoc[] + == Images diff --git a/testdata/test.exp.html b/testdata/test.exp.html index 895a307..30eddd9 100644 --- a/testdata/test.exp.html +++ b/testdata/test.exp.html @@ -2342,6 +2342,50 @@ type anchor struct { }</code></pre> </div> </div> +<div class="dlist"> +<dl> +<dt class="hdlist1">Item 00</dt> +<dd> +<div class="openblock"> +<div class="content"> +<div class="ulist"> +<ul> +<li> +<p>Bullet 0</p> +<div class="paragraph"> +<p>Description 0.</p> +</div> +</li> +<li> +<p>Bullet 1</p> +</li> +</ul> +</div> +</div> +</div> +</dd> +<dt class="hdlist1">Item 01</dt> +<dd> +<div class="openblock"> +<div class="content"> +<div class="ulist"> +<ul> +<li> +<p>Bullet 0</p> +<div class="paragraph"> +<p>Description 0.</p> +</div> +</li> +<li> +<p>Bullet 1</p> +</li> +</ul> +</div> +</div> +</div> +</dd> +</dl> +</div> </div> </div> <div class="sect1"> @@ -2954,7 +2998,7 @@ this sidebar.</p> <div id="footer"> <div id="footer-text"> 1.1.1<br> -Last updated 2022-07-24 20:18:18 +0700 +Last updated 2022-09-04 23:41:43 +0700 </div> </div> </body> diff --git a/testdata/test.got.html b/testdata/test.got.html index 51a8310..33b917d 100644 --- a/testdata/test.got.html +++ b/testdata/test.got.html @@ -2339,6 +2339,50 @@ type anchor struct { }</code></pre> </div> </div> +<div class="dlist"> +<dl> +<dt class="hdlist1">Item 00</dt> +<dd> +<div class="openblock"> +<div class="content"> +<div class="ulist"> +<ul> +<li> +<p>Bullet 0</p> +<div class="paragraph"> +<p>Description 0.</p> +</div> +</li> +<li> +<p>Bullet 1</p> +</li> +</ul> +</div> +</div> +</div> +</dd> +<dt class="hdlist1">Item 01</dt> +<dd> +<div class="openblock"> +<div class="content"> +<div class="ulist"> +<ul> +<li> +<p>Bullet 0</p> +<div class="paragraph"> +<p>Description 0.</p> +</div> +</li> +<li> +<p>Bullet 1</p> +</li> +</ul> +</div> +</div> +</div> +</dd> +</dl> +</div> </div> </div> <div class="sect1"> @@ -2956,7 +3000,7 @@ this sidebar.</p> <div id="footer"> <div id="footer-text"> 1.1.1<br> -Last updated 2022-07-24 21:44:51 +0700 +Last updated 2022-09-04 23:41:43 +0700 </div> </div> </body> |
