Class: Bookmaker::Parser::HTML

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

Instance Attribute Summary

Attributes inherited from Base

#root_dir, #source

Instance Method Summary collapse

Methods inherited from Base

#config, #entries, #initialize, #name, parse, #read_content, #render_template, #spawn_command

Constructor Details

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

Instance Method Details

#contentObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bookmaker/parser/html.rb', line 5

def content
  raw = []
  entries.keys.each do |chapter|
    text = "<h2>#{chapter.split(/_/)[1].gsub('-',' ').titleize}</h2>"
    sections = []
    entries[chapter].each do |section|
      sections << "<p>#{read_content(section)[0].split(/\n{2,}/).map do |s|
        s.gsub!(/%.*/, '')
        s.squish
      end.join("</p>\n\n<p>")}</p>"
    end
    text << sections.join("\n\n<hr />\n\n")
    raw << "<div class='chapter'>\n#{text}\n</div>\n"
  end
  raw
end

#parseObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bookmaker/parser/html.rb', line 21

def parse
  puts "-- Exporting HTML"
  html = parse_layout(content)
  toc = TOC::HTML.generate(html)
  locals = config.merge({
            :contents  => toc.content,
            :toc       => toc.to_html,
           })
  output = render_template(root_dir.join("templates/html/layout.erb"), locals)        
  f = File.open(root_dir.join("output/#{name}.html"), 'w')
  f.write(output)
  f.close
  true
rescue Exception
  p $!, $@
  false
end

#parse_layout(chapters) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bookmaker/parser/html.rb', line 38

def parse_layout(chapters)
  output = ''
  chapters.each do |text|
    # text.gsub!("{%", "{")
    # text.gsub!(/%.*/,'')
    text.gsub!(/``(.*?'?)''/) { "&ldquo;#{$1}&rdquo;"}
    text.gsub!(/``/, "&ldquo;")
    text.gsub!(/\b'\b/) { "&#39;" }
    text.gsub!(/`(.*?)'/) { "&lsquo;#{$1}&rsquo;"}
    # \{([^\}]+?)\} Within the curly braces.
    text.gsub!(/\\Dash\{\}/, "&mdash;")
    text.gsub!(/\\begin\{quote\}(.*?)\\end\{quote\}/m) { "<blockquote>#{$1.strip}</blockquote>"}
    text.gsub!(/<\/blockquote>\s+?<blockquote>/m, "\n")
    text.gsub!(/\\begin\{([^\}]+?)\}(.*?)\\end\{[^\}]+?\}/m) { "<div class='#{$1.strip}'>#{$2.strip}</div>"}
    text.gsub!(/\\section\{([^\}]+?)\}/m) { "<h3>#{$1.strip}</h3>"}
    text.gsub!(/\\emph\{([^\}]+?)\}/m) { "<em>#{$1.strip}</em>"}
    text.gsub!(/\\thought\{([^\}]+?)\}/m) { "<em>#{$1.strip}</em>"}
    text.gsub!(/\\(.*?)\{([^\}]+?)\}/) { "<span class='#{$1}'>#{$2.strip}</span>"}
    text.gsub!(/\\(.*?)\{([^\}]+?)\}/) { "<span class='#{$1}'>#{$2.strip}</span>"}
    text.gsub!(/<\/span>\{[^\}]+?}/, "</span>")
    text.gsub!(/<p><h([1-6])>(.*?)<\/h[1-6]><\/p>/) { "<h#{$1}>#{$2.strip}</h#{$1}>"}
    text.gsub!(/(\S+)~(\S+)/) { "#{$1}&nbsp;#{$2}"}
    # text.gsub!(/> +/, ">")
    output << text
  end
  output.gsub!(/\n\n+/, "\n\n")
  return output
end