Skip to content

Commit

Permalink
Prefix convert methods with "convert_", require asciidoctor ~> 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Aug 27, 2022
1 parent f04b1d1 commit f4eee46
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $ bundle install
The main entry point is method `Asciidoctor::TemplatesCompiler::Slim#compile_converter` (for Slim) that accepts the following keyword arguments.

backend_info::
A hash of keys for `backend_info`: `basebackend`, `outfilesuffix`, `filetype`, `htmlsyntax`, `supports_templates` (supported since Asciidoctor ≥ 1.5.7).
A hash of keys for `backend_info`: `basebackend`, `outfilesuffix`, `filetype`, `htmlsyntax`, `supports_templates`.

class_name::
Full name of the converter class to generate (e.g. `My::HTML::Converter`).
Expand Down
2 changes: 1 addition & 1 deletion asciidoctor-templates-compiler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Gem::Specification.new do |s|

s.required_ruby_version = '>= 2.1'

s.add_runtime_dependency 'asciidoctor', '>= 1.5.0', '< 2.1'
s.add_runtime_dependency 'asciidoctor', '~> 2.0'
s.add_runtime_dependency 'corefines', '~> 1.2'
s.add_runtime_dependency 'docopt', '~> 0.6'
s.add_runtime_dependency 'slim', '>= 2.1', '< 5.0'
Expand Down
3 changes: 1 addition & 2 deletions bin/asciidoctor-templates-compiler
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ HELP_MSG = <<-EOF.unindent
Options:
-b --backend-info KEY=VAL[,...] Parameters for backend_info: basebackend, outfilesuffix,
filetype, htmlsyntax, and supports_templates (requires
Asciidoctor >= 1.5.7).
filetype, htmlsyntax, and supports_templates.
-n --class-name NAME Full name of the converter class to generate (e.g.
My::HTML::Converter) [default: Converter].
Expand Down
20 changes: 5 additions & 15 deletions lib/asciidoctor/templates_compiler/converter_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def generate(out = StringIO.new)
out << helpers_code << "\n" unless @helpers_code.blank?
out << initialization_code << "\n"
out << convert_method_code << "\n"
out << handles_method_code << "\n"
transform_methods_code(out)
out << support_methods_code << "\n"
out << tail_code
Expand Down Expand Up @@ -151,35 +150,26 @@ def initialization_code

def convert_method_code
converter = if @delegate_backend
'respond_to?(transform) ? self : @delegate_converter'
'respond_to?(meth_name) ? self : @delegate_converter'
else
'self'
end

<<-EOF.reindent(2)
def convert(node, transform = nil, opts = {})
transform ||= node.node_name
meth_name = "convert_\#{transform || node.node_name}"
opts ||= {}
converter = #{converter}
if opts.empty?
converter.send(transform, node)
converter.send(meth_name, node)
else
converter.send(transform, node, opts)
converter.send(meth_name, node, opts)
end
end
EOF
end

# This is for compatibility with Asciidoctor >=2.0.0 (see #5).
def handles_method_code
<<-EOF.reindent(2)
def handles?(transform)
respond_to?("convert_\#{transform}") || respond_to?(transform)
end
EOF
end

def support_methods_code
return '' if @ignore_convert_opts

Expand All @@ -197,7 +187,7 @@ def transform_methods_code(out)

@transforms_code.each do |name, code|
out << "\n"
out << " def #{name}(node, opts = {})\n"
out << " def convert_#{name}(node, opts = {})\n"
out << " node.extend(Helpers)\n" unless @helpers_code.blank?
out << " node.instance_eval do\n"
out << " converter.set_local_variables(binding, opts) unless opts.empty?\n" unless @ignore_convert_opts # rubocop:disable LineLength
Expand Down
20 changes: 6 additions & 14 deletions spec/templates_compiler/converter_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ def set_local_variables(binding, vars)
EOF
end

it 'defines method handles?' do
is_expected.to include <<-EOF.reindent(2)
def handles?(transform)
respond_to?("convert_\#{transform}") || respond_to?(transform)
end
EOF
end

context 'when class_name' do
shared_examples :converter_class do
it 'declares converter class extending ::Asciidoctor::Converter::Base' do
Expand Down Expand Up @@ -170,7 +162,7 @@ def handles?(transform)
it 'defines correct transform method for each item' do
transforms_code.each do |name, code|
is_expected.to include <<-EOF.unindent.%(code.indent(4)).indent(2)
def #{name}(node, opts = {})
def convert_#{name}(node, opts = {})
node.extend(Helpers)
node.instance_eval do
converter.set_local_variables(binding, opts) unless opts.empty?
Expand All @@ -185,7 +177,7 @@ def #{name}(node, opts = {})
context 'and helpers_code is blank' do
it 'does not extend node with Helpers in transform methods' do
transforms_code.each do |name, _|
is_expected.to_not match(/def #{name}\([^)]*\).*?\.extend\(Helpers\).*?\bend\b/m)
is_expected.to_not match(/def convert_#{name}\([^)]*\).*?\.extend\(Helpers\).*?\bend\b/m)
end
end
end
Expand Down Expand Up @@ -296,14 +288,14 @@ def initialize(backend, opts = {})

let(:convert_method_code) do <<-EOF.reindent(2)
def convert(node, transform = nil, opts = {})
transform ||= node.node_name
meth_name = "convert_\#{transform || node.node_name}"
opts ||= {}
converter = %s
if opts.empty?
converter.send(transform, node)
converter.send(meth_name, node)
else
converter.send(transform, node, opts)
converter.send(meth_name, node, opts)
end
end
EOF
Expand Down Expand Up @@ -342,7 +334,7 @@ def initialize(backend, opts = {})

it 'declares method #convert that uses @delegate_converter' do
is_expected.to include \
convert_method_code % 'respond_to?(transform) ? self : @delegate_converter'
convert_method_code % 'respond_to?(meth_name) ? self : @delegate_converter'
end
end
end
Expand Down

0 comments on commit f4eee46

Please sign in to comment.