Class: Ronn::Template
- Inherits:
-
Mustache
- Object
- Mustache
- Ronn::Template
- Defined in:
- lib/ronn/template.rb
Instance Attribute Summary collapse
-
#style_path ⇒ Object
Returns the value of attribute style_path.
Instance Method Summary collapse
- #custom_title? ⇒ Boolean
- #date ⇒ Object
- #generator ⇒ Object
-
#initialize(document, style_path = ENV['RONN_STYLE'].to_s.split(':')) ⇒ Template
constructor
A new instance of Template.
-
#inline_stylesheet(path, media = 'all') ⇒ Object
TEMPLATE CSS LOADING.
- #manual ⇒ Object
-
#missing_styles ⇒ Object
Array of style names for which no file could be found.
-
#name ⇒ Object
Basic document attributes.
- #name_and_section? ⇒ Boolean
- #organization ⇒ Object
- #page_name ⇒ Object
- #remote_stylesheet(name, media = 'all') ⇒ Object
- #render(template = 'default') ⇒ Object
- #section ⇒ Object
-
#section_heads ⇒ Object
Section TOCs.
-
#style_files ⇒ Object
Array of expanded stylesheet file names.
-
#styles ⇒ Object
Array of style module names as given on the command line.
- #stylesheet(path, media = 'all') ⇒ Object
-
#stylesheet_tags ⇒ Object
All embedded stylesheets.
-
#stylesheets ⇒ Object
Array of stylesheet info hashes.
- #tagline ⇒ Object (also: #tagline?)
- #title ⇒ Object
- #wrap_class_name ⇒ Object
Constructor Details
#initialize(document, style_path = ENV['RONN_STYLE'].to_s.split(':')) ⇒ Template
Returns a new instance of Template.
8 9 10 11 |
# File 'lib/ronn/template.rb', line 8 def initialize(document, style_path=ENV['RONN_STYLE'].to_s.split(':')) @document = document @style_path = style_path + [Template.template_path] end |
Instance Attribute Details
#style_path ⇒ Object
Returns the value of attribute style_path.
118 119 120 |
# File 'lib/ronn/template.rb', line 118 def style_path @style_path end |
Instance Method Details
#custom_title? ⇒ Boolean
45 46 47 |
# File 'lib/ronn/template.rb', line 45 def custom_title? !name_and_section? && tagline end |
#date ⇒ Object
69 70 71 |
# File 'lib/ronn/template.rb', line 69 def date @document.date.strftime('%B %Y') end |
#generator ⇒ Object
57 58 59 |
# File 'lib/ronn/template.rb', line 57 def generator "Ronn/v#{Ronn.version} (http://github.com/rtomayko/ronn/tree/#{Ronn.revision})" end |
#inline_stylesheet(path, media = 'all') ⇒ Object
TEMPLATE CSS LOADING
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ronn/template.rb', line 144 def inline_stylesheet(path, media='all') data = File.read(path) data.gsub!(%r|/\*.+?\*/|m, '') # comments data.gsub!(/([;{,]) *\n/m, '\1') # end-of-line whitespace data.gsub!(/\n{2,}/m, "\n") # collapse lines data.gsub!(/[; ]+\}/, '}') # superfluous trailing semi-colons data.gsub!(/([{;,+])[ ]+/, '\1') # whitespace around things data.gsub!(/[ \t]+/m, ' ') # coalescing whitespace elsewhere data.gsub!(/^/, ' ') # indent data.strip! [ "<style type='text/css' media='#{media}'>", "/* style: #{File.basename(path, '.css')} */", data, "</style>" ].join("\n ") end |
#manual ⇒ Object
61 62 63 |
# File 'lib/ronn/template.rb', line 61 def manual @document.manual end |
#missing_styles ⇒ Object
Array of style names for which no file could be found.
134 135 136 137 138 139 |
# File 'lib/ronn/template.rb', line 134 def missing_styles style_files. zip(files). select { |style, file| file.nil? }. map { |style, file| style } end |
#name ⇒ Object
Basic document attributes
20 21 22 |
# File 'lib/ronn/template.rb', line 20 def name @document.name end |
#name_and_section? ⇒ Boolean
33 34 35 |
# File 'lib/ronn/template.rb', line 33 def name_and_section? name && section end |
#organization ⇒ Object
65 66 67 |
# File 'lib/ronn/template.rb', line 65 def organization @document.organization end |
#page_name ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/ronn/template.rb', line 49 def page_name if section "#{name}(#{section})" else name end end |
#remote_stylesheet(name, media = 'all') ⇒ Object
162 163 164 165 |
# File 'lib/ronn/template.rb', line 162 def remote_stylesheet(name, media='all') path = File.("../template/#{name}.css", __FILE__) "<link rel='stylesheet' type='text/css' media='#{media}' href='#{path}'>" end |
#render(template = 'default') ⇒ Object
13 14 15 |
# File 'lib/ronn/template.rb', line 13 def render(template='default') super template[0,1] == '/' ? File.read(template) : partial(template) end |
#section ⇒ Object
24 25 26 |
# File 'lib/ronn/template.rb', line 24 def section @document.section end |
#section_heads ⇒ Object
Section TOCs
80 81 82 83 84 85 86 87 |
# File 'lib/ronn/template.rb', line 80 def section_heads @document.section_heads.map do |id, text| { :id => id, :text => text } end end |
#style_files ⇒ Object
Array of expanded stylesheet file names. If a file cannot be found, the resulting array will include nil elements in positions corresponding to the stylesheets array.
123 124 125 126 127 128 129 130 131 |
# File 'lib/ronn/template.rb', line 123 def style_files styles.map do |name| next name if name.include?('/') style_path. reject { |p| p.strip.empty? }. map { |p| File.join(p, "#{name}.css") }. detect { |file| File.exist?(file) } end end |
#styles ⇒ Object
Array of style module names as given on the command line.
93 94 95 |
# File 'lib/ronn/template.rb', line 93 def styles @document.styles end |
#stylesheet(path, media = 'all') ⇒ Object
167 168 169 |
# File 'lib/ronn/template.rb', line 167 def stylesheet(path, media='all') inline_stylesheet(name, media) end |
#stylesheet_tags ⇒ Object
All embedded stylesheets.
112 113 114 115 116 |
# File 'lib/ronn/template.rb', line 112 def stylesheets. map { |style| inline_stylesheet(style[:path], style[:media]) }. join("\n ") end |
#stylesheets ⇒ Object
Array of stylesheet info hashes.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ronn/template.rb', line 98 def stylesheets styles.zip(style_files).map do |name, path| base = File.basename(path, '.css') fail "style not found: #{style.inspect}" if path.nil? { :name => name, :path => path, :base => File.basename(path, '.css'), :media => (base =~ /(print|screen)$/) ? $1 : 'all' } end end |
#tagline ⇒ Object Also known as: tagline?
28 29 30 |
# File 'lib/ronn/template.rb', line 28 def tagline @document.tagline end |
#title ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/ronn/template.rb', line 37 def title if !name_and_section? && tagline tagline else [page_name, tagline].compact.join(' - ') end end |
#wrap_class_name ⇒ Object
73 74 75 |
# File 'lib/ronn/template.rb', line 73 def wrap_class_name 'mp' end |