Module: Rock::Doc::HTML

Defined in:
lib/rock/doc.rb

Defined Under Namespace

Classes: PackageRenderingContext, RenderingContext, TaskRenderingContext, TypeRenderingContext, VCSRenderingContext

Constant Summary collapse

TEMPLATE_DIR =
File.expand_path(File.join('templates', 'html'), File.dirname(__FILE__))

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.templatesObject (readonly)

Returns the value of attribute templates



14
15
16
# File 'lib/rock/doc.rb', line 14

def templates
  @templates
end

Class Method Details

.allocate_help_idObject



36
37
38
# File 'lib/rock/doc.rb', line 36

def self.allocate_help_id
    @help_id += 1
end

.escape_html(string) ⇒ Object



18
19
20
21
22
# File 'lib/rock/doc.rb', line 18

def self.escape_html(string)
    string.
        gsub('<', '&lt;').
        gsub('>', '&gt;')
end

.help_tip(doc) ⇒ Object



40
41
42
43
# File 'lib/rock/doc.rb', line 40

def self.help_tip(doc)
    id = allocate_help_id
    "<span class=\"help_trigger\" id=\"#{id}\"><img src=\"{relocatable: /img/help.png}\" /></span><div class=\"help\" id=\"help_#{id}\">#{doc}</div>"
end

.load_template(full_path) ⇒ Object



45
46
47
48
49
50
# File 'lib/rock/doc.rb', line 45

def self.load_template(full_path)
    if template = @templates[full_path]
        return template
    end
    @templates[full_path] = ERB.new(File.read(full_path), nil, nil, full_path.gsub(/[\/\.-]/, '_'))
end

.obscure_email(email) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rock/doc.rb', line 24

def self.obscure_email(email)
    return nil if email.nil? #Don't bother if the parameter is nil.
    lower = ('a'..'z').to_a
    upper = ('A'..'Z').to_a
    email.split('').map { |char|
        output = lower.index(char) + 97 if lower.include?(char)
        output = upper.index(char) + 65 if upper.include?(char)
        output ? "&##{output};" : (char == '@' ? '&#0064;' : char)
    }.join
end

.render_object(object, *template_path) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rock/doc.rb', line 102

def self.render_object(object, *template_path)
    if template_path.last.kind_of?(Hash)
        options = template_path.pop
    else
        options = Hash.new
    end
    options = Kernel.validate_options options,
        :context => nil

    context = options[:context] || rendering_context_for(object)
    if template_path.empty?
        template_path = context.default_template
        if !template_path || template_path.empty?
            raise ArgumentError, "no default fragment defined for #{object}, of class #{object.class}"
        end
    end

    context.render(*template_path)
rescue Exception => e
    raise e, "failed to render object #{object}: #{e.message}", e.backtrace
end

.render_page(body) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rock/doc.rb', line 70

def self.render_page(body)
    html =<<-EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <link rel="stylesheet" href="file://#{File.join(Rock::Doc::HTML.template_path, "style.css")}" type="text/css" />
</head>
<body>
     #{body}
</body>
    EOF
end

.render_template(*path) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/rock/doc.rb', line 60

def self.render_template(*path)
    binding = path.pop
    path = template_path(*path)
		template = load_template(path)
    begin template.result(binding)
    rescue Exception => e
        raise e, "failed to render #{File.join(*path)}: #{e.message}", e.backtrace
    end
end

.rendering_context_for(object) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rock/doc.rb', line 83

def self.rendering_context_for(object)
    case object
    when Autoproj::PackageDefinition
        PackageRenderingContext.new(object)
    when Autoproj::VCSDefinition
        VCSRenderingContext.new(object)
    when Orocos::Spec::TaskContext
        TaskRenderingContext.new(object)
    when Class
        if object <= Typelib::Type
            TypeRenderingContext.new(object)
        else
            RenderingContext.new(object)
        end
    else
        RenderingContext.new(object)
    end
end

.template_path(*relpath) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/rock/doc.rb', line 52

def self.template_path(*relpath)
    if relpath.empty?
        TEMPLATE_DIR
    else
        File.expand_path(File.join(*relpath), TEMPLATE_DIR)
    end
end