summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jirutka <jakub@jirutka.cz>2018-08-12 12:42:36 +0200
committerJakub Jirutka <jakub@jirutka.cz>2018-08-12 16:09:36 +0200
commit6e5548ef9a522dfc9705d552a7444eeb613ac31f (patch)
tree80fd092d1cdb6eebec197d768b8f11f8f9f4e531
parent3e3d1cc187282102ad2e8484122a7838724a52d3 (diff)
Add special API for JS, don't register implicitly after require
-rw-r--r--README.adoc35
-rw-r--r--Rakefile7
-rw-r--r--lib/asciidoctor/interdoc_reftext.rb11
-rw-r--r--lib/asciidoctor/interdoc_reftext/version.rb2
-rw-r--r--src/asciidoctor-interdoc-reftext.tmpl.js94
5 files changed, 140 insertions, 9 deletions
diff --git a/README.adoc b/README.adoc
index 46498df..af8c70b 100644
--- a/README.adoc
+++ b/README.adoc
@@ -109,11 +109,42 @@ IMPORTANT: Bundler automatically _requires_ all the specified gems.
----
// Load asciidoctor.js and {gem-name}.
const asciidoctor = require('asciidoctor.js')()
-require('{gem-name}')
+const interdocReftextExt = require('{gem-name}')
+
+// See documentation at the end of this section.
+const options = {
+ asciidocExts: ['.adoc'],
+}
+
+// Configure the extension and register it into the global registry.
+interdocReftextExt.register(asciidoctor.Extensions, options)
// Convert the file to HTML.
-asciidoctor.convertFile("./README.adoc")
+asciidoctor.convertFile('./README.adoc')
+----
+
+If you don’t want to register the extension globally, you can also pass a custom registry to the `register` function:
+
+[source, js]
----
+// ...
+
+const registry = asciidoctor.Extensions.create()
+interdocReftextExt.register(registry, options)
+
+const html = asciidoctor.convertFile('./README.adoc', {
+ 'extension_registry': registry,
+})
+----
+
+.*Options:*
+asciidocExts::
+ AsciiDoc file extensions (e.g. `.adoc`).
+ Default is `['.adoc', '.asciidoc', '.ad']`.
+
+raiseExceptions::
+ Whether to raise exceptions (`true`), or just log them (`false`).
+ Default is `true`.
== License
diff --git a/Rakefile b/Rakefile
index bc86f92..54fd891 100644
--- a/Rakefile
+++ b/Rakefile
@@ -32,12 +32,15 @@ namespace :build do
dynamic_require_severity: :error,
})
builder.append_paths 'lib'
- builder.build 'asciidoctor-interdoc-reftext'
+ builder.build 'asciidoctor/interdoc_reftext'
out_file = 'js/asciidoctor-interdoc-reftext.js'
mkdir_p(File.dirname(out_file), verbose: false)
- File.binwrite out_file, builder.to_s
+ File.open(out_file, 'w') do |file|
+ template = File.read('src/asciidoctor-interdoc-reftext.tmpl.js')
+ file << template.sub('//OPAL-GENERATED-CODE//', builder.to_s)
+ end
File.binwrite "#{out_file}.map", builder.source_map
end
end
diff --git a/lib/asciidoctor/interdoc_reftext.rb b/lib/asciidoctor/interdoc_reftext.rb
index dc884cb..165bd13 100644
--- a/lib/asciidoctor/interdoc_reftext.rb
+++ b/lib/asciidoctor/interdoc_reftext.rb
@@ -1,9 +1,12 @@
# frozen_string_literal: true
-require 'asciidoctor' unless RUBY_PLATFORM == 'opal'
-require 'asciidoctor/extensions' unless RUBY_PLATFORM == 'opal'
require 'asciidoctor/interdoc_reftext/version'
require 'asciidoctor/interdoc_reftext/processor'
-Asciidoctor::Extensions.register do
- treeprocessor Asciidoctor::InterdocReftext::Processor
+unless RUBY_PLATFORM == 'opal'
+ require 'asciidoctor'
+ require 'asciidoctor/extensions'
+
+ Asciidoctor::Extensions.register do
+ treeprocessor Asciidoctor::InterdocReftext::Processor
+ end
end
diff --git a/lib/asciidoctor/interdoc_reftext/version.rb b/lib/asciidoctor/interdoc_reftext/version.rb
index 2913b0e..d52fde6 100644
--- a/lib/asciidoctor/interdoc_reftext/version.rb
+++ b/lib/asciidoctor/interdoc_reftext/version.rb
@@ -3,6 +3,6 @@
module Asciidoctor
module InterdocReftext
# Version of the asciidoctor-interdoc-reftext gem.
- VERSION = '0.3.0'.freeze
+ VERSION = '0.3.0'
end
end
diff --git a/src/asciidoctor-interdoc-reftext.tmpl.js b/src/asciidoctor-interdoc-reftext.tmpl.js
new file mode 100644
index 0000000..495643e
--- /dev/null
+++ b/src/asciidoctor-interdoc-reftext.tmpl.js
@@ -0,0 +1,94 @@
+(function (Opal) {
+ function initialize (Opal) {
+//OPAL-GENERATED-CODE//
+ }
+
+ var mainModule
+
+ function resolveModule (name) {
+ if (!mainModule) {
+ checkAsciidoctor()
+ initialize(Opal)
+ mainModule = Opal.const_get_qualified(Opal.Asciidoctor, 'InterdocReftext')
+ }
+ if (!name) {
+ return mainModule
+ }
+ return Opal.const_get_qualified(mainModule, name)
+ }
+
+ function checkAsciidoctor () {
+ if (typeof Opal.Asciidoctor === 'undefined') {
+ throw new TypeError('Asciidoctor.js is not loaded')
+ }
+ }
+
+ /**
+ * @param {Object} opts
+ * @param {String[]} opts.asciidocExts AsciiDoc file extensions (e.g. `.adoc`).
+ * Default is `['.adoc', '.asciidoc', '.ad']`.
+ * @param {boolean} opts.raiseExceptions Whether to raise exceptions (`true`),
+ * or just log them (`false`). Default is `true`.
+ * @return A new instance of `Asciidoctor::InterdocReftext::Processor`.
+ */
+ function TreeProcessor (opts) {
+ opts = opts || {}
+
+ var processor = resolveModule('Processor').$new(Opal.hash({
+ resolver_class: opts.resolverClass,
+ asciidoc_exts: opts.asciidocExts,
+ logger: opts.logger,
+ raise_exceptions: opts.raiseExceptions,
+ }))
+ processor.process = processor.$process
+
+ return processor
+ }
+
+ /**
+ * @return {string} Version of this extension.
+ */
+ function getVersion () {
+ return resolveModule().$$const.VERSION.toString()
+ }
+
+ /**
+ * Creates and configures the Inter-doc Reference Text extension and registers
+ * it in the extensions registry.
+ *
+ * @param registry The Asciidoctor extensions registry to register this
+ * extension into. Defaults to the global Asciidoctor registry.
+ * @param {Object} opts See {TreeProcessor} (optional).
+ * @throws {TypeError} if the *registry* is invalid or Asciidoctor.js is not loaded.
+ */
+ function register (registry, opts) {
+ if (!registry) {
+ checkAsciidoctor()
+ registry = Opal.Asciidoctor.Extensions
+ }
+ var processor = TreeProcessor(opts)
+
+ // global registry
+ if (typeof registry.register === 'function') {
+ registry.register(function () {
+ this.treeProcessor(processor)
+ })
+ // custom registry
+ } else if (typeof registry.block === 'function') {
+ registry.treeProcessor(processor)
+ } else {
+ throw new TypeError('Invalid registry object')
+ }
+ }
+
+ var facade = {
+ TreeProcessor: TreeProcessor,
+ getVersion: getVersion,
+ register: register,
+ }
+
+ if (typeof module !== 'undefined' && module.exports) {
+ module.exports = facade
+ }
+ return facade
+})(Opal);