all repos — h3rald @ e02ff3e430e9640525c851204d425dbd2f79bcd9

The sources of https://h3rald.com

tasks/db.rake

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
require 'rubygems'
require 'extlib'
require 'pathname'
require 'fileutils'
require 'mysql' rescue nil
require 'sequel' rescue nil
require 'yaml'
require 'iconv'

module TypoUtils

	def get_filter(db, fid)
		filter = db[:text_filters].where("id = ?", fid).get(:name).downcase
		# Multiple filters are not handled (e.g. markdown smartypants)
		filter = filter.split(' ')[0]
		# Prepare metadata
		case filter
		when 'textile' then
			return ['redcloth'], 'textile'
		when 'markdown' then
			return ['bluecloth'], 'markdown'
		when 'bbcode' then
			return ['bbcode'], 'bbcode'
		else
			return [], 'txt'
		end
	end

	def convert_code_blocks(text)
		text.gsub!(/<typo:code lang="([a-zA-Z0-9]+)">/, '<% highlight :\1 do %>')
		text.gsub!(/<typo:code>/, '<% highlight :text do %>')
		text.gsub!(/<\/typo:code>/, "<% end %>")
		text
	end

	def write_page(meta, contents, extension)
		path = (meta['type'] == 'article') ? Pathname.new(Dir.pwd)/"content/articles/" :  Pathname.new(Dir.pwd)/"content/"
		name = "#{meta['permalink']}.#{extension}"
		path.mkpath
		(path/name).open('w+') do |f|
			f.print "--"
			f.puts meta.to_yaml
			f.puts "-----"
			f.puts contents
		end	
	end

end

include TypoUtils

namespace :db do

	task :migrate => [:migrate_contents] do
	end

	task :migrate_contents, :db, :usr, :pwd, :host do |t, args|
		raise RuntimeError, "Please provide :db, :usr, :pass" unless args[:db] && args[:usr] && args[:pwd]
		db = Sequel.mysql args[:db], :user => args[:usr], :password => args[:pwd], :host => args[:host] || 'localhost'
		# Remove all existing pages!
		dir = Pathname.new(Dir.pwd/'content')
		dir.rmtree if dir.exist?
		dir.mkpath
		# Prepare page data
		db[:contents].where("state = 'published' || type = 'Page'").each do |a|
			meta = {}
			# Process tags
			if a[:keywords] then
				if a[:keywords].match ',' then
					meta['tags'] = a[:keywords].downcase.split(", ")
				else
					meta['tags'] = a[:keywords].downcase.split(" ")
				end
			else
				meta['tags'] = []
			end
			meta['permalink'] = a[:permalink] || a[:name]
			meta['title'] = a[:title]
			meta['type'] = a[:type].downcase
			meta['date'] = a[:published_at]
			meta['toc'] = true
			meta['filters_pre'], extension = get_filter db, a[:text_filter_id]
			contents = a[:body]+a[:extended].to_s
			if contents.match /<typo:code/ then
				contents = convert_code_blocks contents
				meta['filters_pre'] = ['erb'].concat meta['filters_pre']
			end
			write_page meta, contents, extension
		end
	end

end