Class: VivlioPack::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio_pack/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(workdir) ⇒ Generator

Returns a new instance of Generator.



9
10
11
12
13
# File 'lib/vivlio_pack/generator.rb', line 9

def initialize(workdir)
  @workdir = File.expand_path(workdir)
  @names = Psych.load_file("articles.yaml")
  @markdown = Redcarpet::Markdown.new(Renderer, autolink: true, tables: true, fenced_code_blocks: true, footnotes: true)
end

Instance Method Details

#generate(html_path) ⇒ Object



15
16
17
18
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
# File 'lib/vivlio_pack/generator.rb', line 15

def generate(html_path)
  file_paths = @names.map{|name| "#{@workdir}/articles/#{name}.md"}
  File.open(html_path, "w") do |html|
    html.write <<~HTML
      <!DOCTYPE html>
      <html>
      <head>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="contents/css/style.css">
        <link rel="stylesheet" type="text/css" href="contents/css/bw.css">
        <link href="https://fonts.googleapis.com/css?family=M+PLUS+Rounded+1c" rel="stylesheet">
      </head>
      <body>
    HTML

    html.write <<~HTML
        <div class="toc">
          <h1>目次</h1>
          #{TocRenderer.render(file_paths)}
        </div>
        <div class="page-break"></div>
    HTML

    file_paths.each do |path|
      File.open(path, "r") do |file|
        html.write(@markdown.render(file.read))
      end
    end

    html.write <<~HTML
      </body>
      </html>
    HTML
  end
end