Class: Ronn::Template
- Inherits:
-
Mustache
- Object
- Mustache
- Ronn::Template
- Defined in:
- lib/ronn/template.rb
Overview
A Mustache Template for HTML formatting.
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.
9 10 11 12 13 |
# File 'lib/ronn/template.rb', line 9 def initialize(document, style_path = ENV['RONN_STYLE'].to_s.split(':')) super() @document = document @style_path = style_path + [Template.template_path] end |
Instance Attribute Details
#style_path ⇒ Object
Returns the value of attribute style_path.
120 121 122 |
# File 'lib/ronn/template.rb', line 120 def style_path @style_path end |
Instance Method Details
#custom_title? ⇒ Boolean
47 48 49 |
# File 'lib/ronn/template.rb', line 47 def custom_title? !name_and_section? && tagline end |
#date ⇒ Object
71 72 73 |
# File 'lib/ronn/template.rb', line 71 def date @document.date.strftime('%B %Y') end |
#generator ⇒ Object
59 60 61 |
# File 'lib/ronn/template.rb', line 59 def generator "Ronn-NG/v#{Ronn.version} (http://github.com/apjanke/ronn-ng/tree/#{Ronn.revision})" end |
#inline_stylesheet(path, media = 'all') ⇒ Object
TEMPLATE CSS LOADING
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ronn/template.rb', line 146 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
63 64 65 |
# File 'lib/ronn/template.rb', line 63 def manual @document.manual end |
#missing_styles ⇒ Object
Array of style names for which no file could be found.
136 137 138 139 140 141 |
# File 'lib/ronn/template.rb', line 136 def missing_styles style_files .zip(files) .select { |_style, file| file.nil? } .map { |style, _file| style } end |
#name ⇒ Object
Basic document attributes
22 23 24 |
# File 'lib/ronn/template.rb', line 22 def name @document.name end |
#name_and_section? ⇒ Boolean
35 36 37 |
# File 'lib/ronn/template.rb', line 35 def name_and_section? name && section end |
#organization ⇒ Object
67 68 69 |
# File 'lib/ronn/template.rb', line 67 def organization @document.organization end |
#page_name ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/ronn/template.rb', line 51 def page_name if section "#{name}(#{section})" else name end end |
#remote_stylesheet(name, media = 'all') ⇒ Object
164 165 166 167 |
# File 'lib/ronn/template.rb', line 164 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
15 16 17 |
# File 'lib/ronn/template.rb', line 15 def render(template = 'default') super(template[0, 1] == '/' ? File.read(template) : partial(template)) end |
#section ⇒ Object
26 27 28 |
# File 'lib/ronn/template.rb', line 26 def section @document.section end |
#section_heads ⇒ Object
Section TOCs
82 83 84 85 86 87 88 89 |
# File 'lib/ronn/template.rb', line 82 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.
125 126 127 128 129 130 131 132 133 |
# File 'lib/ronn/template.rb', line 125 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.
95 96 97 |
# File 'lib/ronn/template.rb', line 95 def styles @document.styles end |
#stylesheet(_path, media = 'all') ⇒ Object
169 170 171 |
# File 'lib/ronn/template.rb', line 169 def stylesheet(_path, media = 'all') inline_stylesheet(name, media) end |
#stylesheet_tags ⇒ Object
All embedded stylesheets.
114 115 116 117 118 |
# File 'lib/ronn/template.rb', line 114 def stylesheets .map { |style| inline_stylesheet(style[:path], style[:media]) } .join("\n ") end |
#stylesheets ⇒ Object
Array of stylesheet info hashes.
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ronn/template.rb', line 100 def stylesheets styles.zip(style_files).map do |name, path| base = File.basename(path, '.css') raise "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?
30 31 32 |
# File 'lib/ronn/template.rb', line 30 def tagline @document.tagline end |
#title ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/ronn/template.rb', line 39 def title if !name_and_section? && tagline tagline else [page_name, tagline].compact.join(' - ') end end |
#wrap_class_name ⇒ Object
75 76 77 |
# File 'lib/ronn/template.rb', line 75 def wrap_class_name 'mp' end |