class Nanoc::Core::SiteLoader

Constants

ENCODING_REGEX

Public Class Methods

cwd_is_nanoc_site?() click to toggle source

@return [Boolean]

# File lib/nanoc/core/site_loader.rb, line 13
def self.cwd_is_nanoc_site?
  Nanoc::Core::ConfigLoader.cwd_is_nanoc_site?
end

Public Instance Methods

gen_data_source_for_config(config) click to toggle source
# File lib/nanoc/core/site_loader.rb, line 17
def gen_data_source_for_config(config)
  data_sources_to_aggregate =
    with_data_sources(config) do |data_sources|
      data_sources.map do |ds|
        Nanoc::Core::PrefixedDataSource.new(ds, ds.items_root, ds.layouts_root)
      end
    end

  Nanoc::Core::AggregateDataSource.new(data_sources_to_aggregate, config)
end
new_from_cwd() click to toggle source
# File lib/nanoc/core/site_loader.rb, line 8
def new_from_cwd
  site_from_config(Nanoc::Core::ConfigLoader.new.new_from_cwd)
end

Private Instance Methods

code_snippets_from_config(config) click to toggle source
# File lib/nanoc/core/site_loader.rb, line 72
def code_snippets_from_config(config)
  config[:lib_dirs].flat_map do |lib|
    Dir["#{lib}/**/*.rb"].sort.map do |filename|
      Nanoc::Core::CodeSnippet.new(
        read_code_snippet_contents(filename),
        filename,
      )
    end
  end
end
create_data_sources(config) click to toggle source
# File lib/nanoc/core/site_loader.rb, line 54
def create_data_sources(config)
  config[:data_sources].map do |data_source_hash|
    # Get data source class
    data_source_class = Nanoc::Core::DataSource.named(data_source_hash[:type].to_sym)
    if data_source_class.nil?
      raise Nanoc::Core::Errors::UnknownDataSource.new(data_source_hash[:type])
    end

    # Create data source
    data_source_class.new(
      config,
      data_source_hash[:items_root],
      data_source_hash[:layouts_root],
      data_source_hash.merge(data_source_hash[:config] || {}),
    )
  end
end
encoding_from_magic_comment(raw) click to toggle source
# File lib/nanoc/core/site_loader.rb, line 83
def encoding_from_magic_comment(raw)
  match = ENCODING_REGEX.match(raw)
  match ? match['encoding'] : nil
end
read_code_snippet_contents(filename) click to toggle source
# File lib/nanoc/core/site_loader.rb, line 88
def read_code_snippet_contents(filename)
  raw = File.read(filename, encoding: 'ASCII-8BIT')

  enc = encoding_from_magic_comment(raw)
  if enc
    raw = raw.force_encoding(enc).encode('UTF-8').sub(ENCODING_REGEX, '')
  else
    raw.force_encoding('UTF-8')
  end

  raw
end
site_from_config(config) click to toggle source
# File lib/nanoc/core/site_loader.rb, line 30
def site_from_config(config)
  code_snippets = code_snippets_from_config(config)
  code_snippets.each(&:load)

  data_source = gen_data_source_for_config(config)

  Nanoc::Core::Site.new(
    config: config,
    code_snippets: code_snippets,
    data_source: data_source,
  )
end
with_data_sources(config) { |data_sources| ... } click to toggle source
# File lib/nanoc/core/site_loader.rb, line 43
def with_data_sources(config, &_block)
  data_sources = create_data_sources(config)

  begin
    data_sources.each(&:use)
    yield(data_sources)
  ensure
    data_sources.each(&:unuse)
  end
end