4 # http://snarfed.org/pyblosxom2wxr
5 # Ryan Barrett <pyblosxom2wxr@ryanb.org>
6 # Version 0.2. This script is public domain.
8 # This script converts PyBlosxom posts and comments into a WXR (WordPress
9 # eXtensible RSS) XML file that can be imported into a WordPress blog.
13 # $ ./pyblosxom2wxr.sh post1.txt post2.txt > posts.xml
15 # pyblosxom2wxr has been tested with PyBlosxom 1.4 and WordPress 2.9 and 3.0. It
16 # should work with other versions too, but your mileage may vary.
18 # TODO: comment ordering
24 if [[ $# = "0" || $1 = "--help" ]]; then
25 echo 'Usage: pyblosxom2wxr.sh FILES...'
33 # comment id sequence number
37 now=`date --rfc-3339=seconds`
39 <?xml version="1.0" encoding="UTF-8"?>
41 <!-- generator="pyblosxom2wxr/1.0" created="${now}" -->
43 xmlns:excerpt="http://wordpress.org/export/1.0/excerpt/"
44 xmlns:content="http://purl.org/rss/1.0/modules/content/"
45 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
46 xmlns:dc="http://purl.org/dc/elements/1.1/"
47 xmlns:wp="http://wordpress.org/export/1.0/">
52 <description></description>
54 <generator>http://snarfed.org/pyblosxom2wxr?v=1.0</generator>
55 <language>en</language>
56 <wp:wxr_version>1.0</wp:wxr_version>
57 <wp:base_site_url></wp:base_site_url>
58 <wp:base_blog_url></wp:base_blog_url>
65 fullname=`basename "$file" .txt`
67 commentdir="$(dirname "$file")/comments"
68 title=`head -n 1 "$file"`
73 pubDate=$(date -uR -r $file)
74 date=$(date -r "$file" +'%F %T')
75 dateGmt=$(date -u -r "$file" +'%F %T')
77 if grep -q ']]>' "$file"; then
78 echo "WARNING: $file contains the string ]]>, which makes its CDATA " \
79 "section invalid. WordPress handles this ok, but still, heads up." 1>&2
84 <title>${title}</title>
85 <pubDate>${pubDate}</pubDate>
86 <guid isPermaLink="true">/${fullname}</guid>
87 <description></description>
88 <content:encoded><![CDATA[$(tail -n +4 "$file" | markdown_py)]]></content:encoded>
89 <wp:post_date>${date}</wp:post_date>
90 <wp:post_date_gmt>${dateGmt}</wp:post_date_gmt>
91 <wp:comment_status>open</wp:comment_status>
92 <wp:ping_status>open</wp:ping_status>
93 <wp:post_name>${name}</wp:post_name>
94 <wp:status>publish</wp:status>
95 <wp:post_parent>0</wp:post_parent>
96 <wp:menu_order>0</wp:menu_order>
97 <wp:post_type>${type}</wp:post_type>
98 <wp:post_password></wp:post_password>
99 <wp:is_sticky>0</wp:is_sticky>
100 <dc:creator>${creator}</dc:creator>
104 raw_tags=$(grep '#tags' "$file" |perl -pe 's/.tags // '|tr ',' "\n")
105 for tag in $raw_tags; do
106 echo "<category domain=\"post_tag\" nicename=\"${tag}\">${tag}</category>"
107 #echo "<category domain=\"category\" nicename=\"$category\">$category</category>"
109 all_tags="$all_tags$raw_tags"
111 # category="uncategorized"
112 # <category domain="category" nicename="$category">$category</category>
114 # other possible elements:
115 # <link>/${fullname}</link>
116 # <wp:post_id></wp:post_id>
117 # <excerpt:encoded></excerpt:encoded>
120 for cmtfile in ${commentdir}/"$fullname"-{all,[0-9]*}.cmt; do
121 if [[ -e "$cmtfile" ]]; then
122 set +e # because the perl script below uses a non-zero exit code
123 tail -q -n +2 "$cmtfile" | \
125 s/^<item>$/<wp:comment>\n<wp:comment_id>X<\/wp:comment_id>/;
126 s/^<\/item>$/<wp:comment_approved>1<\/wp:comment_approved>\n<\/wp:comment>/;
127 s/<(\/)?author>/<\1wp:comment_author>/g;
128 s/<(\/)?link>/<\1wp:comment_author_url>/g;
129 s/<(\/)?ipaddress>/<\1wp:comment_author_IP>/g;
130 s/<(\/)?description>/<\1wp:comment_content>/g;
131 s/^<(ajax|cmt_date|email|openid_url|parent|post|secretToken|source|title|w3cdate)>.+$//;
134 perl -pe 'use HTML::Entities; decode_entities($_)' | \
135 perl -pe 'use POSIX qw(strftime);
136 s/^<pubDate>(.+)<\/pubDate>$/"<wp:comment_date>" . (strftime "%Y-%m-%d %H:%M:%S", localtime($1)) . "<\/wp:comment_date>"/e;' | \
138 my $id = '${commentid}';
140 s/^(<wp:comment_id>)X(<\/wp:comment_id>)$/$1 . $id++ . $2/e;
143 exit $id - '${commentid}';'
144 # TODO: this is a hack since exit codes are only 8 bits unsigned.
145 # this will break on posts with >255 comments.
158 #for category in $(echo "$all_categories"|sort|uniq); do
160 # <wp:category><wp:term_id>${index}</wp:term_id><wp:category_nicename>${category}</wp:category_nicename><wp:category_parent></wp:category_parent><wp:cat_name><![CDATA[${category}]]></wp:cat_name></wp:category>
162 # index=$(expr $index + 1)
165 for tag in $(echo "$all_tags"|sort|uniq); do
167 <wp:tag><wp:term_id>${index}</wp:term_id><wp:tag_slug>${tag}</wp:tag_slug><wp:tag_name>${tag}</wp:tag_name></wp:tag>
169 index=$(expr $index + 1)