Class | Ronn::Template |
In: |
lib/ronn/template.rb
|
Parent: | Mustache |
style_path | [RW] |
# File lib/ronn/template.rb, line 8 8: def initialize(document, style_path=ENV['RONN_STYLE'].to_s.split(':')) 9: @document = document 10: @style_path = style_path + [Template.template_path] 11: end
# File lib/ronn/template.rb, line 45 45: def custom_title? 46: !name_and_section? && tagline 47: end
# File lib/ronn/template.rb, line 57 57: def generator 58: "Ronn/v#{Ronn.version} (http://github.com/rtomayko/ronn/tree/#{Ronn.revision})" 59: end
TEMPLATE CSS LOADING
# File lib/ronn/template.rb, line 144 144: def inline_stylesheet(path, media='all') 145: data = File.read(path) 146: data.gsub!(%r|/\*.+?\*/|m, '') # comments 147: data.gsub!(/([;{,]) *\n/m, '\1') # end-of-line whitespace 148: data.gsub!(/\n{2,}/m, "\n") # collapse lines 149: data.gsub!(/[; ]+\}/, '}') # superfluous trailing semi-colons 150: data.gsub!(/([{;,+])[ ]+/, '\1') # whitespace around things 151: data.gsub!(/[ \t]+/m, ' ') # coalescing whitespace elsewhere 152: data.gsub!(/^/, ' ') # indent 153: data.strip! 154: [ 155: "<style type='text/css' media='#{media}'>", 156: "/* style: #{File.basename(path, '.css')} */", 157: data, 158: "</style>" 159: ].join("\n ") 160: end
Array of style names for which no file could be found.
# File lib/ronn/template.rb, line 134 134: def missing_styles 135: style_files. 136: zip(files). 137: select { |style, file| file.nil? }. 138: map { |style, file| style } 139: end
Basic document attributes
# File lib/ronn/template.rb, line 20 20: def name 21: @document.name 22: end
# File lib/ronn/template.rb, line 49 49: def page_name 50: if section 51: "#{name}(#{section})" 52: else 53: name 54: end 55: end
# File lib/ronn/template.rb, line 162 162: def remote_stylesheet(name, media='all') 163: path = File.expand_path("../template/#{name}.css", __FILE__) 164: "<link rel='stylesheet' type='text/css' media='#{media}' href='#{path}'>" 165: end
# File lib/ronn/template.rb, line 13 13: def render(template='default') 14: super template[0,1] == '/' ? File.read(template) : partial(template) 15: end
Section TOCs
# File lib/ronn/template.rb, line 80 80: def section_heads 81: @document.section_heads.map do |id, text| 82: { 83: :id => id, 84: :text => text 85: } 86: end 87: end
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.
# File lib/ronn/template.rb, line 123 123: def style_files 124: styles.map do |name| 125: next name if name.include?('/') 126: style_path. 127: reject { |p| p.strip.empty? }. 128: map { |p| File.join(p, "#{name}.css") }. 129: detect { |file| File.exist?(file) } 130: end 131: end
Array of style module names as given on the command line.
# File lib/ronn/template.rb, line 93 93: def styles 94: @document.styles 95: end
# File lib/ronn/template.rb, line 167 167: def stylesheet(path, media='all') 168: inline_stylesheet(name, media) 169: end
All embedded stylesheets.
# File lib/ronn/template.rb, line 112 112: def stylesheet_tags 113: stylesheets. 114: map { |style| inline_stylesheet(style[:path], style[:media]) }. 115: join("\n ") 116: end
Array of stylesheet info hashes.
# File lib/ronn/template.rb, line 98 98: def stylesheets 99: styles.zip(style_files).map do |name, path| 100: base = File.basename(path, '.css') 101: fail "style not found: #{style.inspect}" if path.nil? 102: { 103: :name => name, 104: :path => path, 105: :base => File.basename(path, '.css'), 106: :media => (base =~ /(print|screen)$/) ? $1 : 'all' 107: } 108: end 109: end
# File lib/ronn/template.rb, line 37 37: def title 38: if !name_and_section? && tagline 39: tagline 40: else 41: [page_name, tagline].compact.join(' - ') 42: end 43: end