Skip to content

Commit

Permalink
resolves #1652 Map AbstractBlock#getContentModel and #setContentModel (
Browse files Browse the repository at this point in the history
…#1680)

resolves #1652
  • Loading branch information
ggrossetie authored Jan 10, 2023
1 parent 25c87bb commit 4c30320
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
16 changes: 16 additions & 0 deletions packages/core/spec/share/asciidoctor-spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,22 @@ paragraph 3
expect(paragraph.getAttribute('foo')).to.equal('bar')
})

it('should create a new paragraph block with verbatim content model', function () {
const doc = asciidoctor.load('= Title')
const paragraph = asciidoctor.Block.create(doc, 'paragraph', { source: ' _This_ is a <test>' })
paragraph.setContentModel('verbatim')
expect(paragraph.getContentModel()).to.equal('verbatim')
expect(paragraph.getContent()).to.equal(' _This_ is a <test>')
})

it('should create a new literal block with empty content model', function () {
const doc = asciidoctor.load('= Title')
const paragraph = asciidoctor.Block.create(doc, 'literal', { source: '_This_ is a <test>' })
paragraph.setContentModel('empty')
expect(paragraph.getContentModel()).to.equal('empty')
expect(paragraph.getContent()).to.be.undefined()
})

it('should assign a caption on a Block', function () {
const doc = asciidoctor.load('= Title')
const image = asciidoctor.Block.create(doc, 'image', {
Expand Down
29 changes: 27 additions & 2 deletions packages/core/src/asciidoctor-core-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,30 @@ Asciidoctor.prototype.loadFile = function (filename, options) {
*/
const AbstractBlock = Opal.Asciidoctor.AbstractBlock

/**
* Describes the type of content this block accepts and how it should be converted.
* @returns {string} - the type of content this block accepts.
* @memberof AbstractBlock
*/
AbstractBlock.prototype.getContentModel = function () {
const contentModel = this.content_model
return contentModel === Opal.nil ? undefined : contentModel
}

/**
* Set the type of content this block accepts. Acceptable values are:
* - compound - this block contains other blocks
* - simple - this block holds a paragraph of prose that receives normal substitutions
* - verbatim - this block holds verbatim text (displayed "as is") that receives verbatim substitutions
* - raw - this block holds unprocessed content passed directly to the output with no substitutions applied
* - empty - this block has no content
* @param {string} contentModel - type of content, one of: compound, simple, verbatim, raw or empty.
* @memberof AbstractBlock
*/
AbstractBlock.prototype.setContentModel = function (contentModel) {
this.content_model = contentModel
}

/**
* Append a block to this block's list of child blocks.
* @param {AbstractBlock} block - the block to append
Expand All @@ -457,7 +481,7 @@ AbstractBlock.prototype.append = function (block) {
}

/**
* Get the String title of this Block with title substitions applied
* Get the String title of this Block with title substitutions applied
*
* The following substitutions are applied to block and section titles:
*
Expand Down Expand Up @@ -664,7 +688,8 @@ AbstractBlock.prototype.getBlocks = function () {
* @memberof AbstractBlock
*/
AbstractBlock.prototype.getContent = function () {
return this.$content()
const content = this.$content()
return content === Opal.nil ? undefined : content
}

/**
Expand Down
36 changes: 28 additions & 8 deletions packages/core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1983,15 +1983,35 @@ export class Document extends AbstractBlock {
getExtensions(): Registry;
}

/**
*/
export class AbstractBlock extends AbstractNode {
type ContentModel = 'compound' | 'simple' | 'verbatim' | 'raw' | 'empty'

/**
* Append a block to this block's list of child blocks.
* @param block - the block to append
* @returns the parent block to which this block was appended.
*/
append(block: AbstractBlock): AbstractBlock;
export class AbstractBlock extends AbstractNode {

/**
* Describes the type of content this block accepts and how it should be converted.
* @returns the type of content this block accepts.
*/
getContentModel(): ContentModel

/**
* Set the type of content this block accepts. Acceptable values are:
* - compound - this block contains other blocks
* - simple - this block holds a paragraph of prose that receives normal substitutions
* - verbatim - this block holds verbatim text (displayed "as is") that receives verbatim substitutions
* - raw - this block holds unprocessed content passed directly to the output with no substitutions applied
* - empty - this block has no content
* @param contentModel - the type of content
*/
setContentModel(contentModel: ContentModel): void;

/**
* Append a block to this block's list of child blocks.
* @param block - the block to append
* @returns the parent block to which this block was appended.
*/
append(block: AbstractBlock): AbstractBlock;

/**
* Get the String title of this Block with title substitions applied
Expand Down Expand Up @@ -2118,7 +2138,7 @@ export class AbstractBlock extends AbstractNode {
* Get the converted result of the child blocks by converting the children appropriate to content model that this block supports.
* @returns the converted result of the child blocks
*/
getContent(): string;
getContent(): string|undefined;

/**
* Get the converted content for this block.
Expand Down
10 changes: 10 additions & 0 deletions packages/core/types/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ assert(attributes.count === 3);
assert(attributes.awesome);
assert(attributes.status === 'passive');

const literalBlockWithEmptyContentModel = processor.Block.create(doc, 'literal', { source: '_This_ is a <test>' });
literalBlockWithEmptyContentModel.setContentModel('empty')
assert(literalBlockWithEmptyContentModel.getContentModel() === 'empty');
assert(literalBlockWithEmptyContentModel.getContent() === undefined);

const paragraphBlockWithVerbatimContentModel = processor.Block.create(doc, 'paragraph', { source: '_This_ is a <test>' });
paragraphBlockWithVerbatimContentModel.setContentModel('verbatim')
assert(paragraphBlockWithVerbatimContentModel.getContentModel() === 'verbatim');
assert(paragraphBlockWithVerbatimContentModel.getContent() === '_This_ is a <test>');

// Section
const section = processor.Section.create();
section.setId('sect1');
Expand Down

0 comments on commit 4c30320

Please sign in to comment.