asciidoctor-templates-compiler 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 367bbbb5d5b7d7f38e994e845a65921592fe9036
4
- data.tar.gz: f941a11c99f96177f4a251cc8f15820670c348b3
3
+ metadata.gz: ad02dd81d47a7185c55b040d6a80af3f6fa5ba24
4
+ data.tar.gz: 26b3aaa536e7f76843fa4f7036c9d8d1901e4453
5
5
  SHA512:
6
- metadata.gz: dc05c92e8548eb23e49de8a8d6dee7d67b985b0c5ce74af785c0d0f41d360f9ffb4c0d3708b5ccf3323db5c441bde2a6536a6de70cd67296923f21f3fbf0b2a0
7
- data.tar.gz: c466abc187ea3d4d7b781caad7b061db1873f37cbaa0109538c1506f2c76ad268db7a5bcde865674af92e19c0a60c409470f951d2d0895361a3f8363fadc2a19
6
+ metadata.gz: 5190bb978d148d33659d16ce2065f6455e7da03322fe8c45979403ef6f4fc1f60baf27765d5900e16d04019466ff3e55169301172f897715d55772d903f1423e
7
+ data.tar.gz: 27cb8c19f213138b057d998d8f1a53eae0a03798ba0d508053ae651750ecbeec2b321d3d922bf4ef32db521c3b36b7ff34d9120f2400ecccafa6147563c391e5
data/README.adoc CHANGED
@@ -2,7 +2,7 @@
2
2
  :source-language: ruby
3
3
  // custom
4
4
  :gem-name: asciidoctor-templates-compiler
5
- :gem-version: 0.1.3
5
+ :gem-version: 0.2.0
6
6
  :gh-name: jirutka/{gem-name}
7
7
  :gh-branch: master
8
8
  :codacy-id: b23b8c6503474ea5b13537eaef0c73d5
@@ -43,9 +43,8 @@ $ bundle install
43
43
 
44
44
  The main entry point is method `Asciidoctor::TemplatesCompiler::Slim#compile_converter` (for Slim) that accepts the following keyword arguments.
45
45
 
46
- templates_dir::
47
- Path of the directory where to look for templates (`*.slim` files not starting with `_`, in the case of Slim) and (optional) `helpers.rb`.
48
- This argument is **required**.
46
+ backend_info::
47
+ A hash of keys for `backend_info`: `basebackend`, `outfilesuffix`, `filetype`, `htmlsyntax`.
49
48
 
50
49
  class_name::
51
50
  Full name of the converter class to generate (e.g. `My::HTML::Converter`).
@@ -55,20 +54,24 @@ delegate_backend::
55
54
  Name of the backend (converter) to use as a fallback for AST nodes not supported by your converter.
56
55
  If not specified (default), no fallback will be used and converter will raise `NoMethodError` when it try to convert an unsupported node.
57
56
 
58
- register_for::
59
- An array of backend names that the generated converter should be registered in Asciidoctor to handle.
60
- Default is empty.
57
+ engine_opts::
58
+ A Hash of options to pass into the templating engine that compiles templates into Ruby code.
61
59
 
62
- backend_info::
63
- A hash of keys for `backend_info`: `basebackend`, `outfilesuffix`, `filetype`, `htmlsyntax`.
60
+ output::
61
+ An output stream (`IO` object like opened file, `$stdout`, …) to write the generated converter into.
62
+ Default is `StringIO.new` (it’s the return value of `#compile_converter`).
64
63
 
65
64
  pretty::
66
65
  Enable pretty-formatting of the generated Ruby code (generated by Slim/Temple)?
67
66
  Default is `false`.
68
67
 
69
- output::
70
- An output stream (`IO` object like opened file, `$stdout`, …) to write the generated converter into.
71
- Default is `StringIO.new` (it’s the return value of `#compile_converter`).
68
+ register_for::
69
+ An array of backend names that the generated converter should be registered in Asciidoctor to handle.
70
+ Default is empty.
71
+
72
+ templates_dir::
73
+ Path of the directory where to look for templates (`*.slim` files not starting with `_`, in the case of Slim) and (optional) `helpers.rb`.
74
+ This argument is **required**.
72
75
 
73
76
 
74
77
  === Examples
@@ -14,16 +14,15 @@ module Asciidoctor::TemplatesCompiler
14
14
  alias call compile_converter
15
15
  end
16
16
 
17
- def compile_converter(output: StringIO.new, templates_dir:, pretty: false, **opts)
17
+ def compile_converter(templates_dir:, engine_opts: {}, pretty: false, **opts)
18
18
  unless Dir.exist? templates_dir
19
19
  raise "Templates directory '#{templates_dir}' does not exist"
20
20
  end
21
21
 
22
- backend_info = opts[:backend_info] || {}
23
22
  templates = find_templates(templates_dir)
24
- transforms_code = compile_templates(templates, backend_info: backend_info, pretty: pretty)
23
+ transforms_code = compile_templates(templates, engine_opts, pretty: pretty)
25
24
 
26
- generate_class(output: output, transforms_code: transforms_code,
25
+ generate_class(transforms_code: transforms_code,
27
26
  helpers_code: read_helpers(templates_dir), **opts)
28
27
  end
29
28
 
@@ -32,7 +31,7 @@ module Asciidoctor::TemplatesCompiler
32
31
  protected
33
32
 
34
33
  # @abstract
35
- def compile_template(filename, backend_info: {})
34
+ def compile_template(filename, engine_opts = {})
36
35
  end
37
36
 
38
37
  # @abstract
@@ -43,9 +42,9 @@ module Asciidoctor::TemplatesCompiler
43
42
  RubyBeautify.call(code, **opts)
44
43
  end
45
44
 
46
- def compile_templates(template_files, backend_info: {}, pretty: false)
45
+ def compile_templates(template_files, engine_opts = {}, pretty: false)
47
46
  template_files.lazy.map do |path|
48
- code = compile_template(path, backend_info: backend_info)
47
+ code = compile_template(path, engine_opts)
49
48
  code = beautify_code(code) if pretty
50
49
 
51
50
  [transform_name_from_tmpl_name(path), code]
@@ -58,7 +57,7 @@ module Asciidoctor::TemplatesCompiler
58
57
 
59
58
  def read_helpers(templates_dir)
60
59
  path = File.join(templates_dir, 'helpers.rb')
61
- IO.read(path) if File.exist? path
60
+ File.exist?(path) ? IO.read(path) : ''
62
61
  end
63
62
 
64
63
  def transform_name_from_tmpl_name(filename)
@@ -6,6 +6,7 @@ require 'stringio'
6
6
  module Asciidoctor::TemplatesCompiler
7
7
  class ConverterGenerator
8
8
  using Corefines::String::indent
9
+ using Corefines::Object::blank?
9
10
 
10
11
  class << self
11
12
  def generate(output: StringIO.new, **opts)
@@ -20,14 +21,18 @@ module Asciidoctor::TemplatesCompiler
20
21
  @class_name = class_name
21
22
  @transforms_code = transforms_code
22
23
  @helpers_code = helpers_code
23
- @register_for = register_for
24
+ @register_for = Array(register_for)
24
25
  @backend_info = backend_info
25
26
  @delegate_backend = delegate_backend
27
+
28
+ if !helpers_code.blank? && helpers_code !~ /\bmodule Helpers[\s#]/
29
+ raise ArgumentError, 'The helpers_code does not contain module Helpers'
30
+ end
26
31
  end
27
32
 
28
33
  def generate(out = StringIO.new)
29
34
  out << head_code << "\n"
30
- out << helpers_code << "\n" if @helpers_code
35
+ out << helpers_code << "\n" unless @helpers_code.blank?
31
36
  out << initialization_code << "\n"
32
37
  out << convert_method_code << "\n"
33
38
  transform_methods_code(out)
@@ -142,7 +147,7 @@ module Asciidoctor::TemplatesCompiler
142
147
  out << <<~EOF.indent(2, ' ')
143
148
 
144
149
  def #{name}(node, opts = {})
145
- node.extend(Helpers)
150
+ #{' node.extend(Helpers)' unless @helpers_code.blank?}
146
151
  node.instance_eval do
147
152
  converter.set_local_variables(binding, opts) unless opts.empty?
148
153
  #{code.indent(6, ' ')}
@@ -3,22 +3,29 @@ require 'asciidoctor/converter'
3
3
  require 'asciidoctor/converter/template'
4
4
  require 'asciidoctor/templates_compiler/version'
5
5
  require 'asciidoctor/templates_compiler/base'
6
+ require 'asciidoctor/templates_compiler/temple_ext'
6
7
  require 'corefines'
7
8
  require 'slim'
8
9
  require 'slim/include'
9
10
 
10
11
  module Asciidoctor::TemplatesCompiler
11
12
  class Slim < Base
12
- using Corefines::Object::then
13
+
14
+ DEFAULT_ENGINE_OPTS =
15
+ ::Asciidoctor::Converter::TemplateConverter::DEFAULT_ENGINE_OPTIONS[:slim].dup.freeze
16
+
17
+ def compile_converter(backend_info: {}, engine_opts: {}, **)
18
+ engine_opts[:format] ||= backend_info.fetch('htmlsyntax', 'html').to_sym
19
+ super
20
+ end
13
21
 
14
22
  protected
15
23
 
16
- def compile_template(filename, backend_info: {})
17
- htmlsyntax = backend_info[:htmlsyntax] || backend_info['htmlsyntax'] || :html
18
- opts = engine_options.merge(file: filename, format: htmlsyntax.to_sym)
24
+ def compile_template(filename, engine_opts = {})
25
+ engine_opts = DEFAULT_ENGINE_OPTS.merge(**engine_opts, file: filename)
19
26
  content = IO.read(filename)
20
27
 
21
- ::Slim::Engine.new(opts).call(content).tap do |code|
28
+ ::Slim::Engine.new(engine_opts).call(content).tap do |code|
22
29
  code.scan(/::(?:Slim|Temple)(?:\:\:\w+)*/).uniq.each do |name|
23
30
  $stderr.puts "WARNING: Compiled template '#{filename}' references constant #{name}"
24
31
  end
@@ -30,11 +37,7 @@ module Asciidoctor::TemplatesCompiler
30
37
  end
31
38
 
32
39
  def read_helpers(templates_dir)
33
- super.then { |s| s.sub('module Slim::Helpers', 'module Helpers') }
34
- end
35
-
36
- def engine_options
37
- ::Asciidoctor::Converter::TemplateConverter::DEFAULT_ENGINE_OPTIONS[:slim]
40
+ super.sub('module Slim::Helpers', 'module Helpers')
38
41
  end
39
42
  end
40
43
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ require 'temple'
3
+
4
+ module Temple
5
+ # Monkey-patch Temple::Generator to fix bugs/inconviences waiting to be
6
+ # merged into upstream.
7
+ class Generator
8
+
9
+ # XXX: Remove after https://github.com/judofyr/temple/pull/113 is merged.
10
+ def initialize(opts = {})
11
+ self.class.options[:capture_generator] = self.class
12
+ super
13
+ end
14
+
15
+ # XXX: Remove after https://github.com/judofyr/temple/pull/112 is merged.
16
+ def on_capture(name, exp)
17
+ capture_generator.new(**options, buffer: name).call(exp)
18
+ end
19
+ end
20
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Asciidoctor
4
4
  module TemplatesCompiler
5
- VERSION = '0.1.3'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-templates-compiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Jirutka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-27 00:00:00.000000000 Z
11
+ date: 2017-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -143,6 +143,7 @@ files:
143
143
  - lib/asciidoctor/templates_compiler/converter_generator.rb
144
144
  - lib/asciidoctor/templates_compiler/ruby_beautify.rb
145
145
  - lib/asciidoctor/templates_compiler/slim.rb
146
+ - lib/asciidoctor/templates_compiler/temple_ext.rb
146
147
  - lib/asciidoctor/templates_compiler/version.rb
147
148
  homepage: https://github.com/jirutka/asciidoctor-templates-compiler
148
149
  licenses:
@@ -164,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
165
  version: '0'
165
166
  requirements: []
166
167
  rubyforge_project:
167
- rubygems_version: 2.6.11
168
+ rubygems_version: 2.6.13
168
169
  signing_key:
169
170
  specification_version: 4
170
171
  summary: Compile templates-based Asciidoctor converter (backend) into a single Ruby