Skip to content

Instantly share code, notes, and snippets.

@benjaminkreen
Last active June 27, 2019 16:35
Show Gist options
  • Save benjaminkreen/4c7ca141416d0d0338cc19482662e5ee to your computer and use it in GitHub Desktop.
Save benjaminkreen/4c7ca141416d0d0338cc19482662e5ee to your computer and use it in GitHub Desktop.
A script to convert plos landing page json
require 'json'
require 'csv'
require 'pry'
class Transmogrifier
# this class takes a bunch of plos landing page json and
# renders bootstrap 4 html into a CSV that can be imported
# to wordpress. Makes ya wonder.
def initialize(page_object)
@page_object = page_object
@host = 'https://www.plos.org'
end
def sorted_rows
@page_object['rows'].sort_by { |row| row['rank'] }
end
def convert
{
post_title: @page_object['landing_page']['title'],
post_content: render_content,
post_date: @page_object['landing_page'][''],
post_slug: @page_object['landing_page']['slug'],
post_author: @page_object['landing_page']['last_published_by']
}
end
def render_content
"<div class='container'>#{render_menu_row}#{rendered_rows.join}</div>"
end
def rendered_rows
sorted_rows.map(&method(:render_row))
end
def render_row(row)
case row['row_category_type']
when 'TextRow'
render_text_row(row)
when 'FeatureRow'
render_feature_row(row)
when 'JournalRow'
render_journals_row
when 'PhotoRow'
render_photo_row(row)
else
wrap_in_row("#{row['row_category_type']} not supported yet")
end
end
# row rendering methods
def render_photo_row(row)
photo_row = get_item('photo_rows', row['row_category_id'])
photo_row_image = get_item('photo_row_images', photo_row['photo_row_image_id'])
photo_url = @host + photo_row_image['url']
wrap_in_row("<img src='#{photo_url}'>")
end
def render_journals_row
# hard codded, just like prod :P
journal_data = [
%w(pone https://www.plos.org/lemur-commons/assets/images/journals/bg/pone-76ab33f1fbe2ef0199f2acd168bf4fdf.jpg https://www.plos.org/lemur-commons/assets/images/journals/logos/PLOS_one_h.svg http://journals.plos.org/plosone/),
%w(pbio https://www.plos.org/lemur-commons/assets/images/journals/bg/pbio-9105fe077ea9ce978195b6f901664edd.jpg https://www.plos.org/lemur-commons/assets/images/journals/logos/PLOS_bio_h.svg http://journals.plos.org/plosbiology/),
%w(pmed https://www.plos.org/lemur-commons/assets/images/journals/bg/pmed-51302102e9df46f94f916d1c9969404a.jpg https://www.plos.org/lemur-commons/assets/images/journals/logos/PLOS_med_h.svg http://journals.plos.org/plosmedicine/),
%w(pcbi https://www.plos.org/lemur-commons/assets/images/journals/bg/pcbi-2228d5cb1ea31230cbd956753dfd9d7c.jpg https://www.plos.org/lemur-commons/assets/images/journals/logos/PLOS_cb_h.svg http://journals.plos.org/ploscompbiol/),
%w(pgen https://www.plos.org/lemur-commons/assets/images/journals/bg/pgen-6547dc259e11f63165eb3f225a3fe0c6.jpg https://www.plos.org/lemur-commons/assets/images/journals/logos/PLOS_gen_h.svg http://journals.plos.org/plosgenetics/),
%w(pntd https://www.plos.org/lemur-commons/assets/images/journals/bg/pntd-96163a970f759e4f3fe9d590c27ed4a2.jpg https://www.plos.org/lemur-commons/assets/images/journals/logos/PLOS_ntd_h.svg http://journals.plos.org/plosntds/),
%w(ppat https://www.plos.org/lemur-commons/assets/images/journals/bg/ppat-cd05ff3919dc70e856952b4918901eba.jpg https://www.plos.org/lemur-commons/assets/images/journals/logos/PLOS_path_h.svg http://journals.plos.org/plospathogens/)
]
rows = journal_data.map do |j_data|
col_val = j_data[0] == 'pone' ? 12 : 6
#TODO: make sure these extra quotes dont break csv
"<a class='col-#{col_val} text-center' href='#{j_data[3]}' style='height: 60px; background-image:url(\"#{j_data[1]}\")'><img style='height: 100%' src='#{j_data[2]}'></a>"
end
wrap_in_row(rows.join)
end
def render_feature_row(row)
feature_row = get_item('feature_rows', row['row_category_id'])
rendered_blocks = feature_row['feature_block_ids'].map(&method(:render_feature_blocks))
wrap_in_row(rendered_blocks.join)
end
def render_text_row(row)
text_row = get_item('text_rows', row['row_category_id'])
rendered_row = "<div class='col-12 text-center'>#{text_row['text']}</div>" + render_link_button(row['id']).to_s
wrap_in_row(rendered_row, ['justify-content-center'])
end
def render_menu_row
return unless @page_object['landing_page']['menu_id']
menu_items = @page_object['menus'].first['menu_item_ids'].map do |menu_id|
menu_item = get_item('menu_items', menu_id)
<<-HTML
<li class="nav-item">
<a class="nav-link" href="/#{menu_item['slug']}">#{menu_item['title']}</a>
</li>
HTML
end
wrap_in_row("<ul class='nav justify-content-center'>#{menu_items.join}</ul></nav>", ['justify-content-center'])
end
# end of row rending methods
def wrap_in_row(stuff, classes=[])
"<div class='row p-3 #{classes.join(' ')}'>#{stuff.gsub("\n", ' ').squeeze(' ')}</div>"
end
def get_item(item_type, id)
@page_object[item_type].find { |r| r['id'] == id }
end
def visible_button(button_id)
@page_object['row_buttons'].find { |b| b['is_visible'] && b['id'] == button_id }
end
def render_link_button(row_id)
# buttons and rows have the same id?
button_object = @page_object['row_buttons'].find { |b| b['is_visible'] && b['id'] == row_id }
button_object ? "<div class='col-12 text-center'><a href='#{button_object['url']}' class='btn btn-primary'>#{button_object['text']}</a></div>" : nil
end
def render_feature_block_image(img_id)
return unless img_id
feature_block_image = get_item('feature_block_images', img_id)
# hotlinking for now
"<img class='card-img-top mx-auto' src='#{@host}#{feature_block_image['url']}'>"
end
def render_feature_blocks(block_id)
feature_block = @page_object['feature_blocks'].find { |block| block['id'] == block_id }
<<-HTML
<div class='col-sm'>
<div class='card h-100'>
#{render_feature_block_image(feature_block['feature_block_image_id'])}
<div class='card-body'>
#{feature_block['text']}
</div>
</div>
</div>
HTML
end
end
json_data = Dir['./*.json'].map { |f| File.read(f) }
page_objects = json_data.map { |jd| Transmogrifier.new(JSON.parse(jd)).convert }
csv_header = %w[post_title post_content post_date post_author post_slug post_parent post_status Keywords menu_order wp_page_template]
CSV.open('landing_pages_wp_import.csv', 'wb') do |csv|
csv << csv_header
page_objects.each do |po|
puts po[:post_content]
csv << [po[:post_title], po[:post_content], Time.now.to_s.split(' ')[0..1].join(' '), 'transmogrifier', po[:post_slug], nil, 'publish', nil, nil, 'page-templates/full-width.php']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment