aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--document_parser.go4
-rw-r--r--parser.go24
-rw-r--r--testdata/include_code_block_test.txt37
3 files changed, 63 insertions, 2 deletions
diff --git a/document_parser.go b/document_parser.go
index ca167e6..8de91cc 100644
--- a/document_parser.go
+++ b/document_parser.go
@@ -552,7 +552,7 @@ func (docp *documentParser) parseBlock(parent *element, term int) {
el.kind = docp.kind
el.addRole(classNameListingBlock)
line = docp.consumeLinesUntil(el, docp.kind, nil)
- el.raw = applySubstitutions(docp.doc, el.raw)
+ el.raw = preprocessBlockCode(docp.doc, el.raw)
parent.addChild(el)
el = &element{}
continue
@@ -872,7 +872,7 @@ func (docp *documentParser) parseListBlock() (el *element, line []byte) {
kind: docp.kind,
}
docp.consumeLinesUntil(el, docp.kind, nil)
- el.raw = applySubstitutions(docp.doc, el.raw)
+ el.raw = preprocessBlockCode(docp.doc, el.raw)
line = nil
break
}
diff --git a/parser.go b/parser.go
index 6912890..bf435f8 100644
--- a/parser.go
+++ b/parser.go
@@ -314,6 +314,30 @@ var _attrRef = map[string]string{
`zwsp`: htmlSymbolZeroWidthSpace,
}
+// preprocessBlockCode preprocess the content of block code, like "include::"
+// directive, and return the new content.
+func preprocessBlockCode(doc *Document, content []byte) (newContent []byte) {
+ var bbuf bytes.Buffer
+ var lines = bytes.Split(content, []byte{'\n'})
+ for _, line := range lines {
+ if bytes.HasPrefix(line, []byte(`include::`)) {
+ var elInclude = parseInclude(doc, line)
+ if elInclude != nil {
+ bbuf.Write(elInclude.content)
+ bbuf.WriteByte('\n')
+ continue
+ }
+ }
+ bbuf.Write(line)
+ bbuf.WriteByte('\n')
+ }
+
+ newContent = applySubstitutions(doc, bbuf.Bytes())
+ return newContent
+}
+
+// applySubstitutions scan the content and replace attribute reference "{}"
+// with its value, and character '<', '>', '&' with HTML symbol.
func applySubstitutions(doc *Document, content []byte) []byte {
var (
raw = bytes.TrimRight(content, " \n")
diff --git a/testdata/include_code_block_test.txt b/testdata/include_code_block_test.txt
new file mode 100644
index 0000000..6215062
--- /dev/null
+++ b/testdata/include_code_block_test.txt
@@ -0,0 +1,37 @@
+Test include directive inside source code block.
+
+>>> case-01
+
+----
+include::testdata/_includes/fragment1.adoc[]
+----
+
+<<< case-01
+
+<div class="listingblock">
+<div class="content">
+<pre>// SPDX-FileCopyrightText: 2020 M. Shulhan &lt;ms@kilabit.info&gt;
+// SPDX-License-Identifier: GPL-3.0-or-later
+This is inside the fragment1.adoc.</pre>
+</div>
+</div>
+
+>>> case-02
+
+----
+before
+include::testdata/_includes/fragment1.adoc[]
+after
+----
+
+<<< case-02
+
+<div class="listingblock">
+<div class="content">
+<pre>before
+// SPDX-FileCopyrightText: 2020 M. Shulhan &lt;ms@kilabit.info&gt;
+// SPDX-License-Identifier: GPL-3.0-or-later
+This is inside the fragment1.adoc.
+after</pre>
+</div>
+</div>