Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
Make it so
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Sep 14, 2016
1 parent 6956bae commit f725050
Show file tree
Hide file tree
Showing 16 changed files with 656 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
build/
npm-debug.log
12 changes: 6 additions & 6 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License
The MIT License

Copyright (c) 2016 Guillaume Grossetie
Copyright (C) 2016 Guillaume Grossetie

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
2 changes: 2 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generic template backend for Asciidoctor.js
Guillaume Grossetie <https://github.com/mogztter[@mogztter]>
2 changes: 2 additions & 0 deletions lib/asciidoctor/core_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'asciidoctor/core_ext/factory'
require 'asciidoctor/core_ext/template'
50 changes: 50 additions & 0 deletions lib/asciidoctor/core_ext/factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# encoding: UTF-8
module Asciidoctor
# Monkey patch the create method to add JavaScript slide converters
module Converter
class Factory
def create backend, opts = {}
if (converter = resolve backend)
if converter.is_a? ::Class
return converter.new backend, opts
else
return converter
end
end

base_converter = case backend
when 'html5'
unless defined? ::Asciidoctor::Converter::Html5Converter
require 'asciidoctor/converter/html5'.to_s
end
Html5Converter.new backend, opts
when 'docbook5'
unless defined? ::Asciidoctor::Converter::DocBook5Converter
require 'asciidoctor/converter/docbook5'.to_s
end
DocBook5Converter.new backend, opts
when 'docbook45'
unless defined? ::Asciidoctor::Converter::DocBook45Converter
require 'asciidoctor/converter/docbook45'.to_s
end
DocBook45Converter.new backend, opts
when 'manpage'
unless defined? ::Asciidoctor::Converter::ManPageConverter
require 'asciidoctor/converter/manpage'.to_s
end
ManPageConverter.new backend, opts
end

return base_converter unless opts.key? :template_dirs

unless defined? ::Asciidoctor::Converter::TemplateConverter
require 'asciidoctor/converter/template'.to_s
end
unless defined? ::Asciidoctor::Converter::CompositeConverter
require 'asciidoctor/converter/composite'.to_s
end
TemplateConverter.new backend, opts[:template_dirs], opts
end
end
end
end
58 changes: 58 additions & 0 deletions lib/asciidoctor/core_ext/template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Asciidoctor

class Converter::TemplateConverter < Converter::Base
def initialize backend, template_dirs, opts = {}
@backend = backend
@engine = 'jade' # Only Jade/Pug templates are supported for now
if template_dirs.kind_of?(String)
@template_dirs = [template_dirs]
else
@template_dirs = template_dirs
end
end

def resolve_template name
path_resolver = PathResolver.new
backend = @backend
engine = @engine
@template_dirs.each do |template_dir|
# FIXME need to think about safe mode restrictions here
next unless ::File.directory?(template_dir = (path_resolver.system_path template_dir, nil))

# NOTE last matching template wins for template name if no engine is given
if engine
# example: templates/haml
if ::File.directory?(engine_dir = (::File.join template_dir, engine))
template_dir = engine_dir
end
end

# example: templates/html5 or templates/haml/html5
if ::File.directory?(backend_dir = (::File.join template_dir, backend))
template_dir = backend_dir
end

template = ::File.join(template_dir, name + "." + engine)
return ::File.read(template) if ::File.exist?(template);
end
return nil
end

def convert node, template_name = nil, opts = {}
template_name ||= node.node_name
unless (template = resolve_template template_name)
raise %(Could not find a custom template to handle transform: #{template_name})
end

%x(
if (typeof window !== 'undefined') {
var jade = jade || window.jade;
} else if (typeof require !== 'undefined') {
var jade = jade || require('./jade.js') || require('jade');
}
var compiled = jade.compile(#{template}, {pretty: true});
return compiled({ node: #{node} });
)
end
end
end
4 changes: 4 additions & 0 deletions npm/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var Builder = require('./builder.js');
var builder = new Builder();

builder.build();
Loading

0 comments on commit f725050

Please sign in to comment.