Content Directory
Content Directory is a lightweight replacement of Content Management System. It provides structure for text based content. It comes with a parser, which allows content entries to have metadata and rich formatting.
Structure
Content Directory does not require a database. Instead, it uses file system. An entry is a file written in Markdown syntax (with one of the following extensions: .md, .markdown, .txt, .text). You can group related entries in a folder.
- content
- home
- main.md
- features
- collaboration.md
- manage.md
- posts
- a-new-blog.md
- sortable-stars.md
Writing entries
Entry uses Markdown syntax, but there is an additional rule that every entry must have a metadata declaration on the top. Metadata declaration block uses YAML syntax.
Title: Introduction
Date: 2013/03/10
Tags: ["post", "short"]
It was a bright cold day in April, and the clocks were striking
thirteen. Winston Smith, his chin nuzzled into his breast in an
effort to escape the vile wind, slipped quickly through the
glass doors of *Victory Mansions*, though not quickly enough to
prevent a swirl of gritty dust from entering along with him.
The hallway smelt of boiled cabbage and old rag mats. At one end
of it a coloured poster, too large for indoor display, had been
tacked to the wall. It depicted simply an **enormous** face,
more than a metre wide: the face of a man of about forty-five,
with a heavy black moustache and ruggedly handsome features.
Reading entries
Once you have written entries, you can use ContentDirectory.find
to get the processed entries and render them into views.
ContentDirectory.find
returns a Hash
of ContentDirectory::Entry
. It accepts one argument, which is a path relative to ContentDirectory.root
. If path is not specified, ContentDirectory.find
will find all possible entries.
posts = ContentDirectory.find "posts"
You can use metadata to sort posts by date
posts = ContentDirectory.find("posts").values
posts.sort_by! { |post| post.metadata["Date"] }
Entry has three important values: metadata
, text
, html
.
-
metadata
is aHash
of parsed metadata block from entry file. -
text
is aString
of original entry text. -
html
is aString
of the result of original text after processed by Redcarpet Markdown parser.
You can you these three values to render entry to a view.
for post in posts
puts post.metadata["Title"]
puts post.text
puts post.html
end
Installation
Add this line to your application's Gemfile:
gem "content_directory"
And then execute:
$ bundle
Or install it yourself as:
$ gem install content_directory
Configuration
ContentDirectory.root is default to "#{Rails.root}/content"
when used in Rails. If you want to use other directory, you can easily change it in the initializer.
ContentDirectory.root = "path/to/content"
Use with Rails
Content Directory is intended to be used in Rails to extract content from view templates. In this fashion, content can be easily reused and queried.
- Create
content
directory in Rails root path. - Write entries in this directory.
- Use
ContentDirectory.find
in the controllers. - Render entry
html
ortext
in the templates.
Test
rake test
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request