Class: DataDoc::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/data_doc/document.rb

Overview

Class for processing and rendering data_docs.

Manages processing and formatting of the document.

Defined Under Namespace

Classes: IsolatedLayoutContext

Constant Summary collapse

OUTPUT_TYPES =

Available mime types that can be generated.

['html']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocument

Sets up so that default options can be read.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/data_doc/document.rb', line 22

def initialize
  @format = 'html'
  @output = STDOUT
  @verbose = false
  @read_only = false
  @data_only = false
  @connection = nil
  @layout_filename = nil
  
  @headers = Array.new
end

Instance Attribute Details

#data_onlyObject

do not change schema; truncates tables



50
51
52
# File 'lib/data_doc/document.rb', line 50

def data_only
  @data_only
end

#formatObject

MIME-type for output



35
36
37
# File 'lib/data_doc/document.rb', line 35

def format
  @format
end

#outputObject

output filename



41
42
43
# File 'lib/data_doc/document.rb', line 41

def output
  @output
end

#read_onlyObject

do not change schema or data



47
48
49
# File 'lib/data_doc/document.rb', line 47

def read_only
  @read_only
end

#verboseObject

display verbose output during processing



44
45
46
# File 'lib/data_doc/document.rb', line 44

def verbose
  @verbose
end

Class Method Details

.default_layoutObject

Default layout if no layout file provided.

Simplistic valid html which accepts headers and puts content directly into body tag.



110
111
112
# File 'lib/data_doc/document.rb', line 110

def self.default_layout
  "<!DOCTYPE html>\n<html>\n<head><%= yield :head %></head>\n<body>\n<%= yield %>\n</body>\n</html>"
end

Instance Method Details

#connection=(connection) ⇒ Object

Sets the database connection that the stores will be using



55
56
# File 'lib/data_doc/document.rb', line 55

def connection=(connection)
end

#generate(content_io) ⇒ Object

Generate the output for the given input content.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/data_doc/document.rb', line 74

def generate(content_io)
  erb_content = content_io.read
  begin
    self.untrust
    mark_down = ERB.new(erb_content, 4).result(binding.taint) # $SAFE = 4
  ensure
    self.trust
  end
  content_html = RDiscount.new(mark_down).to_html
  html = wrap_in_layout(content_html)
  @output.write(html)
  0
end

#layout=(filename) ⇒ Object

Sets the layout file option.

See #set_layout_file below for format.



63
64
65
# File 'lib/data_doc/document.rb', line 63

def layout=(filename)
  @layout_filename = filename
end

Adds a link tag to the headers



143
144
145
# File 'lib/data_doc/document.rb', line 143

def link(attrs = {})
  add_header "<link #{html_attrs(attrs)}>"
end

#meta(attrs = {}) ⇒ Object

Adds a meta tag to the headers



129
130
131
# File 'lib/data_doc/document.rb', line 129

def meta(attrs = {})
  add_header "<meta #{html_attrs(attrs)}>"    
end

#script(attrs = {}) ⇒ Object

Adds a script tag to the headers, yielding for body of tag.



136
137
138
# File 'lib/data_doc/document.rb', line 136

def script(attrs = {})
  add_header "<script #{html_attrs(attrs)}>#{"\n"+yield+"\n" if block_given?}</script>"    
end

#set_layout_file(filename) ⇒ Object

Set layout file (from the filename, not the template content itself)

Content is html, with ERB placeholders to yield to add content at appropriate points.

<%= yield :head %>

marks where to place any headers defined (see #meta below for an example)

<%= yield :content %> or <%= yield %>

marks where to place the processed content from the input file.



101
102
103
# File 'lib/data_doc/document.rb', line 101

def set_layout_file(filename)
  self.layout=(filename)
end

#title(text) ⇒ Object

Sets the title tag and emits the title in a classed div.title.



121
122
123
124
# File 'lib/data_doc/document.rb', line 121

def title(text)
  add_header "<title>#{text}</title>"
  "<div class=\"title\">#{text}</div>"
end