aboutsummaryrefslogtreecommitdiff
path: root/document_parser.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-12-10 02:38:42 +0700
committerShulhan <ms@kilabit.info>2023-12-10 02:40:21 +0700
commitcdd1faa6b97de023ef7129399483e841fb4a9b16 (patch)
tree1d16642ad81c391554e3c9e2420f4fdd7003d738 /document_parser.go
parent9242df367ec97093dbfce62830db020ec53def11 (diff)
downloadasciidoctor-go-cdd1faa6b97de023ef7129399483e841fb4a9b16.tar.xz
all: fix custom IDs on first section got replaced
Any custom ID on the first section always replaced with the subsection because, first, when detecting preamble we did not check for line with kind ID "[[...]]" an short ID "[#...]". Second, when parsing preamble we did not stop when we found line kind ID and short ID. This preamble thing is kind of annoying. We need to revisit again how to detect preamble, maybe not calling separate block parser, but making it linear as the default first child of parent element.
Diffstat (limited to 'document_parser.go')
-rw-r--r--document_parser.go31
1 files changed, 23 insertions, 8 deletions
diff --git a/document_parser.go b/document_parser.go
index d165ff9..27f92be 100644
--- a/document_parser.go
+++ b/document_parser.go
@@ -61,6 +61,8 @@ func parseSub(parentDoc *Document, content []byte) (subdoc *Document) {
return subdoc
}
+// consumeLinesUntil given an element el, consume all lines until we found
+// a line with kind match with term or match with one in terms.
func (docp *documentParser) consumeLinesUntil(el *element, term int, terms []int) (line []byte) {
var (
logp = `consumeLinesUntil`
@@ -148,7 +150,9 @@ func (docp *documentParser) hasPreamble() bool {
kind, _, _ = whatKindOfLine(line)
if kind == elKindSectionL1 || kind == elKindSectionL2 ||
kind == elKindSectionL3 || kind == elKindSectionL4 ||
- kind == elKindSectionL5 {
+ kind == elKindSectionL5 ||
+ kind == lineKindID ||
+ kind == lineKindIDShort {
return notEmtpy > 0
}
notEmtpy++
@@ -276,9 +280,7 @@ func (docp *documentParser) parseMultiline(out io.Writer) {
func (docp *documentParser) parseBlock(parent *element, term int) {
var (
logp = `parseBlock`
- el = &element{
- kind: elKindUnknown,
- }
+ el = &element{}
line []byte
isTerm bool
@@ -314,13 +316,19 @@ func (docp *documentParser) parseBlock(parent *element, term int) {
continue
case lineKindID:
+ if parent.kind == elKindPreamble {
+ docp.kind = lineKindEmpty
+ docp.prevKind = lineKindEmpty
+ docp.lineNum--
+ isTerm = true
+ continue
+ }
var (
idLabel = line[2 : len(line)-2]
id, label = parseIDLabel(idLabel)
)
if len(id) > 0 {
- el.ID = docp.doc.registerAnchor(
- string(id), string(label))
+ el.ID = docp.doc.registerAnchor(string(id), string(label))
line = nil
continue
}
@@ -330,6 +338,14 @@ func (docp *documentParser) parseBlock(parent *element, term int) {
continue
case lineKindIDShort:
+ if parent.kind == elKindPreamble {
+ docp.kind = lineKindEmpty
+ docp.prevKind = lineKindEmpty
+ docp.lineNum--
+ isTerm = true
+ continue
+ }
+
var (
id = line[2 : len(line)-1]
label []byte
@@ -338,8 +354,7 @@ func (docp *documentParser) parseBlock(parent *element, term int) {
id, label = parseIDLabel(id)
if len(id) > 0 {
- el.ID = docp.doc.registerAnchor(
- string(id), string(label))
+ el.ID = docp.doc.registerAnchor(string(id), string(label))
line = nil
continue
}