class ArticleJSON::Article

Attributes

additional_elements[R]
article_elements[R]

Public Class Methods

from_google_doc_html(html) click to toggle source

Build a new article from a Google Doc HTML export @return [ArticleJSON::Article]

# File lib/article_json/article.rb, line 116
def from_google_doc_html(html)
  parser = ArticleJSON::Import::GoogleDoc::HTML::Parser.new(html)
  new(parser.parsed_content)
end
from_hash(hash) click to toggle source

Build a new article from hash (like the one generated by to_h) @return [ArticleJSON::Article]

# File lib/article_json/article.rb, line 103
def from_hash(hash)
  hash = { content: hash } if hash.is_a?(Array)
  new(ArticleJSON::Elements::Base.parse_hash_list(hash[:content]))
end
from_json(json) click to toggle source

Build a new article from JSON (like the one generated by to_json) @return [ArticleJSON::Article]

# File lib/article_json/article.rb, line 110
def from_json(json)
  from_hash(JSON.parse(json, symbolize_names: true))
end
new(elements) click to toggle source

@param [Array] elements

# File lib/article_json/article.rb, line 6
def initialize(elements)
  @article_elements = elements
  @additional_elements = []
end

Public Instance Methods

amp_exporter() click to toggle source

Exporter instance for AMP @return [ArticleJSON::Export::AMP::Exporter]

# File lib/article_json/article.rb, line 55
def amp_exporter
  ArticleJSON::Export::AMP::Exporter.new(elements)
end
elements() click to toggle source

All elements of this article with optional additional elements placed in between @return [Array]

# File lib/article_json/article.rb, line 14
def elements
  @elements ||= begin
    if @additional_elements.any?
      ArticleJSON::Utils::AdditionalElementPlacer
        .new(@article_elements, @additional_elements)
        .merge_elements
    else
      @article_elements
    end
  end
end
facebook_instant_article_exporter() click to toggle source

Exporter instance for FacebookInstantArticle @return [ArticleJSON::Export::FacebookInstantArticle::Exporter]

# File lib/article_json/article.rb, line 67
def facebook_instant_article_exporter
  ArticleJSON::Export::FacebookInstantArticle::Exporter.new(elements)
end
html_exporter() click to toggle source

Exporter instance for HTML @return [ArticleJSON::Export::HTML::Exporter]

# File lib/article_json/article.rb, line 43
def html_exporter
  ArticleJSON::Export::HTML::Exporter.new(elements)
end
place_additional_elements(additional_elements) click to toggle source

Distribute passed elements evenly throughout the article. All passed elements need to have an exporter to be represented in the rendered article. If the method is called multiple times, the order of additional elements is maintained. @param [Object] additional_elements

# File lib/article_json/article.rb, line 94
def place_additional_elements(additional_elements)
  # Reset the `#elements` method memoization
  @elements = nil
  @additional_elements.concat(additional_elements)
end
plain_text_exporter() click to toggle source

Exporter instance for plain text @return [ArticleJSON::Export::PlainText::Exporter]

# File lib/article_json/article.rb, line 79
def plain_text_exporter
  ArticleJSON::Export::PlainText::Exporter.new(elements)
end
to_amp() click to toggle source

AMP export of the article @return [String]

# File lib/article_json/article.rb, line 61
def to_amp
  amp_exporter.html
end
to_facebook_instant_article() click to toggle source

FacebookInstantArticle export of the article @return [String]

# File lib/article_json/article.rb, line 73
def to_facebook_instant_article
  facebook_instant_article_exporter.html
end
to_h() click to toggle source

Hash representation of the article @return [Hash]

# File lib/article_json/article.rb, line 28
def to_h
  {
    article_json_version: VERSION,
    content: elements.map(&:to_h),
  }
end
to_html() click to toggle source

HTML export of the article @return [String]

# File lib/article_json/article.rb, line 49
def to_html
  html_exporter.html
end
to_json() click to toggle source

JSON representation of the article @return [String]

# File lib/article_json/article.rb, line 37
def to_json
  to_h.to_json
end
to_plain_text() click to toggle source

Plain text export of the article @return [String]

# File lib/article_json/article.rb, line 85
def to_plain_text
  plain_text_exporter.text
end