class Polites::Nanoc::DataSource

A data source for Nanoc that creates Nanoc content items from Polites files in a particular directory.

Public Instance Methods

items() click to toggle source
# File lib/polites/nanoc/data_source.rb, line 25
def items
  @input_files.flat_map do |input_file|
    File.open(input_file) do |file|
      sheet = @parser.parse_sheet(file.content)

      inline_file_items = sheet.inline_files.map do |image|
        build_file_item(file.media(image.image), image.image, input_file, image.filename)
      end

      file_items = sheet.attached_files.map do |id|
        build_file_item(file.media(id), id, input_file)
      end

      [
        new_item(
          @formatter.call(sheet),
          {
            keywords: sheet.keywords,
            image: sheet.attached_files.first,
            image_caption: sheet.notes.any? ? @formatter.call(sheet.notes.first) : nil,
            inline_file_items: inline_file_items,
            filename: input_file.to_s,
            mtime: input_file.mtime
          },
          identifier(input_file)
        ),
        *inline_file_items,
        *file_items
      ]
    end
  end
end
up() click to toggle source
# File lib/polites/nanoc/data_source.rb, line 16
def up
  @root = Pathname(@config[:path])
  @settings = Settings.from_directory(@root)
  @extension = ".#{@settings['defaultPathExtensions']}"
  @input_files = @root.glob("*#{@extension}")
  @parser = Polites::Parser.new
  @formatter = Polites::HtmlFormatter.new
end

Private Instance Methods

build_file_item(entry, id, input_file, filename = nil) click to toggle source
# File lib/polites/nanoc/data_source.rb, line 60
def build_file_item(entry, id, input_file, filename = nil)
  p = filename ? Pathname(filename) : Pathname(entry.name).basename
  i = "#{identifier(input_file)}/media#{identifier(p, p.extname)}"
  new_item(
    input_file.expand_path.to_s,
    {
      explicit_filename: filename,
      id: id,
      subpath: entry.name,
      mtime: input_file.mtime
    },
    i,
    binary: true
  )
end
identifier(path, extension = @extension) click to toggle source
# File lib/polites/nanoc/data_source.rb, line 76
def identifier(path, extension = @extension)
  "/#{path
    .relative_path_from(@root)
    .basename(extension)
    .to_s
    .then { |s| underscore(s) }}#{extension}"
end
underscore(str) click to toggle source
# File lib/polites/nanoc/data_source.rb, line 84
def underscore(str)
  str
    .gsub(/[^a-zA-Z0-9\-_]/, '-')
    .squeeze('-')
    .gsub(/^-*|-*$/, '')
    .downcase
end