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.



118
119
120
# File 'lib/ronn/template.rb', line 118

def style_path
  @style_path
end

Instance Method Details

#custom_title?Boolean

Returns:

  • (Boolean)


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

def custom_title?
  !name_and_section? && tagline
end

#dateObject



69
70
71
# File 'lib/ronn/template.rb', line 69

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

#generatorObject



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

#manualObject



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

def manual
  @document.manual
end

#missing_stylesObject

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

#nameObject

Basic document attributes



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

def name
  @document.name
end

#name_and_section?Boolean

Returns:

  • (Boolean)


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

def name_and_section?
  name && section
end

#organizationObject



65
66
67
# File 'lib/ronn/template.rb', line 65

def organization
  @document.organization
end

#page_nameObject



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.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



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_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.



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

#stylesObject

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_tagsObject

All embedded stylesheets.



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

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

#stylesheetsObject

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

#taglineObject Also known as: tagline?



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

def tagline
  @document.tagline
end

#titleObject



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_nameObject



73
74
75
# File 'lib/ronn/template.rb', line 73

def wrap_class_name
  'mp'
end