Class: DoubleDoc::HtmlGenerator
- Inherits:
-
Object
- Object
- DoubleDoc::HtmlGenerator
- Defined in:
- lib/double_doc/html_generator.rb
Defined Under Namespace
Classes: SitemapItem
Constant Summary collapse
- DEFAULT_CSS =
File.("../../templates/screen.css", File.dirname(__FILE__)).freeze
Class Method Summary collapse
Instance Method Summary collapse
- #copy_assets ⇒ Object
- #generate ⇒ Object
-
#initialize(sources, options) ⇒ HtmlGenerator
constructor
A new instance of HtmlGenerator.
- #sitemap ⇒ Object
- #template ⇒ Object
Constructor Details
#initialize(sources, options) ⇒ HtmlGenerator
10 11 12 13 14 15 16 17 18 |
# File 'lib/double_doc/html_generator.rb', line 10 def initialize(sources, ) @sources = sources @template_file = [:html_template] || File.("../../templates/default.html.erb", File.dirname(__FILE__)) @output_directory = Pathname.new([:html_destination]) @html_renderer = [:html_renderer] || HtmlRenderer @stylesheet = [:html_css] || DEFAULT_CSS @title = [:title] || 'Documentation' = [:exclude_from_navigation] || [] end |
Class Method Details
.convert_links_to_html!(markdown) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/double_doc/html_generator.rb', line 102 def self.convert_links_to_html!(markdown) markdown.gsub!(/(\[[^\]]+\]\([^\)]+)\.md([^\)]*)\)/) do |match| $1 + '.html' + $2 + ')' end markdown end |
Instance Method Details
#copy_assets ⇒ Object
92 93 94 95 96 |
# File 'lib/double_doc/html_generator.rb', line 92 def copy_assets if @stylesheet FileUtils.cp(@stylesheet, @output_directory) end end |
#generate ⇒ Object
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 |
# File 'lib/double_doc/html_generator.rb', line 20 def generate FileUtils.mkdir_p(@output_directory) copy_assets generated_files = [@output_directory + File.basename(@stylesheet)] @sources.each do |src| from_markdown = src.to_s =~ /\.md$/ if from_markdown path = File.basename(src).sub(/\.md$/, '.html') else path = File.basename(src) end dst = @output_directory + path puts "#{src} -> #{dst}" FileUtils.mkdir_p(File.dirname(dst)) if from_markdown markdown = self.class.convert_links_to_html!(File.new(src).read) body = @html_renderer.render(markdown) else body = File.new(src).read end File.open(dst, 'w') do |out| html = template.result( :title => @title, :body => body, :css => File.basename(@stylesheet), :sitemap => sitemap, :path => path ) out.write(html) end generated_files << dst end end |
#sitemap ⇒ Object
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 |
# File 'lib/double_doc/html_generator.rb', line 61 def sitemap return @sitemap unless @sitemap.nil? @sitemap = [] @sources.each do |src| path = File.basename(src).sub(/\.md$/, '.html') next if .include?(path) lines = File.readlines(src) item = nil lines.each do |line| if line =~ /^\#\#\s/ title = line.sub(/^\#+\s*/, '').strip @sitemap << item if item item = SitemapItem.new(title, path) elsif line =~ /^\#\#\#\s/ item ||= SitemapItem.new(path, path) title = line.sub(/^\#+\s*/, '').strip item.add_child(SitemapItem.new(title, path, DoubleDoc::HtmlRenderer.generate_id(title))) end end @sitemap << item if item end @sitemap end |
#template ⇒ Object
98 99 100 |
# File 'lib/double_doc/html_generator.rb', line 98 def template @template ||= Erubis::Eruby.new(File.read(@template_file)) end |