all repos — h3rald @ d88f7a3095b04e45259adfb0d2611bfa61d75b52

The sources of https://h3rald.com

tasks/typo.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
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
require 'rubygems'
require 'extlib'
require 'pathname'
require 'fileutils'
begin
	require 'mysql'
	require 'sequel'
rescue Exception => e
end
require 'yaml'
require 'iconv'

module TypoUtils

	# Ignored by Nanoc 3
	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 get_tags(keywords=nil)
		tags = []
		if keywords then
			if keywords.match ',' then
				tags = keywords.downcase.split(", ")
			else
				tags = keywords.downcase.split(" ")
			end
		end
		tags
	end

	def get_comments(db, aid)
		dataset = db[:feedback].where("article_id = ? && state LIKE '%ham%'", aid)
		comments = []
		dataset.each do |c|
			comment = {}
			comment[:id] = c[:id]
			comment[:author] = c[:author]
			comment[:body] = c[:body].to_s
			comment[:url] = c[:url]
			comment[:date] = c[:published_at]
			comments << comment
		end
		comments
	end	

	def convert_code_blocks(meta, contents)
		if contents.match /<typo:code/ then
			# troubles if erb filter is enabled!
			contents.gsub! /<%/, '&lt;%'
			contents.gsub! /%>/, '%&gt;'
			contents.gsub!(/<typo:code lang="([a-zA-Z0-9]+)">/, '<% highlight :\1 do %>')
			contents.gsub!(/<typo:code>/, '<% highlight :text do %>')
			contents.gsub!(/<\/typo:code>/, "<% end %>")
			meta['filters_pre'] = ['erb'].concat meta['filters_pre']
		end
		contents
	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 :typo do

	task :migrate, :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
		dataset = db[:contents].where("state = 'published' || type = 'Page'")
		total = dataset.count 
		c = 1
		total_tags = []
		dataset.each do |a|
			puts "Migrating [#{c}/#{total}]: '#{a[:title]}'..."
			meta = {}
			meta['tags'] = get_tags a[:keywords]
			meta['comments'] = get_comments db, a[:id]
			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 = convert_code_blocks meta, a[:body]+a[:extended].to_s
			write_page meta, contents, extension
			c = c+1
		end
	end

end