Module: MiniTransformer

Defined in:
lib/mini_transformer.rb,
lib/mini_transformer/cli.rb,
lib/mini_transformer/book.rb,
lib/mini_transformer/html.rb,
lib/mini_transformer/entry.rb,
lib/mini_transformer/version.rb,
lib/mini_transformer/key_list.rb

Defined Under Namespace

Classes: Book, CLI, Entry, KeyList, Parser

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.generate_html(resource, mapping = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
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
50
51
52
53
54
55
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
# File 'lib/mini_transformer/html.rb', line 3

def self.generate_html(resource, mapping=nil)
  #puts "generate html"
  builder = Nokogiri::HTML::Builder.new(:encoding => 'utf-8') do |doc|
    doc.html(:lang=> "en") {
      doc.head {
        doc.meta(:id => "book-resource-type", :content => resource.type)
        doc.meta(:id => "identifier", :content => "//apple_ref/doc/uid/#{resource.uid}")
        doc.title(resource.title)
      }

      doc.body() {
        doc.header {
          doc.text "\n"
          resource.introduction.children.each do |para|
            doc.p(para.text)
          end
          doc.text "\n"
        }

        doc.text "\n"

        doc.details {
          doc.text "\n"
          doc.summary(:class => "key-list-name") {doc.text resource.key_list.name}
          doc.text "\n"
        }

        doc.div(:class => "key-list") {
          doc.text "\n"
          doc.span(:class => "key-label") {
            doc.text resource.key_list.key_label
          }
          doc.text "\n"
          doc.span(:class => "description-label") {
            doc.text resource.key_list.description_label
          }
          doc.text "\n"

          if mapping
            doc.send(mapping['entries'], :class => 'entries') {
              resource.key_list.entries.each do |entry|
                if mapping['entry']
                  doc.send(mapping['entry'], :class => 'entry') {
                    doc.send(mapping['key-name'], :class => 'key-name') {
                      doc.text entry.key_name
                    }
                    doc.send(mapping['key-description'], :class => 'key-description') {
                      doc.text entry.description
                    }
                  }
                else
                  doc.send(mapping['key-name'], :class => 'key-name') {
                    doc.text entry.key_name
                  }
                  doc.send(mapping['key-description'], :class => 'key-description') {
                    doc.text entry.description
                  }
                end
              end
            }
          else
            doc.dl(:class => "entries") {
              resource.key_list.entries.each do |entry|
                doc.dt(:class => 'key-name') {
                  doc.text entry.key_name
                }
                doc.dd(:class => 'key-description') {
                  doc.text entry.description
                }
              end
            }
          end
        }
      }
    }
  end

  html = builder.to_html
        
  # Use an XSLT Stylesheet to pretty generate the HTML file. Adjustments follow.
  xsl = Nokogiri::XSLT(File.read(File.join(File.dirname(__FILE__), "format.xsl")))
  html = xsl.apply_to(Nokogiri::HTML html).to_s
  
  # Nokogiri's HTML Builder creates an HTML4 file and cannot be easily changed due to DTD Validation
  html = html.gsub(/<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd">\n/, '<!DOCTYPE html>')
  
  # Realign the meta tag that is added by the XSLT transform with Nokogiri
  html = html.gsub(/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\">\s+$/, '    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">')
  html = html.gsub(/UTF-8/, 'utf-8')
end