Class: DoubleDoc::HtmlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/double_doc/html_generator.rb

Defined Under Namespace

Classes: SitemapItem

Constant Summary collapse

DEFAULT_CSS =
File.expand_path("../../templates/screen.css", File.dirname(__FILE__)).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources, options) ⇒ HtmlGenerator

Returns a new instance of HtmlGenerator.



10
11
12
13
14
15
16
17
# File 'lib/double_doc/html_generator.rb', line 10

def initialize(sources, options)
  @sources = sources
  @template_file = options[:html_template] || File.expand_path("../../templates/default.html.erb", File.dirname(__FILE__))
  @output_directory = Pathname.new(options[:html_destination])
  @html_renderer = options[:html_renderer] || HtmlRenderer
  @stylesheet = options[:html_css] || DEFAULT_CSS
  @title = options[:title] || 'Documentation'
end

Class Method Details



98
99
100
101
102
103
# File 'lib/double_doc/html_generator.rb', line 98

def self.convert_links_to_html!(markdown)
  markdown.gsub!(/(\[[^\]]+\]\([^\)]+)\.md([^\)]*)\)/) do |match|
    $1 + '.html' + $2 + ')'
  end
  markdown
end

Instance Method Details

#copy_assetsObject



88
89
90
91
92
# File 'lib/double_doc/html_generator.rb', line 88

def copy_assets
  if @stylesheet
    FileUtils.cp(@stylesheet, @output_directory)
  end
end

#generateObject



19
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
# File 'lib/double_doc/html_generator.rb', line 19

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

#sitemapObject



60
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
# File 'lib/double_doc/html_generator.rb', line 60

def sitemap
  return @sitemap unless @sitemap.nil?

  @sitemap = []

  @sources.each do |src|
    path = File.basename(src).sub(/\.md$/, '.html')
    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

#templateObject



94
95
96
# File 'lib/double_doc/html_generator.rb', line 94

def template
  @template ||= Erubis::Eruby.new(File.read(@template_file))
end