Sha256: fe8114b72ecc3c7903aaea63db836fe1927fe726df34d5169ce6cc9e7b6489a6

Contents?: true

Size: 1.29 KB

Versions: 14

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Stepmod
  module Utils
    module Converters
      class A < Stepmod::Utils::Converters::Base
        def convert(node, state = {})
          name  = treat_children(node, state)
          href  = node["href"]
          title = extract_title(node)
          id = node["id"] || node["name"]

          id = id&.gsub(/\s/, "")&.gsub(/__+/, "_")

          if /^_Toc\d+$|^_GoBack$/.match? id
            ""
          elsif !id.nil? && !id.empty?
            "[[#{id}]]"
          elsif href.to_s.start_with?("#")
            href = href.sub(/^#/, "").gsub(/\s/, "").gsub(/__+/, "_")
            if name.empty? || number_with_optional_prefix?(name)
              "<<#{href}>>"
            else
              "<<#{href},#{name}>>"
            end
          elsif href.to_s.empty?
            name
          else
            name = title if name.empty?
            href = "link:#{href}" unless href.to_s&.match?(URI::DEFAULT_PARSER.make_regexp)
            link = href
            link += "[#{name}]" unless number_with_optional_prefix?(name)
            " #{link}"
          end
        end

        private

        def number_with_optional_prefix?(name)
          /.*?\s*\(?\d+\)?/.match(name)
        end
      end

      ReverseAdoc::Converters.register :a, A.new
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
stepmod-utils-0.6.7 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.6.6 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.6.5 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.6.3 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.6.2 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.6.1 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.6.0 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.5.0 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.4.14 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.4.13 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.4.12 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.4.11 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.4.10 lib/stepmod/utils/converters/a.rb
stepmod-utils-0.4.9 lib/stepmod/utils/converters/a.rb