require_relative 'rails_base' require 'github/markup' require 'nokogiri' require 'ro_html_handler' module RoCommands class PostCell include Celluloid def handle_single(p) tag_name = File.dirname(p) title, content = if p.match(%r{\.md$}) handle_markdown(p) elsif p.match(%r{(.+)\.html$}) handle_html(p) end return {title: title, content: content} #post.tags.create name: tag_name end private def handle_markdown(p) title = File.basename(p).gsub(%r{\.\w+$}, "") content = GitHub::Markup::Markdown.new.render(RoFile.read(p)) return title, content end def handle_html(p) origin = RoFile.read(p) title = get_title(origin) content = get_smth_in_html(origin, 'inner_html') return title, content end def get_title(html) doc(html) do |d| d.css('title').text end end def get_smth_in_html(html, smth) doc(html) do |d| d.css("body").send(:"#{smth}") end end private include RoHtmlHandler end class PostHandler < RailsBase desc usage("handle"), "" def handle path = File.join(::Rails.root, 'posts') futures = [] ::Find.find(path) do |p| if test(?f, p) and p.match(%r{(.+)\.(html|md)}) futures << PostCell.new.future.handle_single(p) end end futures.map(&:value).each do |record| post_create(record) end end private def post_create(attrs={}) if attrs[:title] Out.table("creating a record", attrs[:title], "file:#{File.basename __FILE__} line:#{__LINE__}") post = Post.find_or_initialize_by(title: attrs[:title]) post.update(attrs) post else raise "create a post must be has a title" end end end end