Class: Bookshelf::Parser::HTML

Inherits:
Base
  • Object
show all
Defined in:
lib/bookshelf/parser/html.rb

Constant Summary collapse

IGNORE_DIR =

List of directories that should be skipped.

%w[. .. .svn]
IGNORE_FILES =

Files that should be skipped.

/^(TOC)\..*?$/
EXTENSIONS =

List of recognized extensions.

%w[md mkdn markdown html]

Instance Attribute Summary

Attributes inherited from Base

#book_dir

Instance Method Summary collapse

Methods inherited from Base

#config, #initialize, #name, parse, #spawn_command

Constructor Details

This class inherits a constructor from Bookshelf::Parser::Base

Instance Method Details

#contentObject

Return all chapters wrapped in a div.chapter tag.



34
35
36
37
38
39
40
# File 'lib/bookshelf/parser/html.rb', line 34

def content
  @content ||= String.new.tap do |chapters|
    entries.each do |entry|
      chapters << %[<div class="chapter">#{render_file(entry)}</div>]
    end
  end
end

#parseObject

Parse all files and save the parsed content to output/book_name.html.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bookshelf/parser/html.rb', line 19

def parse
  File.open(Bookshelf.root_dir.join("output/#{name}.html"), "w") do |file|
    locals = config.merge({
      :content => content,
      :toc => toc
    })
    file << Bookshelf.render_template(Bookshelf.root_dir.join("templates/html/layout.erb"), locals)
  end
  true
rescue Exception
  false
end

#tocObject



42
43
44
45
46
47
48
49
50
# File 'lib/bookshelf/parser/html.rb', line 42

def toc
  if @toc.blank?
    @toc = ""
    Nokogiri::HTML(content).css(".chapter h2:first-of-type").each do |xml|
      @toc << %[<div><a href="##{xml.attribute("id")}"><span>#{CGI.escape_html(xml.text)}</span></a></div>]
    end
  end
  return @toc
end