Class: ContentFS::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/contentfs/content.rb

Overview

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

Constant Summary collapse

FRONT_MATTER_REGEXP =
/\A---\s*\n(.*?\n?)^---\s*$\n?/m
INCLUDE_REGEXP =
/<!-- @include\s*([a-zA-Z0-9\-_\/.]*) -->/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, database:, metadata: {}, namespace: [], &block) ⇒ Content

Returns a new instance of Content.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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
   = .merge((content))
  @content = content.gsub(FRONT_MATTER_REGEXP, "")
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



23
24
25
# File 'lib/contentfs/content.rb', line 23

def format
  @format
end

#metadataObject (readonly)

Returns the value of attribute metadata.



23
24
25
# File 'lib/contentfs/content.rb', line 23

def 
  
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



23
24
25
# File 'lib/contentfs/content.rb', line 23

def namespace
  @namespace
end

#prefixObject (readonly)

Returns the value of attribute prefix.



23
24
25
# File 'lib/contentfs/content.rb', line 23

def prefix
  @prefix
end

#slugObject (readonly)

Returns the value of attribute slug.



23
24
25
# File 'lib/contentfs/content.rb', line 23

def slug
  @slug
end

Class Method Details

.load(path, database:, metadata: {}, namespace: [], &block) ⇒ Object



15
16
17
# File 'lib/contentfs/content.rb', line 15

def load(path, database:, metadata: {}, namespace: [], &block)
  new(path: path, database: database, metadata: , namespace: namespace, &block)
end

Instance Method Details

#renderObject



46
47
48
49
50
51
52
# 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_sObject



42
43
44
# File 'lib/contentfs/content.rb', line 42

def to_s
  @content
end