Sha256: b998a7119d8560b9cb70e23a5aa54234a2b79e8f11f54fd14557829a910ccb0a

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# -*- coding: utf-8 -*-
#
# https://github.com/gettalong/kramdown/pull/219/commits/8dbaf6e2987b283cf82594bae4e95c7ebd5d422d
#--
# Copyright (C) 2009-2014 Thomas Leitner <t_leitner@gmx.at>
#
#
# This file is part of kramdown which is licensed under the MIT.
#++
#

require 'kramdown'
require 'kramdown/converter'

module Kramdown
  module Converter

    # Converts tree to plain text. Removes all formatting and attributes.
    # This converter can be used to generate clean text for use in meta tags,
    # plain text emails, etc.
    class PlainText < Base

      TEXT_TYPES ||= [
          :smart_quote,
          :typographic_sym,
          :entity,
          :text
      ].freeze

      def initialize(root, options)
        super
        @plain_text = '' # bin for plain text
      end

      def convert(el)
        type = el.type
        category = ::Kramdown::Element.category(el)

        @plain_text << convert_type(type, el) if TEXT_TYPES.include?(type)
        @plain_text << "\n" if category == :block

        el.children.each { |e| convert(e) }

        @plain_text.strip if type == :root
      end

      def convert_type(type, el)
        send("convert_#{type}", el)
      end

      def convert_text(el)
        el.value
      end

      def convert_entity(el)
        el.value.char
      end

      def convert_smart_quote(el)
        smart_quote_entity(el).char
      end

      def convert_typographic_sym(el)
        ::Kramdown::Converter::Html::TYPOGRAPHIC_SYMS[el.value]
            .map(&:char)
            .join('')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
liquid_markdown-0.2.1 lib/liquid_markdown/converter/plain_text.rb