2011-07-19 19:14:32 +02:00
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
def ask message
|
|
|
|
puts message
|
|
|
|
STDIN.gets.chomp
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_jekyll files, template
|
2011-07-25 15:08:16 +02:00
|
|
|
# all tags used in the posts
|
|
|
|
template_tags = {}
|
2011-08-09 14:59:55 +02:00
|
|
|
post_tags = {}
|
|
|
|
puts "If asked about template tags, please give a replacement."
|
2011-07-19 19:14:32 +02:00
|
|
|
# from jekyll - lib/jekyll/convertible.rb#26 (08.2011)
|
|
|
|
files.each do |path|
|
|
|
|
content = File.read(path)
|
|
|
|
data = {}
|
|
|
|
if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
|
|
|
|
content = $POSTMATCH
|
|
|
|
|
|
|
|
begin
|
|
|
|
data = YAML.load($1)
|
|
|
|
rescue => e
|
|
|
|
puts "YAML exception reading #{path}: #{e.message}"
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
2011-07-25 15:08:16 +02:00
|
|
|
content = content.gsub /({{.*?}})/ do |m|
|
|
|
|
if template_tags.has_key? $1
|
|
|
|
template_tags[$1]
|
|
|
|
else
|
|
|
|
template_tags[$1] = ask("Found tag \"#{$1}\" in post #{data['title']}:")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-19 19:14:32 +02:00
|
|
|
post = Post.new(
|
|
|
|
:written => data['date'],
|
|
|
|
:released => true,
|
|
|
|
:account => Account.first(:username => data['author']),
|
|
|
|
:markup => template,
|
|
|
|
:content => content,
|
|
|
|
:title => data['title']
|
|
|
|
)
|
2011-08-09 14:59:55 +02:00
|
|
|
if post.save
|
|
|
|
data['tags'].each do |t|
|
|
|
|
if (post_tags.has_key? t)
|
|
|
|
post_tags[t] << post
|
|
|
|
else
|
|
|
|
post_tags[t] = [post]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2011-07-25 15:08:16 +02:00
|
|
|
puts "post #{post.title} of file #{path} could not be saved!"
|
|
|
|
end
|
2011-07-19 19:14:32 +02:00
|
|
|
else
|
|
|
|
puts "No valid jekyll file #{path}"
|
|
|
|
raise ArgumentError, "#{path} is not a jekyll file"
|
|
|
|
end
|
|
|
|
end
|
2011-08-09 14:59:55 +02:00
|
|
|
if post_tags.count > 0
|
|
|
|
post_tags.each do |tag_name, posts|
|
|
|
|
tag = Tag.new(:name => tag_name)
|
|
|
|
tag.posts = posts
|
|
|
|
tag.save
|
|
|
|
end
|
|
|
|
end
|
2011-07-19 19:14:32 +02:00
|
|
|
end
|