module Middleman::Sculptor::Helpers::Resources

Public Instance Methods

append_class(block_name, appended_classes='') click to toggle source

Use in layouts, in templates either Frontmatter or this method can be used

# File lib/middleman-sculptor/helpers/resources.rb, line 71
def append_class(block_name, appended_classes='')
  current_page_classes = current_page.data[block_name] || ''
  existing_classes_a = current_page_classes.split(' ')
  appended_classes_a = appended_classes.split(' ')
  classes = existing_classes_a.concat(appended_classes_a).reverse.join(' ')
  content_for(block_name, classes)
end
get(url, options = {}) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 79
def get(url, options = {})
  begin
    result = RestClient.get(url, options)
  rescue => e
    raise "GET - #{e.message}: '#{url}'"
  end

  Oj.load(result)
end
include_javascripts(javascripts) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 7
def include_javascripts(javascripts)
  include_assets(:javascript_include_tag, javascripts)
end
include_stylesheets(stylesheets) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 11
def include_stylesheets(stylesheets)
  include_assets(:stylesheet_link_tag, stylesheets)
end
local_data(path) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 52
def local_data(path)
  current_path =  current_resource.path
  if current_resource.metadata.page.has_key? :local_url
    current_path = current_resource.metadata.page.local_url
  end
  result = sitemap.find_resource_by_path(relative_dir(current_path, path).to_s)
  raise "#{path} not found" unless result

  case result.ext
  when '.yaml', '.yml'
    result = YAML.load(result.render)
  when '.json'
    result = Oj.load(result.render)
  end

  result
end
main_sections() click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 46
def main_sections
  resources_for('/')
    .select {|r| r.parent.url == '/' }
    .reject {|r| r.url == '/./'}
end
post(url, params = {}, headers = {}) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 89
def post(url, params = {}, headers = {})
  begin
    result = RestClient.post(url, params, headers)
  rescue => e
    raise "POST - #{e.message}: '#{url}'"
  end

  Oj.load(result)
end
resource_dir(resource) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 19
def resource_dir(resource)
  resource.url.split('/').last
end
resource_file(resource) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 15
def resource_file(resource)
  resource.path.split('/').last.gsub(resource.ext, '')
end
resource_tree(dir, ext: 'html', data_fields: [:title], exclude_indexes: false, sort_by: nil, ignore: nil) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 33
def resource_tree(dir, ext: 'html', data_fields: [:title], exclude_indexes: false, sort_by: nil, ignore: nil)
  resources_for(dir, ext: ext, exclude_indexes: exclude_indexes, sort_by: sort_by, ignore: ignore)
    .map { |r| parse_resource(r, {
        ext: ext,
        data_fields: data_fields,
        exclude_indexes: exclude_indexes,
        sort_by: sort_by,
        ignore: ignore
      })
    }
    .reject { |r| r[:parent] != '/' }  # Remove non-root directories from the root
end
resources_for(dir, ext: 'html', exclude_indexes: false, sort_by: nil, ignore: nil, allow_hidden: false) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 23
def resources_for(dir, ext: 'html', exclude_indexes: false, sort_by: nil, ignore: nil, allow_hidden: false)
  filtered = sitemap.resources
    .reject  {|r| r.url == dir}                      # Exclude main directory index
    .select  {|r| r.url.start_with?(dir)}            # Select files in the given dir

  collect_resources(filtered,
    { ext: ext, exclude_indexes: exclude_indexes, sort_by: sort_by, ignore: ignore, allow_hidden: allow_hidden}
  )
end

Private Instance Methods

breadcrumbs(page=current_page, separator="›", remove_current=true) click to toggle source
collect_resources(resources, options) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 101
def collect_resources(resources, options)
  resources
    .select  {|r| r.ext == ".#{options[:ext]}"}      # Select only files with specified extension
    .sort_by {|r| r.url }                            # Sort by url (default)
    .sort_by {|r| r.data[options[:sort_by]] || -1}   # Sort by `sort_by` param
    .reject  {|r|                                    # Reject hidden (Front matter)
      r.data.hidden unless options[:allow_hidden]
    }
    .reject  {|r|                                    # Exclude all directory indexes
      options[:exclude_indexes] ? r.directory_index? : false
    }
    .reject  {|r| ignore ? r.url.match(options[:ignore]) : false }  # Ignore URLs matching pattern (if provided)
    .reject  {|r| r.path.end_with? ("-isolated#{r.ext}")}         # Ignore proxied '-isolated' mode pages
    .reject  {|r| r.path.start_with? ("glyptotheque/")}             # Ignore Sculptor’s partials
end
get_section_of_resource(resource) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 182
def get_section_of_resource(resource)
  dir = File.dirname(resource.path)
  title = dir.upcase

  if resource.url == '/'
    title = resource.data.title || site_title
  elsif resource.metadata.locals[:section]
    title = resource.data.title if resource.data.title
  end

  {
    title: title,
    slug: dir.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '-')
  }
end
include_assets(asset_tag, assets) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 147
def include_assets(asset_tag, assets)
  return unless assets
  assets = assets.split(/,\s*/) if assets.is_a? String
  resource_path = current_page.metadata && current_page.metadata.page[:local_url] || current_page.path
  Array(assets).map { |a|
    path = a.start_with?('http') ? a : relative_dir(resource_path, a)
    "\n" + method(asset_tag).call(path)
  }.join
end
page_title(page=current_page, separator='›') click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 175
def page_title(page=current_page, separator='›')
  title = strip_tags(breadcrumbs(current_page, separator, false))
    .split(separator)
    .reverse
    .join(" #{separator} ")
end
parse_resource(r, options) click to toggle source
# File lib/middleman-sculptor/helpers/resources.rb, line 117
def parse_resource(r, options)
  data = {}
  data[:path] = url_for("/#{r.path}")
  data[:url] = r.url

  # Add parent to top-level pages (not containing `/` in path)
  if /^((?!\/).)*$/.match r.path
    data[:parent] = '/'
  end

  if r.children.any?
    data[:children] = collect_resources(r.children, options).map { |c| parse_resource(c, options) }
    if r.parent
      data[:parent] = r.parent.url
    end
  end

  r.add_metadata(locals: {
    section: get_section_of_resource(r)
  })

  options[:data_fields].each do |field|
    data[field] = r.data[field]
    if field == :title && !r.data.title
      data[field] = resource_dir(r).upcase
    end
  end
  data
end
relative_dir(path, *args) click to toggle source

Constructs path relative to base path (first argument)

# File lib/middleman-sculptor/helpers/resources.rb, line 158
def relative_dir(path, *args)
  relative_path = args ? args.join('/') : ''
  Pathname(path).dirname.join(relative_path)
end