Sha256: ccbc233311e372855ba8e321f34920e7cc273c5eb4de92128891f0d05d90c298

Contents?: true

Size: 1.24 KB

Versions: 19

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Stepmod
  module Utils
    module Converters
      class Strong < ReverseAdoc::Converters::Base
        BLANK_CHARS = "{blank}"

        def convert(node, state = {})
          content = treat_children(node, state.merge(already_strong: true))
          strong_tag = state[:non_flanking_whitesapce] ? '**' : '*'
          if content.strip.empty? || state[:already_strong]
            content
          else
            handle_express_escape_seq(
              node,
              "#{content[/^\s*/]}#{strong_tag}#{content.strip}#{strong_tag}#{content[/\s*$/]}"
            )
          end
        end

        private

        def handle_express_escape_seq(node, content)
          res = content
          if braces_sibling?(node.previous, true)
            res = "#{BLANK_CHARS}#{res}"
          end
          if braces_sibling?(node.next)
            res = "#{res}#{BLANK_CHARS}"
          end
          res
        end

        def braces_sibling?(sibling, end_of_text = false)
          match = end_of_text ? /\($/ : /^\)/
          sibling&.text? && sibling.text =~ match
        end
      end

      ReverseAdoc::Converters.register :strong, Strong.new
      ReverseAdoc::Converters.register :b,      Strong.new
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
stepmod-utils-0.3.31 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.30 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.29 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.28 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.27 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.26 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.25 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.24 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.23 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.22 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.21 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.20 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.19 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.18 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.17 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.16 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.15 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.14 lib/stepmod/utils/converters/strong.rb
stepmod-utils-0.3.13 lib/stepmod/utils/converters/strong.rb