Class: CG::Convert

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/cg/convert.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Convert

Returns a new instance of Convert.



16
17
18
19
20
21
22
23
# File 'lib/cg/convert.rb', line 16

def initialize(source)
  @source = source || ARGV.first

  @root = File.expand_path(File.dirname(@source)).gsub('markdown', '')
  @domain = @root.split('/').last

  @templates_dir = templates_dir
end

Class Method Details

.start!(source = nil) ⇒ Object



25
26
27
# File 'lib/cg/convert.rb', line 25

def self.start!(source = nil)
  new(source).start
end

Instance Method Details

#article_rendering(markdown) ⇒ Object



69
70
71
72
73
74
# File 'lib/cg/convert.rb', line 69

def article_rendering(markdown)
  @disqus = comment_rendering

  article = Tilt::ErubisTemplate.new { markdown.render }
  article.render(self)
end

#comment_renderingObject



76
77
78
79
80
81
82
83
# File 'lib/cg/convert.rb', line 76

def comment_rendering
  return unless File.exist? "#{@templates_dir}/disqus.erb"

  @disqus_subdomain = @domain.gsub('.', '-')

  comment = Tilt::ErubisTemplate.new { File.read("#{@templates_dir}/disqus.erb") }
  comment.render(self)
end

#gen_output_path(source) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/cg/convert.rb', line 43

def gen_output_path(source)
  markdown_base = File.dirname(source)
  expand_path = source.gsub(/-|\+/, '/')

  dir_path = File.expand_path("#{@root}/#{File.dirname(expand_path).gsub(markdown_base, '')}")
  html_name = File.basename(expand_path).gsub('.mkd', '.html')

  [dir_path, html_name]
end

#load_markdown(source) ⇒ Object



57
58
59
# File 'lib/cg/convert.rb', line 57

def load_markdown(source)
  Tilt::RDiscountTemplate.new { File.read(source) }
end

#load_template(template_name = 'html.erb') ⇒ Object



53
54
55
# File 'lib/cg/convert.rb', line 53

def load_template(template_name = 'html.erb')
  Tilt::ErubisTemplate.new { File.read("#{@templates_dir}/#{template_name}") }
end

#page_build(page) ⇒ Object



85
86
87
88
89
# File 'lib/cg/convert.rb', line 85

def page_build(page)
  @title = [(Nokogiri::HTML(@article)/'h1').text, @domain].join(' - ')

  page.render(self)
end

#startObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cg/convert.rb', line 29

def start
  dir_path, html_name = gen_output_path(@source)
  mkdir_p dir_path

  html_path = File.join(dir_path, html_name)
  open(html_path, 'w') do |f|
    @article = article_rendering(load_markdown(@source))
    @relative = (Pathname.new(@root).relative_path_from(Pathname.new(dir_path))).to_s + '/'

    f.write page_build(load_template)
    puts "#{@source} => #{html_path}"
  end
end

#templates_dirObject



61
62
63
64
65
66
67
# File 'lib/cg/convert.rb', line 61

def templates_dir
  if File.directory? "#{@root}/templates"
    "#{@root}/templates"
  else
    File.expand_path('../../skel/templates', File.dirname(__FILE__))
  end
end