Class: Frontman::Resource
- Inherits:
-
Object
- Object
- Frontman::Resource
- Extended by:
- T::Sig
- Defined in:
- lib/frontman/resource.rb
Instance Attribute Summary collapse
-
#compiled ⇒ Object
Returns the value of attribute compiled.
-
#data ⇒ Object
Returns the value of attribute data.
-
#destination_path ⇒ Object
Returns the value of attribute destination_path.
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#renderers ⇒ Object
Returns the value of attribute renderers.
Class Method Summary collapse
Instance Method Summary collapse
- #content_blocks ⇒ Object
- #indexable? ⇒ Boolean
- #inspect ⇒ Object
- #layout ⇒ Object
- #mtime ⇒ Object
- #parse_data_file ⇒ Object
- #parse_resource(parse_parent = false, data = nil) ⇒ Object
- #render(content_for_layout = nil, extra_data = {}) ⇒ Object
- #setup_destination ⇒ Object
- #strip_extensions(path) ⇒ Object
Instance Attribute Details
#compiled ⇒ Object
Returns the value of attribute compiled.
17 18 19 |
# File 'lib/frontman/resource.rb', line 17 def compiled @compiled end |
#data ⇒ Object
Returns the value of attribute data.
17 18 19 |
# File 'lib/frontman/resource.rb', line 17 def data @data end |
#destination_path ⇒ Object
Returns the value of attribute destination_path.
17 18 19 |
# File 'lib/frontman/resource.rb', line 17 def destination_path @destination_path end |
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
16 17 18 |
# File 'lib/frontman/resource.rb', line 16 def dir @dir end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
16 17 18 |
# File 'lib/frontman/resource.rb', line 16 def extension @extension end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
16 17 18 |
# File 'lib/frontman/resource.rb', line 16 def file @file end |
#file_path ⇒ Object
Returns the value of attribute file_path.
17 18 19 |
# File 'lib/frontman/resource.rb', line 17 def file_path @file_path end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
16 17 18 |
# File 'lib/frontman/resource.rb', line 16 def path @path end |
#renderers ⇒ Object
Returns the value of attribute renderers.
17 18 19 |
# File 'lib/frontman/resource.rb', line 17 def renderers @renderers end |
Class Method Details
.from_path(file_path, destination_path = nil, is_page = true) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/frontman/resource.rb', line 34 def from_path(file_path, destination_path = nil, is_page = true) destination_path ||= file_path file_path = file_path.gsub(%r{^/}, '') destination_path = destination_path.gsub(%r{^/}, '') .gsub(%r{/[0-9]+?-}, '/') .sub(%r{^source/}, '') # We cache the newly created resource so we avoid loosing the cache # if from_path is called again with the same file # This is especially important for perf in case of layouts and templates @@resources ||= {} @@resources[destination_path] ||= new( file_path, destination_path, is_page ) end |
.resources ⇒ Object
23 24 25 |
# File 'lib/frontman/resource.rb', line 23 def resources @@resources || {} end |
Instance Method Details
#content_blocks ⇒ Object
223 224 225 |
# File 'lib/frontman/resource.rb', line 223 def content_blocks @content_blocks ||= {} end |
#indexable? ⇒ Boolean
238 239 240 |
# File 'lib/frontman/resource.rb', line 238 def indexable? data.key?(:indexable) ? data[:indexable] : true end |
#inspect ⇒ Object
233 234 235 |
# File 'lib/frontman/resource.rb', line 233 def inspect "Resource: #{@file_path}" end |
#layout ⇒ Object
212 213 214 215 216 217 218 219 220 |
# File 'lib/frontman/resource.rb', line 212 def layout return nil unless @is_page Frontman::App.instance.layouts.each do |(glob, layout)| return layout if File.fnmatch(glob, @destination_path) end nil end |
#mtime ⇒ Object
228 229 230 |
# File 'lib/frontman/resource.rb', line 228 def mtime @mtime ||= File.mtime(@file_path) end |
#parse_data_file ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/frontman/resource.rb', line 83 def parse_data_file data_file = @file_path_without_extension + '.yml' return unless File.exist?(data_file) begin data = YAML.safe_load(File.read(data_file)).to_ostruct rescue Psych::SyntaxError => e raise("#{e} - #{data_file}") end @data[:yml] = data end |
#parse_resource(parse_parent = false, data = nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/frontman/resource.rb', line 101 def parse_resource(parse_parent = false, data = nil) @rendered_content = nil @content = File.read(@file_path) @data = {}.to_ostruct if Frontman::RendererResolver.instance.valid_extension?(@extension) @data, @content = YAML::FrontMatter.extract(@content).to_ostruct end @data = @data.to_h.merge(data.to_h).to_ostruct unless data.nil? @compiled = nil # If there is only one renderer we can precompile the file because there # is no dependency. This is critical to get good performances, especially # for templates and layouts that we also considered as Resources if @renderers.size == 1 @renderer = @renderers[0] @compiled = @renderer.compile(@content) end if parse_parent proxy_files = Frontman::SitemapTree.proxy_resources_from_template( @file_path ) if proxy_files.length proxy_files.each do |r| r.parse_resource(false, r.data) unless r == self end end end # We need to do that after parsing the resource so @data exists parse_data_file if @is_page end |
#render(content_for_layout = nil, extra_data = {}) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/frontman/resource.rb', line 145 def render(content_for_layout = nil, extra_data = {}) view_data = data.to_h.merge(extra_data).to_ostruct layout_path = layout layout_from_extra_data = false layout_from_extra_data = extra_data[:layout] if extra_data.key?(:layout) if view_data.layout layout_path = File.join( Frontman::Config.get(:layout_dir, fallback: 'views/layouts'), view_data.layout ) end if @is_page && !view_data[:ignore_page] # We store that in App so it can be accessed from any view Frontman::App.instance.current_page = self Frontman::App.instance.reset_ids_generation sitemap_tree = Frontman::App.instance.sitemap_tree.from_resource(self) Frontman::App.instance.current_tree = sitemap_tree end # If we have no layout to render and already cache the rendered content # we can return it directly return @rendered_content if layout_from_extra_data.nil? && !@rendered_content.nil? content = @content # We create a new context for each file getting rendered context = Frontman::Context.new if @compiled # If we manage to compile the template (only 1 renderer) # we render from the compiled template content = @renderer.render( @compiled, content_for_layout, context, view_data ) else # We loop over each renderer (infered from extensions of the file) # and render. The result of the 1st rendered is passed as a argument of # the second renderer and so on until we have the final content @renderers.each do |renderer| compiled = renderer.compile(content) content = renderer.render( compiled, content_for_layout, context, view_data ) end end # Cache the rendered content to avoid non necessary processing # We do this before rendering the layout because it's usually # what's common to every rendering @rendered_content = content # If a layout is specified we take the rendered content a inject it # inside the layout. # If layout:nil was specified when triggering the render we don't # render the layout and override the layout that can be set in # frontmatter or config.rb if layout_path && !layout_from_extra_data.nil? content = Resource.from_path(layout_path, nil, false).render(content) end content end |
#setup_destination ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/frontman/resource.rb', line 63 def setup_destination destination_without_extension, dest_file_extensions = strip_extensions( @destination_path ) destination_without_extension = '' if destination_without_extension == 'index' is_index_page = destination_without_extension.end_with?('/index') @extension = dest_file_extensions.first @destination_path = if (@extension == 'html' || extension.nil?) && !is_index_page destination_without_extension + '/index.html' else destination_without_extension + '.' + @extension end @path = "/#{@destination_path.chomp('index.html')}" .gsub('//', '/') end |
#strip_extensions(path) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/frontman/resource.rb', line 52 def strip_extensions(path) split = path.split('/') without_extension = T.must(split.last).split('.')[0] path_without_extensions = split.first(split.length - 1) .push(T.must(without_extension)) .join('/') extensions = T.must(split.last).split('.')[1..-1] [path_without_extensions, extensions] end |