Skip to content

Commit

Permalink
Add special API for JS, don't register implicitly after require
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
jirutka committed Aug 12, 2018
1 parent 39dde41 commit 4702cb2
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ asciidoctor -r {gem-name} -b html5s FILE...
----
// Load asciidoctor.js and {gem-name}.
const asciidoctor = require('asciidoctor.js')()
require('{gem-name}')
const asciidoctorHtml5s = require('{gem-name}')
// Register the HTML5s converter and supporting extension.
asciidoctorHtml5s.register()
// Convert the content to HTML using html5s converter.
const content = "Hello, *world!*!"
Expand Down
7 changes: 5 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ namespace :build do
dynamic_require_severity: :error,
})
builder.append_paths 'lib'
builder.build 'asciidoctor-html5s'
builder.build 'asciidoctor/html5s'

mkdir_p File.dirname(JS_FILE)
File.binwrite JS_FILE, builder.to_s
File.open(JS_FILE, 'w') do |file|
template = File.read('src/asciidoctor-html5s.tmpl.js')
file << template.sub('//OPAL-GENERATED-CODE//', builder.to_s)
end
File.binwrite "#{JS_FILE}.map", builder.source_map
end
end
Expand Down
9 changes: 6 additions & 3 deletions lib/asciidoctor/html5s.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'asciidoctor/extensions' unless RUBY_PLATFORM == 'opal'
require 'asciidoctor/html5s/version'
require 'asciidoctor/html5s/converter'
require 'asciidoctor/html5s/attached_colist_treeprocessor'

Asciidoctor::Extensions.register do
treeprocessor Asciidoctor::Html5s::AttachedColistTreeprocessor
unless RUBY_PLATFORM == 'opal'
require 'asciidoctor/extensions'

Asciidoctor::Extensions.register do
treeprocessor Asciidoctor::Html5s::AttachedColistTreeprocessor
end
end
4 changes: 3 additions & 1 deletion lib/asciidoctor/html5s/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

module Asciidoctor
module Html5s
VERSION = '0.1.0.beta.6'.freeze
VERSION = '0.1.0.beta.6'
end
end
95 changes: 95 additions & 0 deletions src/asciidoctor-html5s.tmpl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
(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, 'Html5s')
}
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')
}
}

/**
* @return A new instance of `Asciidoctor::Html5s::AttachedColistTreeprocessor`.
*/
function AttachedColistTreeprocessor () {
var processor = resolveModule('AttachedColistTreeprocessor').$new()
processor.process = processor.$process

return processor
}

/**
* @param {string} backend The backend name.
* @param {Object} opts The converter options.
* @return A new instance of `Asciidoctor::Html5s::Converter`.
*/
function Converter (backend, opts) {
var converter = resolveModule('Converter').$new(backend, Opal.hash(opts || {}))

converter.convert = function (node, transform, opts) {
return this.$convert(node, transform, Opal.hash(opts || {}))
}
return converter
}

/**
* @return {string} Version of this extension.
*/
function getVersion () {
return resolveModule().$$const.VERSION.toString()
}

/**
* Registers the HTML5s converter and the supporting extension into Asciidoctor.
*
* @param registry The Asciidoctor extensions registry to register the
* extension into. Defaults to the global Asciidoctor registry.
* @throws {TypeError} if the *registry* is invalid or Asciidoctor.js is not loaded.
*/
function register (registry) {
if (!registry) {
checkAsciidoctor()
registry = Opal.Asciidoctor.Extensions
}
var processor = AttachedColistTreeprocessor()

// 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 = {
AttachedColistTreeprocessor: AttachedColistTreeprocessor,
Converter: Converter,
getVersion: getVersion,
register: register,
}

if (typeof module !== 'undefined' && module.exports) {
module.exports = facade
}
return facade
})(Opal);

0 comments on commit 4702cb2

Please sign in to comment.