lib/kramdown/converter/base.rb in kramdown-1.17.0 vs lib/kramdown/converter/base.rb in kramdown-2.0.0.beta1

- old
+ new

@@ -1,9 +1,9 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8; frozen_string_literal: true -*- # #-- -# Copyright (C) 2009-2016 Thomas Leitner <t_leitner@gmx.at> +# Copyright (C) 2009-2019 Thomas Leitner <t_leitner@gmx.at> # # This file is part of kramdown which is licensed under the MIT. #++ # @@ -99,22 +99,28 @@ # 4. Check if the template name starts with 'string://' and if so, strip this prefix away and # use the rest as template. def self.convert(tree, options = {}) converter = new(tree, ::Kramdown::Options.merge(options.merge(tree.options[:options] || {}))) - apply_template(converter, '') if !converter.options[:template].empty? && converter.apply_template_before? + if !converter.options[:template].empty? && converter.apply_template_before? + apply_template(converter, '') + end result = converter.convert(tree) - result.encode!(tree.options[:encoding]) if result.respond_to?(:encode!) && result.encoding != Encoding::BINARY - result = apply_template(converter, result) if !converter.options[:template].empty? && converter.apply_template_after? + if result.respond_to?(:encode!) && result.encoding != Encoding::BINARY + result.encode!(tree.options[:encoding]) + end + if !converter.options[:template].empty? && converter.apply_template_after? + result = apply_template(converter, result) + end [result, converter.warnings] end # Convert the element +el+ and return the resulting object. # # This is the only method that has to be implemented by sub-classes! - def convert(el) + def convert(_el) raise NotImplementedError end # Apply the +template+ using +body+ as the body string. # @@ -123,32 +129,15 @@ def self.apply_template(converter, body) # :nodoc: erb = ERB.new(get_template(converter.options[:template])) obj = Object.new obj.instance_variable_set(:@converter, converter) obj.instance_variable_set(:@body, body) - erb.result(obj.instance_eval{binding}) + erb.result(obj.instance_eval { binding }) end # Return the template specified by +template+. - def self.get_template(template) - #DEPRECATED: use content of #get_template_new in 2.0 - format_ext = '.' + self.name.split(/::/).last.downcase - shipped = File.join(::Kramdown.data_dir, template + format_ext) - if File.exist?(template) - File.read(template) - elsif File.exist?(template + format_ext) - File.read(template + format_ext) - elsif File.exist?(shipped) - File.read(shipped) - elsif template.start_with?('string://') - template.sub(/\Astring:\/\//, '') - else - get_template_new(template) - end - end - - def self.get_template_new(template) # :nodoc: + def self.get_template(template) # :nodoc: format_ext = '.' + ::Kramdown::Utils.snake_case(self.name.split(/::/).last) shipped = File.join(::Kramdown.data_dir, template + format_ext) if File.exist?(template) File.read(template) elsif File.exist?(template + format_ext) @@ -230,14 +219,14 @@ # Uses the option +auto_id_prefix+: the value of this option is prepended to every generated # ID. def generate_id(str) str = ::Kramdown::Utils::Unidecoder.decode(str) if @options[:transliterated_header_ids] gen_id = basic_generate_id(str) - gen_id = 'section' if gen_id.length == 0 + gen_id = 'section' if gen_id.empty? @used_ids ||= {} - if @used_ids.has_key?(gen_id) - gen_id += '-' << (@used_ids[gen_id] += 1).to_s + if @used_ids.key?(gen_id) + gen_id += "-#{@used_ids[gen_id] += 1}" else @used_ids[gen_id] = 0 end @options[:auto_id_prefix] + gen_id end @@ -250,10 +239,10 @@ gen_id.tr!(' ', '-') gen_id.downcase! gen_id end - SMART_QUOTE_INDICES = {:lsquo => 0, :rsquo => 1, :ldquo => 2, :rdquo => 3} # :nodoc: + SMART_QUOTE_INDICES = {lsquo: 0, rsquo: 1, ldquo: 2, rdquo: 3} # :nodoc: # Return the entity that represents the given smart_quote element. def smart_quote_entity(el) res = @options[:smart_quotes][SMART_QUOTE_INDICES[el.value]] ::Kramdown::Utils::Entities.entity(res)