Sha256: 076f20034ef25b6b849293c92e02a688147d128222b81a52349b2ddc6b6058af

Contents?: true

Size: 932 Bytes

Versions: 1

Compression:

Stored size: 932 Bytes

Contents

module ReverseMarkdown
  module Converters
    class Text < Base
      def convert(node, options = {})
        if node.text.strip.empty?
          treat_empty(node)
        else
          treat_text(node)
        end
      end

      private

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

      def treat_text(node)
        text = remove_border_newlines(node.text)
        text = remove_inner_newlines(text)
        escape_keychars text
      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
    end

    register :text, Text.new
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reverse_markdown-0.6.0 lib/reverse_markdown/converters/text.rb