Class: Ronn::Template

Inherits:
Mustache
  • Object
show all
Defined in:
lib/ronn/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject

Returns the value of attribute style_path.



105
106
107
# File 'lib/ronn/template.rb', line 105

def style_path
  @style_path
end

Instance Method Details

#dateObject



56
57
58
# File 'lib/ronn/template.rb', line 56

def date
  @document.date.strftime('%B %Y')
end

#generatorObject



44
45
46
# File 'lib/ronn/template.rb', line 44

def generator
  "Ronn/v#{Ronn::VERSION} (http://github.com/rtomayko/ronn)"
end

#htmlObject



60
61
62
# File 'lib/ronn/template.rb', line 60

def html
  @document.to_html_fragment(wrap_class=nil)
end

#inline_stylesheet(path, media = 'all') ⇒ Object

TEMPLATE CSS LOADING



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ronn/template.rb', line 131

def inline_stylesheet(path, media='all')
  data = File.read(path)
  data.gsub!(/([;{]) *\n/m, '\1')  # end-of-line whitespace
  data.gsub!(/([;{]) +/m, '\1')    # coalescing whitespace elsewhere
  data.gsub!(/[; ]+\}/, '}')       # remove superfluous trailing semi-colons
  data.gsub!(%r|/\*.+?\*/|m, '')   # comments
  data.gsub!(/\n{2,}/m, "\n")      # collapse lines
  data.gsub!(/^/, '  ')
  data.strip!
  [
    "<style type='text/css' media='#{media}'>",
    "/* style: #{File.basename(path, '.css')} */",
    data,
    "</style>"
  ].join("\n  ")
end

#manualObject



48
49
50
# File 'lib/ronn/template.rb', line 48

def manual
  @document.manual
end

#missing_stylesObject

Array of style names for which no file could be found.



121
122
123
124
125
126
# File 'lib/ronn/template.rb', line 121

def missing_styles
  style_files.
    zip(files).
    select { |style, file| file.nil? }.
    map    { |style, file| style }
end

#nameObject

Basic document attributes



20
21
22
# File 'lib/ronn/template.rb', line 20

def name
  @document.name
end

#organizationObject



52
53
54
# File 'lib/ronn/template.rb', line 52

def organization
  @document.organization
end

#page_nameObject



36
37
38
39
40
41
42
# File 'lib/ronn/template.rb', line 36

def page_name
  if section
    "#{name}(#{section})"
  else
    name
  end
end

#remote_stylesheet(name, media = 'all') ⇒ Object



148
149
150
151
# File 'lib/ronn/template.rb', line 148

def remote_stylesheet(name, media='all')
  path = File.expand_path("../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

#sectionObject



24
25
26
# File 'lib/ronn/template.rb', line 24

def section
  @document.section
end

#section_headsObject

Section TOCs



67
68
69
70
71
72
73
74
# File 'lib/ronn/template.rb', line 67

def section_heads
  @document.section_heads.map do |id, text|
    {
      :id   => id,
      :text => text
    }
  end
end

#style_filesObject

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.



110
111
112
113
114
115
116
117
118
# File 'lib/ronn/template.rb', line 110

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

#stylesObject

Array of style module names as given on the command line.



80
81
82
# File 'lib/ronn/template.rb', line 80

def styles
  @document.styles
end

#stylesheet(path, media = 'all') ⇒ Object



153
154
155
# File 'lib/ronn/template.rb', line 153

def stylesheet(path, media='all')
  inline_stylesheet(name, media)
end

#stylesheet_tagsObject

All embedded stylesheets.



99
100
101
102
103
# File 'lib/ronn/template.rb', line 99

def stylesheet_tags
  stylesheets.
    map { |style| inline_stylesheet(style[:path], style[:media]) }.
    join("\n  ")
end

#stylesheetsObject

Array of stylesheet info hashes.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ronn/template.rb', line 85

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

#taglineObject



28
29
30
# File 'lib/ronn/template.rb', line 28

def tagline
  @document.tagline
end

#titleObject



32
33
34
# File 'lib/ronn/template.rb', line 32

def title
  [page_name, tagline].compact.join(' - ')
end