Skip to content

Commit

Permalink
Merge 730431c into 87caa9c
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Mar 9, 2018
2 parents 87caa9c + 730431c commit c700546
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
26 changes: 25 additions & 1 deletion spec/node/asciidoctor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function asciidoctorVersionGreaterThan (version) {
// ignore the fourth number, keep only major, minor and patch numbers
const currentVersionNumeric = parseInt(currentVersion.replace('.dev', '').replace(/\./g, '').substring(0, 3));
const versionNumeric = version.replace(/\./g, '');
return currentVersionNumeric > versionNumeric;
return currentVersionNumeric > versionNumeric;
}

const Opal = require('opal-runtime').Opal; // for testing purpose only
Expand Down Expand Up @@ -407,6 +407,30 @@ Content 2`;
expect(result).toContain('twemoji.maxcdn.com');
});

it('should be able to register a include processor class', function () {
var registry = asciidoctor.Extensions.create();
registry.includeProcessor(asciidoctor.Extensions.createIncludeProcessor('StaticIncludeProcessor', function (doc, reader, target, attrs) {
reader.pushInclude(['included content'], target, target, 1, attrs);
}));
var opts = {};
opts[asciidoctorVersionGreaterThan('1.5.5') ? 'extension_registry' : 'extensions_registry'] = registry;
opts['safe'] = 'safe';
var result = asciidoctor.convert('include::whatever.adoc[]', opts);
expect(result).toContain('included content');
});

it('should be able to register an include processor instance', function () {
var registry = asciidoctor.Extensions.create();
registry.includeProcessor(asciidoctor.Extensions.newIncludeProcessor('StaticIncludeProcessor', function (doc, reader, target, attrs) {
reader.pushInclude(['included content'], target, target, 1, attrs);
}));
var opts = {};
opts[asciidoctorVersionGreaterThan('1.5.5') ? 'extension_registry' : 'extensions_registry'] = registry;
opts['safe'] = 'safe';
var result = asciidoctor.convert('include::whatever.adoc[]', opts);
expect(result).toContain('included content');
});

it('should be able to convert a file and include the default stylesheet', () => {
const options = {safe: 'safe', header_footer: true};
const html = asciidoctor.convert('=== Test', options);
Expand Down
24 changes: 23 additions & 1 deletion src/asciidoctor-extensions-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ Registry.$$proto.inlineMacro = function (name, block) {
* @memberof Extensions/Registry
*/
Registry.$$proto.includeProcessor = function (block) {
return Opal.send(this, 'include_processor', null, toBlock(block));
if (typeof block === 'function') {
return Opal.send(this, 'include_processor', null, toBlock(block));
}
return this.$include_processor(block);
};

/**
Expand Down Expand Up @@ -292,3 +295,22 @@ var DocinfoProcessor = Extensions.DocinfoProcessor;
DocinfoProcessor.$$proto.atLocation = function (value) {
this.$at_location(value);
};


Extensions.createIncludeProcessor = function (name, processFunction, handlesFunction) {
var superclass = Opal.const_get_qualified(Extensions, 'IncludeProcessor');
var scope = Opal.klass(Opal.Object, superclass, name, function () {});
Opal.defn(scope, '$initialize', function initialize () {
Opal.send(this, Opal.find_super_dispatcher(this, 'initialize', initialize));
});
Opal.defn(scope, '$process', processFunction);
if (typeof handlesFunction !== 'function') {
handlesFunction = function () { return true; };
}
Opal.defn(scope, '$handles?', handlesFunction);
return scope;
};

Extensions.newIncludeProcessor = function (name, processFunction, handlesFunction) {
return this.createIncludeProcessor(name, processFunction, handlesFunction).$new();
};

0 comments on commit c700546

Please sign in to comment.