Created
February 24, 2012 05:02
-
-
Save robstenzinger/1897897 to your computer and use it in GitHub Desktop.
Script to transform my blog's ATOM feed to Wordpress Export format to assist in moving to new CMS.
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
<html> | |
<head> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js"></script> | |
<script> | |
/* | |
Script to transform my blog's ATOM feed to Wordpress Export format to assist in moving to new CMS. | |
REQUIRES Underscore.js and jQuery | |
*/ | |
var dataResult; | |
$(document).ready(function(){ | |
//load my custom ATOM file I exported from my google app engine hosted blog | |
$.get("index7.html", xmlReturn); | |
//parse the xml, format, and export results to the console | |
function xmlReturn(data, textStatus){ | |
//I used dataResult to play around with the data in the js console | |
dataResult = data; | |
var entries = $(dataResult).children("entry"); | |
var core_template = _.template($("#wordpress-export").html()); | |
var item_post_template = _.template($("#item-template-post").html()); | |
var item_template_category = _.template($("#item-template-category").html()); | |
var item_template_tag = _.template($("#item-template-tag").html()); | |
var blog_template_category = _.template($("#blog-template-category").html()); | |
var blog_template_tag = _.template($("#blog-template-tag").html()); | |
var post_id_counter = 0; | |
// prepare all the posts/feed entries | |
for(var i=entries.length-1; i>-1; i--){ | |
post_id_counter = post_id_counter+1; | |
var title = $(entries[i]).find("title").text(), | |
link = $(entries[i]).find("link").attr("href"), | |
pubDate = $(entries[i]).find("updated").text(), | |
creator = "Rob Stenzinger", | |
description = "", | |
content = $(entries[i]).find("content").html(), | |
post_id = post_id_counter, //$(entries[i]).find("id").text(), | |
post_date = pubDate, | |
post_date_gmt = pubDate, | |
post_tags = "", | |
post_categories = "", | |
slug = String($(entries[i]).find("link").attr("href")).split("/").pop(); | |
// get categories and tags | |
// ... categories (in my custom CMS categories were "books") | |
var category_chapter = $(entries[i]).find("chapter").text(); | |
if(category_chapter){ | |
post_categories = post_categories + item_template_category({"name":category_chapter, "namelowertrimmed":category_chapter.toLowerCase().replace(/ /g,"")}); | |
} | |
//tags | |
var tags = $(entries[i]).children("tags").children("tag"); | |
for(var j=0; j<tags.length; j++){ | |
var t = $(tags[j]).text(); | |
if(t){ | |
post_tags = post_tags + item_template_tag({"name":t, "namelowertrimmed":t.toLowerCase().replace(/ /g,"")}); | |
} | |
} | |
$("#export-result-items").append(item_post_template( | |
{ | |
"title":title, | |
"link":link, | |
"pubDate":pubDate, | |
"creator":creator, | |
"description":description, | |
"content":content, | |
"post_id":post_id, | |
"post_date":post_date, | |
"post_date_gmt":post_date_gmt, | |
"slug":slug, | |
"post_tags":post_tags, | |
"post_categories":post_categories, | |
} | |
)); | |
} | |
// prepare the overall wrapper/parent and sibling XML for the feeds | |
var blog_categories = "", | |
blog_tags = "", | |
blog_items = $("#export-result-items").html(), | |
blog_link = "http://www.artgeekzoo.com/", | |
blog_description = "Art Geek Zoo Comics: Featuring The Way of Sound! Comics, Music, and Art Journal of Rob Stenzinger", | |
blog_site_url = "http://www.artgeekzoo.com/", | |
blog_blog_url = "http://www.artgeekzoo.com/", | |
pubDate = "", | |
blog_title = "Art Geek Zoo Comics"; | |
// get blog level categories and tags | |
//categories | |
var blog_category = $(dataResult).children("categories"); | |
for(var j=0; j<blog_category.length; j++){ | |
var t = $(blog_category[j]).find("category").text(); | |
if(t){ | |
blog_categories = blog_categories + blog_template_category({"name":t, "namelowertrimmed":t.toLowerCase().replace(/ /g,"")}); | |
} | |
} | |
//tags | |
var blog_tags_data = $(dataResult).children("tags"); | |
for(var j=0; j<blog_tags_data.length; j++){ | |
var t = $(blog_tags_data[j]).find("tag").text(); | |
if(t){ | |
blog_tags = blog_tags + blog_template_tag({"name":t, "namelowertrimmed":t.toLowerCase().replace(/ /g,"")}); | |
} | |
} | |
// asssemble the final result | |
$("#export-result").append(core_template( | |
{ | |
"blog_categories":blog_categories, | |
"blog_tags":blog_tags, | |
"blog_items":blog_items, | |
"blog_title":blog_title, | |
"blog_link":blog_link, | |
"blog_blog_url":blog_blog_url, | |
"blog_site_url":blog_site_url, | |
"pubDate":pubDate, | |
"blog_description":blog_description, | |
} | |
)); | |
var result = $("#export-result").html(); | |
//manual text cleanup... totally correcting syntax errors in the output | |
result = result.replace(/<!\[CDATA\[<!\[CDATA\[/gi,"<![CDATA["); | |
result = result.replace(/]]>]]>/g,"]]>"); | |
result = result.replace(/<!\[CDATA\[/g,"<![CDATA["); | |
result = result.replace(/]]>/g,"]]>"); | |
result = result.replace(/]]-->]]>/g,"]]-->"); | |
result = result.replace(/<!--\[CDATA\[/g,"<![CDATA["); | |
result = result.replace(/]]-->/g,"]]>"); | |
result = result.replace(/<!\[CDATA\[<!\[CDATA\[/g,"<![CDATA["); | |
result = result.replace(/---->/g,">"); | |
//ready! cut and paste the result into a text file. | |
console.log(result); | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="info">All the magic happens in the JavaScript console.</div> | |
<div id="xmldata" style="visibility:hidden;"> | |
</div> | |
<!-- | |
// all of the underscore.js templates below | |
--> | |
<!-- | |
// template for posts | |
--> | |
<script type="text/template" id="item-template-post"> | |
<item> | |
<title><![CDATA[<%= title %>]]></title> | |
<link><%= link %></link> | |
<pubDate><%= pubDate %></pubDate> | |
<dc:creator><![CDATA[<%= creator %>]]></dc:creator> | |
<%= post_categories %> | |
<%= post_tags %> | |
<guid isPermaLink="false"><%= link %></guid> | |
<description><%= description %></description> | |
<content:encoded><![CDATA[<%= content %>]]></content:encoded> | |
<wp:post_id><%= post_id %></wp:post_id> | |
<wp:post_date><%= post_date %></wp:post_date> | |
<wp:post_date_gmt><%= post_date_gmt %></wp:post_date_gmt> | |
<wp:comment_status>open</wp:comment_status> | |
<wp:ping_status>open</wp:ping_status> | |
<wp:post_name><%= slug %></wp:post_name> | |
<wp:status>publish</wp:status> | |
<wp:post_parent>0</wp:post_parent> | |
<wp:menu_order>0</wp:menu_order> | |
<wp:post_type>post</wp:post_type> | |
<wp:post_password></wp:post_password> | |
</item> | |
</script> | |
<!-- | |
// store the work in progress transformations | |
--> | |
<script type="text/template" id="export-result"> | |
</script> | |
<script type="text/template" id="export-result-items"> | |
</script> | |
<!-- | |
// WHOLE BLOG LEVEL TAGS AND CATEGORIES | |
--> | |
<script type="text/template" id="blog-template-category"> | |
<wp:category><wp:category_nicename><%= namelowertrimmed %></wp:category_nicename> | |
<wp:category_parent></wp:category_parent> | |
<wp:cat_name><![CDATA[<%= name %>]]></wp:cat_name></wp:category> | |
</script> | |
<script type="text/template" id="blog-template-tag"> | |
<wp:tag><wp:tag_slug><%= namelowertrimmed %></wp:tag_slug><wp:tag_name><![CDATA[<%= name %>]]></wp:tag_name></wp:tag> | |
</script> | |
<!-- | |
// POSTLEVEL TAGS AND CATEGORIES | |
--> | |
<script type="text/template" id="item-template-category"> | |
<category><![CDATA[<%= name %>]]></category> | |
<category domain="category" nicename="<%= namelowertrimmed %>"><![CDATA[<%= name %>]]></category> | |
</script> | |
<script type="text/template" id="item-template-tag"> | |
<category domain="tag"><![CDATA[<%= name %>]]></category> | |
<category domain="tag" nicename="<%= namelowertrimmed %>"><![CDATA[<%= name %>]]></category> | |
</script> | |
<script type="text/template" id="item-template-uncategorized"> | |
<category><![CDATA[Uncategorized]]></category> | |
<category domain="category" nicename="uncategorized"><![CDATA[Uncategorized]]></category> | |
</script> | |
<!-- | |
// CORE XML TEMPLATE for WORDPRESS EXPORT FORMAT | |
--> | |
<script type="text/template" id="wordpress-export"> | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. --> | |
<!-- It contains information about your blog's posts, comments, and categories. --> | |
<!-- You may use this file to transfer that content from one site to another. --> | |
<!-- This file is not intended to serve as a complete backup of your blog. --> | |
<!-- To import this information into a WordPress blog follow these steps. --> | |
<!-- 1. Log into that blog as an administrator. --> | |
<!-- 2. Go to Manage: Import in the blog's admin panels. --> | |
<!-- 3. Choose "WordPress" from the list. --> | |
<!-- 4. Upload this file using the form provided on that page. --> | |
<!-- 5. You will first be asked to map the authors in this export file to users --> | |
<!-- on the blog. For each author, you may choose to map to an --> | |
<!-- existing user on the blog or to create a new user --> | |
<!-- 6. WordPress will then import each of the posts, comments, and categories --> | |
<!-- contained in this file into your blog --> | |
<!-- generator="WordPress/MU" created="2008-05-16 22:49"--> | |
<rss version="2.0" | |
xmlns:content="http://purl.org/rss/1.0/modules/content/" | |
xmlns:wfw="http://wellformedweb.org/CommentAPI/" | |
xmlns:dc="http://purl.org/dc/elements/1.1/" | |
xmlns:wp="http://wordpress.org/export/1.0/" | |
> | |
<channel> | |
<title><%= blog_title %></title> | |
<link><%= blog_link %></link> | |
<description><%= blog_description %></description> | |
<pubDate><%= pubDate %></pubDate> | |
<generator>http://wordpress.org/?v=MU</generator> | |
<language>en</language> | |
<wp:wxr_version>1.0</wp:wxr_version> | |
<wp:base_site_url><%= blog_site_url %></wp:base_site_url> | |
<wp:base_blog_url><%= blog_blog_url %></wp:base_blog_url> | |
<%= blog_categories %> | |
<%= blog_tags %> | |
<%= blog_items %> | |
</channel> | |
</rss> | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment