diff options
| author | Shulhan <ms@kilabit.info> | 2025-04-18 15:24:10 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-04-18 15:24:10 +0700 |
| commit | f25296bef9bb02f3be87e61c54703e26b7814096 (patch) | |
| tree | 50693da53586aba7efe84af245f26fbe789725ce | |
| parent | 89d1a301ea4d42b73d7af3c982ede59e1f868260 (diff) | |
| download | asciidoctor-go-f25296bef9bb02f3be87e61c54703e26b7814096.tar.xz | |
all: support include directive inside block code
Example of block code with include directive,
----
...
include::file[]
...
----
| -rw-r--r-- | document_parser.go | 4 | ||||
| -rw-r--r-- | parser.go | 24 | ||||
| -rw-r--r-- | testdata/include_code_block_test.txt | 37 |
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 } @@ -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 <ms@kilabit.info> +// 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 <ms@kilabit.info> +// SPDX-License-Identifier: GPL-3.0-or-later +This is inside the fragment1.adoc. +after</pre> +</div> +</div> |
