Class: CG::Convert
Class Method Summary collapse
Instance Method Summary collapse
- #article_rendering(markdown) ⇒ Object
- #comment_rendering ⇒ Object
- #gen_output_path(source) ⇒ Object
-
#initialize(source) ⇒ Convert
constructor
A new instance of Convert.
- #load_markdown(source) ⇒ Object
- #load_template(template_name = 'html.erb') ⇒ Object
- #page_build(page) ⇒ Object
- #start ⇒ Object
- #templates_dir ⇒ Object
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.(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_rendering ⇒ Object
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) = source.gsub(/-|\+/, '/') dir_path = File.("#{@root}/#{File.dirname(expand_path).gsub(markdown_base, '')}") html_name = File.basename().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 |
#start ⇒ Object
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_dir ⇒ Object
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.('../../skel/templates', File.dirname(__FILE__)) end end |