Skip to content

Commit 0145787

Browse files
committed
Merge pull request #25 from wilkinsona
* pr/25: Copy attributes on macro onto blocks for included code Closes gh-25
2 parents 5d8e8aa + 4df5173 commit 0145787

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed

lib/include-code-extension.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function createExtensionGroup (context) {
3030
if (!langs.length) return log(doc, 'warn', `no search locations defined for include-code::${target}[]`)
3131
const cursor = doc.getReader().$cursor_at_mark()
3232
tabsEnabled ??= doc.getExtensions().hasBlocks() && !!doc.getExtensions().getBlockFor('tabs', 'example')
33-
const attrsStr = Object.entries(attrs).reduce((buf, [n, v]) => `${buf}${buf ? ',' : ''}${n}=${v}`, '')
33+
const attrsStr = Object.entries(attrs).filter(([n, v]) => n != 'title').reduce((buf, [n, v]) => `${buf}${buf ? ',' : ''}${n}=${v}`, '')
3434
const sectionId = (nearest(parent, 'section') || doc).getId()
3535
const relativeIdPath = sectionId
3636
? sectionId.replaceAll('-', '').replaceAll('.', '/') + '/' + sanitizedTarget
@@ -47,7 +47,7 @@ function createExtensionGroup (context) {
4747
return accum.concat({ name, lang, lines })
4848
}, [])
4949
if (!includes.length) return log(doc, 'warn', `no code includes found for ${target}`)
50-
const tabsSource = generateTabsSource(attrs.title, attrs['sync-group-id'], includes, tabsEnabled)
50+
const tabsSource = generateTabsSource(attrs.title, attrs['sync-group-id'], includes, tabsEnabled, attrsStr)
5151
const reader = PreprocessorReader.$new(doc, tabsSource, cursor)
5252
Object.defineProperty(reader, 'lineno', { get: () => cursor.lineno })
5353
return this.parseContent(parent, reader)
@@ -56,24 +56,25 @@ function createExtensionGroup (context) {
5656
}
5757
}
5858

59-
function generateTabsSource (title, syncGroupId, includes, tabsEnabled) {
59+
function generateTabsSource (title, syncGroupId, includes, tabsEnabled, attrsStr) {
6060
const source = []
6161
if (includes.length === 1) {
6262
if (title) source.push('.' + title)
63+
source.push(`[${attrsStr}]`)
6364
source.push(`[,${includes[0].lang}]`, '----', ...includes[0].lines, '----')
6465
} else if (tabsEnabled) {
6566
if (title) source.push('.' + title)
6667
const lastIdx = includes.length - 1
6768
const tabAttrs = syncGroupId ? `,sync-group-id=${syncGroupId}` : ''
6869
includes.forEach(({ name, lang, lines }, idx) => {
6970
idx ? source.push('') : source.push(`[tabs${tabAttrs}]`, '======')
70-
source.push(`${name}::`, '+', `[,${lang}]`, '----', ...lines, '----')
71+
source.push(`${name}::`, '+', `[${attrsStr}]`, `[,${lang}]`, '----', ...lines, '----')
7172
if (idx === lastIdx) source.push('======')
7273
})
7374
} else {
7475
includes.forEach(({ name, lang, lines }) => {
7576
if (source.length) source.push('')
76-
source.push('.' + (title ? title + ' - ' + name : name), `[,${lang}]`, '----', ...lines, '----')
77+
source.push('.' + (title ? title + ' - ' + name : name), `[${attrsStr}]`, `[,${lang}]`, '----', ...lines, '----')
7778
})
7879
}
7980
return source

test/include-code-extension-test.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,24 @@ describe('include-code-extension', () => {
211211
expect(actual.getBlocks()[0].getTitle()).to.equal('Describe This')
212212
})
213213

214+
it('should pass attribute on block macro for single include through to resulting source block', () => {
215+
const inputSource = heredoc`
216+
package org.example
217+
218+
fun main(args : Array<String>) {
219+
println("Hello, World!")
220+
}
221+
`
222+
addExample('kotlin/hello.kt', inputSource)
223+
const input = heredoc`
224+
[some-attribute=alpha]
225+
include-code::hello[]
226+
`
227+
const actual = run(input, { attributes: { 'include-kotlin': 'example$kotlin' } })
228+
expect(actual.getBlocks()).to.have.lengthOf(1)
229+
expect(actual.getBlocks()[0].getAttribute('some-attribute')).to.equal('alpha')
230+
})
231+
214232
it('should report correct line number in warning for block title', () => {
215233
const inputSource = heredoc`
216234
fun main(args : Array<String>) {
@@ -263,6 +281,35 @@ describe('include-code-extension', () => {
263281
expect(actual.getBlocks()[1].getTitle()).to.equal('Describe This - Kotlin')
264282
})
265283

284+
it('should pass attribute on block macro for multiple includes through to resulting source block', () => {
285+
addExample(
286+
'kotlin/hello.kt',
287+
heredoc`
288+
fun main(args : Array<String>) {
289+
println("Hello, World!")
290+
}
291+
`
292+
)
293+
addExample(
294+
'java/hello.java',
295+
heredoc`
296+
public class Hello {
297+
public static void main (String[] args) {
298+
System.out.println("Hello, World!");
299+
}
300+
}
301+
`
302+
)
303+
const input = heredoc`
304+
[some-attribute=alpha]
305+
include-code::hello[]
306+
`
307+
const actual = run(input)
308+
expect(actual.getBlocks()).to.have.lengthOf(2)
309+
expect(actual.getBlocks()[0].getAttribute('some-attribute')).to.equal('alpha')
310+
expect(actual.getBlocks()[1].getAttribute('some-attribute')).to.equal('alpha')
311+
})
312+
266313
it('should support attributes on include directive of included file', () => {
267314
const inputSource = heredoc`
268315
fun main(args : Array<String>) {
@@ -418,6 +465,54 @@ describe('include-code-extension', () => {
418465
expect(actualProperties).to.eql(expected)
419466
})
420467

468+
it('should pass attribute on block macro to each tabbed block if @asciidoctor/tabs extension is registered', () => {
469+
const expected = [
470+
{ style: 'source', language: 'java', title: undefined, someAttribute: 'alpha' },
471+
{ style: 'source', language: 'kotlin', title: undefined, someAttribute: 'alpha' },
472+
{ style: 'source', language: 'groovy', title: undefined, someAttribute: 'alpha' },
473+
{ style: 'source', language: 'xml', title: undefined, someAttribute: 'alpha' },
474+
]
475+
addExample(
476+
'kotlin/hello.kt',
477+
heredoc`
478+
fun main(args : Array<String>) {
479+
println("Hello, World!")
480+
}
481+
`
482+
)
483+
addExample(
484+
'java/hello.java',
485+
heredoc`
486+
public class Hello {
487+
public static void main (String[] args) {
488+
System.out.println("Hello, World!");
489+
}
490+
}
491+
`
492+
)
493+
addExample('groovy/hello.groovy', 'println "Hello, World!"')
494+
addExample('xml/hello.xml', 'println "<hello />"')
495+
const input = heredoc`
496+
[some-attribute=alpha]
497+
include-code::hello[]`
498+
const doc = run(input, { registerAsciidoctorTabs: true })
499+
const tabs = doc.getBlocks()[0]
500+
console.log(tabs)
501+
expect(tabs).to.exist()
502+
expect(tabs.hasRole('tabs')).to.be.true()
503+
expect(tabs.getTitle()).to.be.undefined()
504+
const tablist = tabs.findBy({ context: 'ulist' })[0]
505+
expect(tablist).to.exist()
506+
expect(tablist.getItems()).to.have.lengthOf(4)
507+
const codeBlocks = tabs.findBy({ context: 'listing' })
508+
expect(codeBlocks).to.have.lengthOf(4)
509+
const actualProperties = codeBlocks.map((block) => {
510+
return { style: block.getStyle(), language: block.getAttributes().language, title: block.getTitle(), someAttribute: block.getAttribute('some-attribute') }
511+
})
512+
console.log(actualProperties)
513+
expect(actualProperties).to.eql(expected)
514+
})
515+
421516
it('should apply title to tabs when @asciidoctor/tabs extension is registered', () => {
422517
addExample(
423518
'kotlin/hello.kt',

0 commit comments

Comments
 (0)