Sha256: 9afdb04c626d11728742b00fa14973843beed06e1deaf5e4ae12ed1f85c3c923

Contents?: true

Size: 920 Bytes

Versions: 4

Compression:

Stored size: 920 Bytes

Contents

require 'reverse_markdown'

module Contentful
  module Converter
    class MarkupConverter
      Encoding.default_external = 'utf-8'

      attr_reader :config, :logger

      def initialize(config)
        @config = config
        @logger = Logger.new(STDOUT)
      end

      def convert_markup_to_markdown
        Dir.glob("#{config.entries_dir}/post/*") do |post_file_path|
          logger.info("Converting #{post_file_path} markups...")
          convert_post_content(post_file_path)
        end
      end

      def convert_post_content(post_file_path)
        post_data = JSON.parse(File.read(post_file_path))
        post_data['content'] = ReverseMarkdown.convert post_data['content']
        overwrite_file(post_file_path, post_data)
      end

      def overwrite_file(path, data)
        File.open(path, 'w') do |file|
          file.write(JSON.pretty_generate(data))
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wordpress-exporter-0.2.0 lib/converters/markup_converter.rb
wordpress-exporter-0.1.0 lib/converters/markup_converter.rb
wordpress-exporter-0.0.2 lib/converters/markup_converter.rb
wordpress-exporter-0.0.1 lib/converters/markup_converter.rb