Class: TaskJuggler::HTMLDocument

Inherits:
XMLDocument show all
Includes:
HTMLElements
Defined in:
lib/taskjuggler/HTMLDocument.rb

Overview

HTMLDocument objects are a specialized form of XMLDocument objects. All mandatory elements of a proper HTML document will be added automatically upon object creation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from XMLDocument

#<<, #to_s, #write

Constructor Details

#initialize(docType = :html5, &block) ⇒ HTMLDocument

When creating a HTMLDocument the caller can specify the type of HTML that will be used. The constructor then generates the proper XML declaration for it. :strict, :transitional and :frameset are supported for docType.



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
# File 'lib/taskjuggler/HTMLDocument.rb', line 31

def initialize(docType = :html5, &block)
  super(&block)

  unless docType == :html5
    @elements << XMLBlob.new('<?xml version="1.0" encoding="UTF-8"?>')
    case docType
    when :strict
      dtdRef = 'Strict'
      url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
    when :transitional
      dtdRef = 'Transitional'
      url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
    when :frameset
      dtdRef = 'Frameset'
      url = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd'
    else
      raise "Unsupported docType"
    end
    @elements << XMLBlob.new(
      '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ' +
      "#{dtdRef}//EN\" \"#{url}\">")
  else
    @elements << XMLBlob.new('<!DOCTYPE html>')
  end
  @elements << XMLComment.new('This file has been generated by ' +
                              "#{AppConfig.appName} v#{AppConfig.version}")
  attrs = { 'xml:lang' => 'en', 'lang' => 'en' }
  attrs['xmlns'] ='http://www.w3.org/1999/xhtml' unless docType == :html5
  @elements << (@html = HTML.new(attrs))
end

Instance Attribute Details

#htmlObject (readonly)

Returns the value of attribute html.



26
27
28
# File 'lib/taskjuggler/HTMLDocument.rb', line 26

def html
  @html
end

Instance Method Details

#generateHead(title, metaTags = {}, blob = nil) ⇒ Object

Generate the ‘head’ section of an HTML page.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/taskjuggler/HTMLDocument.rb', line 63

def generateHead(title, metaTags = {}, blob = nil)
  @html << HEAD.new {
    e = [
      TITLE.new { title },
      META.new({ 'http-equiv' => 'Content-Type',
                 'content' => 'text/html; charset=utf-8' }),
      # Ugly hack to force IE into IE-9 mode.
      META.new({ 'http-equiv' => 'X-UA-Compatible', 'content' => 'IE=9' })
    ]
    # Include optional meta tags.
    metaTags.each do |name, content|
      e << META.new({ 'name' => name, 'content' => content })
    end
    # Add a raw HTML blob into the header if provided.
    e << XMLBlob.new(blob) if blob

    e
  }
end