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
18
19
# 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'
  @quiet = options[:quiet] == true
  @exclude_from_navigation = options[:exclude_from_navigation] || []
end

Class Method Details



103
104
105
106
107
108
# File 'lib/double_doc/html_generator.rb', line 103

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

Instance Method Details

#copy_assetsObject



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

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

#generateObject



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

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}" unless @quiet
    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



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

def sitemap
  return @sitemap unless @sitemap.nil?

  @sitemap = []

  @sources.each do |src|
    path = File.basename(src).sub(/\.md$/, '.html')

    next if @exclude_from_navigation.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

#templateObject



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

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