Class: Asciibook::Builders::PdfBuilder

Inherits:
BaseBuilder show all
Defined in:
lib/asciibook/builders/pdf_builder.rb

Instance Method Summary collapse

Methods inherited from BaseBuilder

#copy_file

Constructor Details

#initialize(book) ⇒ PdfBuilder

Returns a new instance of PdfBuilder.



4
5
6
7
8
9
10
# File 'lib/asciibook/builders/pdf_builder.rb', line 4

def initialize(book)
  super
  @dest_dir = File.join(@book.dest_dir, 'pdf')
  @tmp_dir = File.join(@dest_dir, 'tmp')
  @theme_dir = File.join(@book.theme_dir, 'pdf')
  @theme_config = YAML.safe_load(File.read(File.join(@theme_dir, 'config.yml')))
end

Instance Method Details

#buildObject



12
13
14
15
16
17
18
19
# File 'lib/asciibook/builders/pdf_builder.rb', line 12

def build
  prepare_workdir
  generate_pages
  copy_assets
  generate_header_footer
  generate_pdf
  clean_workdir
end

#clean_workdirObject



26
27
28
# File 'lib/asciibook/builders/pdf_builder.rb', line 26

def clean_workdir
  FileUtils.rm_r @tmp_dir
end

#copy_assetsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/asciibook/builders/pdf_builder.rb', line 42

def copy_assets
  @book.assets.each do |path|
    copy_file(path, @book.base_dir, @tmp_dir)
  end

  Dir.glob('**/*.{jpb,png,gif,svg,css,js,eot,ttf,woff,woff2}', File::FNM_CASEFOLD, base: @theme_share_dir).each do |path|
    copy_file(path, @theme_share_dir, @tmp_dir)
  end

  Dir.glob('**/*.{jpb,png,gif,svg,css,js,eot,ttf,woff,woff2}', File::FNM_CASEFOLD, base: @theme_dir).each do |path|
    copy_file(path, @theme_dir, @tmp_dir)
  end
end


56
57
58
59
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/asciibook/builders/pdf_builder.rb', line 56

def generate_header_footer
  layout = Liquid::Template.parse <<~EOF
    <!DOCTYPE html>
    <html>
      <head>
        <script>
          function subst() {
              var vars = {};
              var query_strings_from_url = document.location.search.substring(1).split('&');
              for (var query_string in query_strings_from_url) {
                  if (query_strings_from_url.hasOwnProperty(query_string)) {
                      var temp_var = query_strings_from_url[query_string].split('=', 2);
                      vars[temp_var[0]] = decodeURI(temp_var[1]);
                  }
              }
              var css_selector_classes = ['page', 'frompage', 'topage', 'webpage', 'section', 'subsection', 'date', 'isodate', 'time', 'title', 'doctitle', 'sitepage', 'sitepages'];
              for (var css_class in css_selector_classes) {
                  if (css_selector_classes.hasOwnProperty(css_class)) {
                      var element = document.getElementsByClassName(css_selector_classes[css_class]);
                      for (var j = 0; j < element.length; ++j) {
                          element[j].textContent = vars[css_selector_classes[css_class]];
                      }
                  }
              }
          }
        </script>
        <style>
          html, body {
            margin: 0;
            padding: 0;
          }
        </style>
      </head>
      <body onload="subst()">
        {{ content }}
      </body>
    </html>
  EOF

  File.open(File.join(@tmp_dir, 'header.html'), 'w') do |file|
    file.write layout.render('content' => File.read(File.join(@theme_dir, 'header.html')))
  end

  File.open(File.join(@tmp_dir, 'footer.html'), 'w') do |file|
    file.write layout.render('content' => File.read(File.join(@theme_dir, 'footer.html')))
  end
end

#generate_pagesObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/asciibook/builders/pdf_builder.rb', line 30

def generate_pages
  layout = Liquid::Template.parse(File.read(File.join(@theme_dir, 'layout.html')))
  @book.pages.each do |page|
    File.open(File.join(@tmp_dir, page.path), 'w') do |file|
      file.write layout.render({
        'book' => @book.to_hash,
        'page' => page.to_hash
      })
    end
  end
end

#generate_pdfObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/asciibook/builders/pdf_builder.rb', line 104

def generate_pdf
  command = ['wkhtmltopdf']
  command << '--header-html' << File.expand_path('header.html', @tmp_dir)
  command << '--footer-html' << File.expand_path('footer.html', @tmp_dir)
  command << '--margin-top' << @theme_config.fetch('margin_top', 10).to_s
  command << '--margin-left' << @theme_config.fetch('margin_left', 10).to_s
  command << '--margin-right' << @theme_config.fetch('margin_right', 10).to_s
  command << '--margin-bottom' << @theme_config.fetch('margin_bottom', 10).to_s
  command << '--header-spacing' << @theme_config.fetch('header_spacing', 0).to_s
  command << '--footer-spacing' << @theme_config.fetch('footer_spacing', 0).to_s

  if @book.cover_image_path
    prepare_cover
    command << 'cover' << 'cover.html'
  end

  @book.pages.each do |page|
    if page.node.is_a?(Asciidoctor::Section) && page.node.sectname == 'toc'
      prepare_toc_xsl(page)
      command << 'toc' << '--xsl-style-sheet' << 'toc.xsl'
    else
      command << page.path
    end
  end
  filename = "#{@book.basename}.pdf"
  command << filename
  command << { chdir: @tmp_dir }
  system(*command)

  FileUtils.cp File.join(@tmp_dir, filename), @dest_dir
end

#prepare_coverObject



136
137
138
139
140
141
142
# File 'lib/asciibook/builders/pdf_builder.rb', line 136

def prepare_cover
  File.open(File.join(@tmp_dir, 'cover.html'), 'w') do |file|
    file.write <<~EOF
      <img src="#{@book.cover_image_path}" />
    EOF
  end
end

#prepare_toc_xsl(page) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/asciibook/builders/pdf_builder.rb', line 144

def prepare_toc_xsl(page)
  File.open(File.join(@tmp_dir, 'toc.xsl'), 'w') do |file|
    file.write Liquid::Template.parse(File.read(File.join(@theme_dir, 'toc.xsl'))).render({
      'book' => @book.to_hash,
      'page' => page.to_hash
    })
  end
end

#prepare_workdirObject



21
22
23
24
# File 'lib/asciibook/builders/pdf_builder.rb', line 21

def prepare_workdir
  FileUtils.mkdir_p @tmp_dir
  FileUtils.rm_r Dir.glob("#{@tmp_dir}/*")
end