article_json
The article_json
gem is a Ruby library designed to simplify the conversion and manipulation of structured articles in various formats, allowing easy importing, manipulation and export of content across different platforms and environments.
It takes an article from a Google Doc and creates a JSON version of it.
From there it can export the article:
- as HTML
- as AMP
- as Apple News Format (ANF)
- as Facebook Instant Article HTML
- as plain text
- as JSON
It also provides functionalities to parse content from Google Document HTML exports and initialize articles from JSON strings or already parsed JSON.
Status
Usage
First, install the gem with gem install article_json
or add it to your Gemfile
via gem 'article_json'
.
Ruby
require 'article_json'
article = ArticleJSON::Article.from_google_doc_html(google_doc_html)
article = ArticleJSON::Article.from_json(json_string)
article = ArticleJSON::Article.from_hash(parsed_json)
puts article.to_html
puts article.to_amp
puts article.amp_exporter.amp_libraries
puts article.to_apple_news
puts article.to_facebook_instant_article
puts article.to_plain_text
puts article.to_json
CLI
To load, parse and export the latest (amp/ apple news/ facebook/ html) version of the reference document, run the following:
$ export DOC_ID=1E4lncZE2jDkbE34eDyYQmXKA9O26BHUiwguz4S9qyE8
$ ./bin/article_json_export_google_doc.rb $DOC_ID \
| ./bin/article_json_parse_google_doc.rb \
| ./bin/article_json_export_html.rb
Alternatively, you can concatenate your command line commands, like so:
$ ./bin/article_json_export_google_doc.rb $DOC_ID > test_ref_doc.html
$ cat test_ref_doc.html | bin/article_json_parse_google_doc.rb > \
test_ref_doc_parsed_apple.json
$ cat test_ref_doc_parsed_apple.json | bin/article_json_export_apple_news.rb > \
test_ref_doc_exported_apple.json
You can also update all the different exported versions of the reference document (amp, apple_news, facebook, google_doc, html and plain_text) by running the following command:
$ ./bin/update_reference_document.sh
When running the tests, we use some fixtures to mock the responses for oembed requests, but these may change over time.
To update them, run:
$ ./bin/update_oembed_request-stubs.sh
Configuration
Some configuration options allow a more tailored usage of the article_json
gem.
The following code snippet gives an example for every available setting:
ArticleJSON.configure do |config|
config.oembed_user_agent = 'devex oembed (+https://www.devex.com/)'
config.register_element_exporters(
:html,
advertisement: ArticleJSON::Export::HTML::Elements::Advertisement
)
config.register_element_exporters(
:html,
image: ArticleJSON::Export::HTML::Elements::ScaledImage
)
config.register_element_exporters(
:html,
advertisement: ArticleJSON::Export::HTML::Elements::Advertisement,
image: ArticleJSON::Export::HTML::Elements::ScaledImage
)
config.register_element_exporters(
:amp,
image: ArticleJSON::Export::AMP::Elements::ScaledImage
)
end
Facebook Oembed
Facebook deprecated its public endpoints for embeddable Facebook content in 2020 (See https://developers.facebook.com/docs/plugins/oembed-legacy for more info).
You now need to use a Facebook token to access the new oembed endpoints. You can configure the gem to use this token so:
ArticleJSON.configure do |config|
config.facebook_token = 'token'
end
Find more info about the access token here.
Format
A full example of the format can be found in the test fixtures.
Import
Google Document Parser
This reference document contains all the supported formatting along with some descriptions.
Add custom elements
Sometimes you might want to place additional elements into the article, like e.g. advertisements. article_json
supports this via article.place_additional_elements
, which accepts an array of elements that you can define in your code.
Each element that is added this way will directly get placed in between paragraphs of the article. The method ensures that an additional element is never added before or after any node other than paragraphs (e.g. an image). The elements are added in the order you pass them into the method.
If the article does not have enough space to place all the provided elements, they will be placed after the last
element in the article.
You can pass any type of element into this method.
If the objects you pass in are instances of elements defined within this gem (e.g. ArticleJSON::Elements::Image
), you won't have to do anything else to render them.
If you pass in an instance of a custom class (e.g. MyAdvertisement
), make sure to register an exporter for this type (check the Configuration section for more details).
Example using only existing elements:
article = ArticleJSON::Article.from_hash(parsed_json)
image_advertisement =
ArticleJSON::Elements::Image.new(source_url: 'https://robohash.org/great-ad',
caption: ArticleJSON::Elements::Text.new(
content: 'Buy more robots!',
href: '/robot-sale'
))
text_box_similar_articles =
ArticleJSON::Elements::TextBox.new(content: [
ArticleJSON::Elements::Heading.new(level: 3, content: 'Read more...'),
ArticleJSON::Elements::List.new(content: [
ArticleJSON::Elements::Paragraph(content: [
ArticleJSON::Elements::Text.new(content: 'Very similar article',
href: '/news/123'),
]),
ArticleJSON::Elements::Paragraph(content: [
ArticleJSON::Elements::Text.new(content: 'Great article!',
href: '/news/42'),
]),
]),
])
article.place_additional_elements([image_advertisement,
text_box_similar_articles])
article.to_html
Example with custom advertisement elements:
class MyAdvertisement
attr_reader :url
def initialize(url:)
@url = url
end
def type
:my_advertisement
end
end
class MyAdvertisementExporter <
ArticleJSON::Export::HTML::Elements::Base
def export
create_element(:iframe, src: @element.url)
end
end
config.register_element_exporters(
:html,
my_advertisement: MyAdvertisementExporter
)
ad_1 = MyAdvertisement.new(url: '/my_first_ad')
ad_2 = MyAdvertisement.new(url: '/my_second_ad')
ad_3 = MyAdvertisement.new(url: '/my_last_ad')
article.place_additional_elements([ad_1, ad_2, ad_3])
article.to_html
Export
HTML
The HTML exporter generates an HTML string for a list of elements. An example of the HTML export for the parsed reference document can be found here.
AMP
The AMP exporter generates an AMP HTML representation of the elements.
AMP uses custom HTML tags, some of which require additional Javascript libraries.
If you have an article
(see code example in Usage section), you can get a list of the custom tags required by this article by calling article.amp_exporter.custom_element_tags
and calling article.amp_exporter.amp_libraries
gives a list of <script>
tags that can directly be included on your page to render the AMP article.
An example of the AMP HTML export for the parsed reference document can be found here.
Facebook Instant Articles
The FacebookInstantArticle
exporter generates a custom HTML string for a list of elements. An example of the Facebook Instant Article export for the parsed reference document can be found here.
To learn more about the Facebook Instant Article HTML format see have a look at the Facebook Developer Documentation.
Plain Text
As the name suggests, this exporter generates a plain text version of the article. Rich text elements like images, embeds or even text boxes are not being rendered.
The reference document rendered as plain text can be found here.
Usage:
article = ArticleJSON::Article.from_hash(parsed_json)
article.to_plain_text
Contributing
- Fork this repository
- Implement your feature or fix including tests
- Update the change log
- Commit your changes with a meaningful commit message
- Create a pull request
Thank you!
See the
list of contributors.
Tests
For the whole test suite, run bundle exec rspec
.
For individual tests, run bundle exec rspec spec/article_json/version_spec.rb
.
License
MIT License, see the license file.