Top Level Namespace
Defined Under Namespace
Modules: BlogGenerator
Instance Method Summary collapse
-
#body(format = @format) ⇒ Object
Maybe rename body -> raw_body and to_html -> body.
- #convert_markdown(markup) ⇒ Object
- #excerpt ⇒ Object
- #file(path, content) ⇒ Object
Instance Method Details
#body(format = @format) ⇒ Object
Maybe rename body -> raw_body and to_html -> body. This is being rewritten from initialize!
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/blog-generator/adapters/markdown.rb', line 22 def body(format = @format) case format when :md @body ||= convert_markdown(self.body(:html)) # I don't think this would work with ||= (which we're using so we can rewrite @body = in initialize.) when :html @body ||= File.read(@path).match(/\n---\n(.+)$/m)[1].strip else raise TypeError.new("Format #{@format} isn't supported.") end end |
#convert_markdown(markup) ⇒ Object
1 2 3 4 5 6 7 |
# File 'lib/blog-generator/adapters/markdown.rb', line 1 def convert_markdown(markup) require 'redcarpet' renderer = Redcarpet::Render::HTML.new(no_links: true, hard_wrap: true) markdown = Redcarpet::Markdown.new(renderer, extensions = {}) markdown.render(markup) end |
#excerpt ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/blog-generator/adapters/markdown.rb', line 9 def excerpt if self.format == :html @excerpt ||= Nokogiri::HTML(Nokogiri::HTML(self.body).css('#excerpt').inner_html.strip).css('p').inner_html elsif self.format == :md # We're converting it to MD, apparently it's necessary even though we # converted the whole text initially, but it seems like MD ignores whatever # is in <div id="excerpt">...</div>. @excerpt ||= Nokogiri::HTML(convert_markdown(Nokogiri::HTML(self.body).css('#excerpt').inner_html.strip)).css('p').inner_html end end |
#file(path, content) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/blog-generator/cli/generate.rb', line 40 def file(path, content) puts "~ #{path}" File.open(path, 'w') do |file| file.puts(content) end end |