Sha256: ce9cebf1cdaf6123ece2b47adf344030745933e83c04f5ab160a05a3affd44b4

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module WP2Middleman
  class MiddlemanPost
    def initialize(wp_post, body_to_markdown: false, include_fields: [])
      @wp_post = wp_post
      @body_to_markdown = body_to_markdown
      @include_fields = include_fields
    end

    def title
      wp_post.title
    end

    def title_for_filename
      title.gsub(/[^\w\s_-]+/, '')
        .gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
        .gsub(/\s+/, '-')
    end

    def filename
      "#{date_published}-#{title_for_filename}"
    end

    def date_published
      wp_post.date_published
    end

    def full_filename output_path
      "#{output_path}#{filename}.html.markdown"
    end

    def file_content
      <<-EOS.gsub(/^ {8}/, '')
        #{frontmatter.to_yaml}
        ---

        #{formatted_post_content}
      EOS
    end

    def formatted_post_content
      if body_to_markdown
        markdown_content
      else
        content
      end
    end

    def content
      wp_post.content
    end

    def markdown_content
      html = HTMLPage.new :contents => content
      html.comment do |node,_|
        "#{node}"
      end
      html.iframe do |node,_|
        "#{node}"
      end
      html.markdown
    end

    private

    attr_reader :wp_post, :body_to_markdown, :include_fields

    def frontmatter
      @frontmatter ||= Frontmatter.new(wp_post, include_fields: include_fields)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wp2middleman-0.0.3 lib/wp2middleman/middleman_post.rb
wp2middleman-0.0.2 lib/wp2middleman/middleman_post.rb