Last active
August 29, 2015 14:07
-
-
Save wilfriedE/62b02e524310c89683dd to your computer and use it in GitHub Desktop.
Converting json based articles to html files for jekyll posts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
#parse the json file containing all the articles | |
posts = JSON.parse File.read("<article>.json") | |
#now for each article we will create a jekyll post | |
posts.each do |post| | |
title = post['title'] #the title of a post | |
content = post['content'] #the content or description of the post | |
image_url = post['image_url'] #the image url if it is available of the post (It is by default an empty string anyways) | |
video_url = post['video_url'] #the video url of a post (same thing as above, preset to an empty string. ) | |
created = post['created_at'] #date of creation to be used when naming jekyll post file | |
#creating the file name | |
out_file = File.new(created + "-" + title.gsub(/[^0-9A-Za-z]/, '-') + ".html" , "w") | |
#creating the contents of the post | |
def body(title, content, image_url, video_url ) | |
#value is content of the file. | |
value = "" | |
value << "---\n" | |
value << "layout: post\n" | |
value << "title: " + title + "\n" | |
value << "---\n" | |
if content != '' | |
value << content | |
end | |
if image_url != '' | |
value << "<img src='" + content +"' class='post_image' />" | |
end | |
if video_url != '' | |
value << video_url | |
end | |
return value | |
end | |
#write the file | |
out_file.puts(body(title, content, image_url, video_url )) | |
out_file.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment