Class: DataDoc::Document
- Inherits:
-
Object
- Object
- DataDoc::Document
- 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
-
#data_only ⇒ Object
do not change schema; truncates tables.
-
#format ⇒ Object
MIME-type for output.
-
#output ⇒ Object
output filename.
-
#read_only ⇒ Object
do not change schema or data.
-
#verbose ⇒ Object
display verbose output during processing.
Class Method Summary collapse
-
.default_layout ⇒ Object
Default layout if no layout file provided.
Instance Method Summary collapse
-
#connection=(connection) ⇒ Object
Sets the database connection that the stores will be using.
-
#generate(content_io) ⇒ Object
Generate the output for the given input content.
-
#initialize ⇒ Document
constructor
Sets up so that default options can be read.
-
#layout=(filename) ⇒ Object
Sets the layout file option.
-
#link(attrs = {}) ⇒ Object
Adds a link tag to the headers.
-
#meta(attrs = {}) ⇒ Object
Adds a meta tag to the headers.
-
#script(attrs = {}) ⇒ Object
Adds a script tag to the headers, yielding for body of tag.
-
#set_layout_file(filename) ⇒ Object
Set layout file (from the filename, not the template content itself).
-
#title(text) ⇒ Object
Sets the title tag and emits the title in a classed div.title.
Constructor Details
#initialize ⇒ Document
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_only ⇒ Object
do not change schema; truncates tables
50 51 52 |
# File 'lib/data_doc/document.rb', line 50 def data_only @data_only end |
#format ⇒ Object
MIME-type for output
35 36 37 |
# File 'lib/data_doc/document.rb', line 35 def format @format end |
#output ⇒ Object
output filename
41 42 43 |
# File 'lib/data_doc/document.rb', line 41 def output @output end |
#read_only ⇒ Object
do not change schema or data
47 48 49 |
# File 'lib/data_doc/document.rb', line 47 def read_only @read_only end |
#verbose ⇒ Object
display verbose output during processing
44 45 46 |
# File 'lib/data_doc/document.rb', line 44 def verbose @verbose end |
Class Method Details
.default_layout ⇒ Object
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 |
#link(attrs = {}) ⇒ Object
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 (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 |