Sha256: c3c821af0bc2b7f8fe5f0ece4ed0783eb3e2fd27b56bdd49446b7704ad678dea

Contents?: true

Size: 1.59 KB

Versions: 38

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Stepmod
  module Utils
    module Converters
      class Text < ReverseAdoc::Converters::Base
        def convert(node, state = {})
          if node.text.strip.empty?
            treat_empty(node, state)
          else
            treat_text(node)
          end
        end

        private

        def treat_empty(node, state)
          parent = node.parent.name.to_sym
          if %i[ol ul].include?(parent) # Otherwise the identation is broken
            ""
          elsif state[:tdsinglepara]
            ""
          elsif node.text == " "          # Regular whitespace text node
            " "
          else
            ""
          end
        end

        def treat_text(node)
          text = node.text
          text = preserve_nbsp(text)
          text = remove_inner_newlines(text)
          text = remove_border_newlines(text)

          text = preserve_keychars_within_backticks(text)
          preserve_tags(text)
        end

        def preserve_nbsp(text)
          text.gsub(/\u00A0/, "&nbsp;")
        end

        def preserve_tags(text)
          text.gsub(/[<>]/, ">" => '\>', "<" => '\<')
        end

        def remove_border_newlines(text)
          text.gsub(/\A\n+/, "").gsub(/\n+\z/, "")
        end

        def remove_inner_newlines(text)
          text.tr("\n\t", " ").squeeze(" ")
        end

        def preserve_keychars_within_backticks(text)
          text.gsub(/`.*?`/) do |match|
            match.gsub('\_', "_").gsub('\*', "*")
          end
        end
      end

      ReverseAdoc::Converters.register :text, Text.new
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
stepmod-utils-0.4.8 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.4.7 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.4.6 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.4.5 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.4.2 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.4.1 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.4.0 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.37 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.36 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.35 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.34 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.33 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.32 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.31 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.30 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.29 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.28 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.27 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.26 lib/stepmod/utils/converters/text.rb
stepmod-utils-0.3.25 lib/stepmod/utils/converters/text.rb