Class: ContentFS::Content
- Inherits:
-
Object
- Object
- ContentFS::Content
- 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
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(path:, database:, metadata: {}, namespace: [], &block) ⇒ Content
constructor
A new instance of Content.
- #render ⇒ Object
- #to_s ⇒ Object
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
#format ⇒ Object (readonly)
Returns the value of attribute format.
23 24 25 |
# File 'lib/contentfs/content.rb', line 23 def format @format end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
23 24 25 |
# File 'lib/contentfs/content.rb', line 23 def end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
23 24 25 |
# File 'lib/contentfs/content.rb', line 23 def namespace @namespace end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
23 24 25 |
# File 'lib/contentfs/content.rb', line 23 def prefix @prefix end |
#slug ⇒ Object (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
#render ⇒ Object
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_s ⇒ Object
42 43 44 |
# File 'lib/contentfs/content.rb', line 42 def to_s @content end |