class ContentFS::Content

Structured content, loaded from the filesystem and usually exposed through a database.

Constants

FRONT_MATTER_REGEXP
INCLUDE_REGEXP

Attributes

format[R]
metadata[R]
namespace[R]
prefix[R]
slug[R]

Public Class Methods

load(path, database:, metadata: {}, namespace: [], &block) click to toggle source
# File lib/contentfs/content.rb, line 15
def load(path, database:, metadata: {}, namespace: [], &block)
  new(path: path, database: database, metadata: metadata, namespace: namespace, &block)
end
new(path:, database:, metadata: {}, namespace: [], &block) click to toggle source
# File lib/contentfs/content.rb, line 25
def initialize(path:, database:, metadata: {}, namespace: [], &block)
  path = Pathname.new(path)
  extname = path.extname
  name = path.basename(extname)
  prefix, remainder = Prefix.build(name)
  @prefix = prefix
  @format = extname.to_s[1..]&.to_sym
  @slug = Slug.build(remainder)
  @namespace = namespace.dup << @slug
  @database = database

  content = path.read
  content = block.call(content) if block
  @metadata = metadata.merge(parse_metadata(content))
  @content = content.gsub(FRONT_MATTER_REGEXP, "")
end

Public Instance Methods

render() click to toggle source
# File lib/contentfs/content.rb, line 46
def render
  if @format && (renderer = Renderers.resolve(@format))
    resolve_includes(renderer.render(@content))
  else
    resolve_includes(to_s)
  end
end
to_s() click to toggle source
# File lib/contentfs/content.rb, line 42
def to_s
  @content
end

Private Instance Methods

parse_metadata(content) click to toggle source
# File lib/contentfs/content.rb, line 66
        def parse_metadata(content)
  if (match = content.match(FRONT_MATTER_REGEXP))
    YAML.safe_load(match.captures[0]).to_h
  else
    {}
  end
end
resolve_includes(content) click to toggle source
# File lib/contentfs/content.rb, line 54
        def resolve_includes(content)
  working_content = content.dup

  content.scan(INCLUDE_REGEXP) do |match|
    if (include = @database.find_include(match[0]))
      working_content.gsub!($~.to_s, include.render)
    end
  end

  working_content
end