Permalink
Browse files

put example code into separate files, and include with {% code_snippe…

…t ... %}
  • Loading branch information...
1 parent 5c5816b commit 1713916093bb4c9ee2ba66c8cd3218f875dd3f3e @tlossen tlossen committed Apr 11, 2010
View
@@ -3,32 +3,55 @@ require 'rdiscount'
require 'liquid'
require 'yaml'
+# custom liquid tag to include code blocks
+class CodeSnippet < Liquid::Tag
+ def initialize(tag_name, filename, tokens)
+ super
+ @filename = filename.strip
+ end
+
+ def render(context)
+ out = StringIO.new
+ open(@filename).each do |line|
+ out.write "\t#{line}"
+ end
+ out.string
+ end
+end
+
+Liquid::Template.register_tag('code_snippet', CodeSnippet)
+
+
task :default do
# copy static stuff
`rm -rf public
mkdir public
cp -r site/* public
rm -rf public/_*`
+
+ layout = Liquid::Template.parse(open('site/_views/layout.liquid').read())
# generate recipe pages
- layout = Liquid::Template.parse(open('site/_views/layout.liquid').read())
- recipes = Dir['recipes/*/*/recipe.md'].map do |source|
- meta = YAML.load(open(source.sub('/recipe.md', '/meta.yml')).read())
- name = source.split('/')[-2] + '.html'
+ recipe_header = open('site/_views/recipe_header.liquid').read()
+ recipes = Dir['recipes/*/*/'].map do |dir|
+ name = dir.split('/').last + '.html'
puts dest = "public/#{name}"
open(dest, 'w') do |out|
- content = RDiscount.new(open(source).read()).to_html
- out.write layout.render('meta' => meta, 'content' => content)
+ Dir.chdir(dir) do
+ meta = YAML.load(open('meta.yml').read())
+ recipe = Liquid::Template.parse(recipe_header + open('recipe.md').read())
+ markdown = recipe.render('meta' => meta)
+ content = RDiscount.new(markdown).to_html
+ out.write layout.render('meta' => meta, 'content' => content)
+ { 'title' => meta['title'], 'href' => name }
+ end
end
- { 'title' => meta['title'], 'href' => name }
end.compact
# generate index page
- index = Liquid::Template.parse(open('site/_views/index.liquid').read())
puts dest = 'public/index.html'
open(dest, 'w') do |out|
- content = index.render('recipes' => recipes)
- meta = { 'title' => 'Recipes' }
- out.write layout.render('meta' => meta, 'content' => content)
+ index = Liquid::Template.parse(open('site/_views/index.liquid').read())
+ out.write layout.render('content' => index.render('recipes' => recipes))
end
end
@@ -0,0 +1,23 @@
+require 'ohm'
+
+class Event < Ohm::Model
+ attribute :name
+ reference :venue, Venue
+ set :participants, Person
+ counter :votes
+
+ index :name
+
+ def validate
+ assert_present :name
+ end
+end
+
+class Venue < Ohm::Model
+ attribute :name
+ collection :events, Event
+end
+
+class Person < Ohm::Model
+ attribute :name
+end
@@ -14,43 +14,12 @@ and instead use a very simple domain-specific language to achieve similar result
Consider the following example in Ohm. Here, we'll model three arbitrary objects (an Event, a
Venue, and a Person), and provide validations for the Event.
- class Event < Ohm::Model
- attribute :name
- reference :venue, Venue
- set :participants, Person
- counter :votes
-
- index :name
-
- def validate
- assert_present :name
- end
- end
-
- class Venue < Ohm::Model
- attribute :name
- collection :events, Event
- end
-
- class Person < Ohm::Model
- attribute :name
- end
+{% code_snippet ohm.rb %}
The Ruby libraries also employ a simple style to model associations. Here is an example of how to create a
familiar 'has_many' relationship, using remodel.
- require 'remodel'
-
- class Cookbook < remodel::Entity
- has_many :recipes, :class => 'Recipe', :reverse => :book
- property :title, :class => String
- property :author, :class => String
- end
-
- class Recipe < remodel::Entity
- has_one :book, :class => Cookbook, :reverse => :recipes
- property :name, :class => String
- end
+{% code_snippet remodel.rb %}
If you store the above as `cookbook.rb` and have Redis running locally,
then you can open a Ruby shell and do:
@@ -0,0 +1,12 @@
+require 'remodel'
+
+class Cookbook < Remodel::Entity
+ has_many :recipes, :class => 'Recipe', :reverse => :book
+ property :title, :class => String
+ property :author, :class => String
+end
+
+class Recipe < Remodel::Entity
+ has_one :book, :class => Cookbook, :reverse => :recipes
+ property :name, :class => String
+end
View
@@ -1,4 +1,6 @@
-<ul>
+<h1>Recipes</h1>
+
+<ul class='index'>
{% for recipe in recipes %}
<li><a href="{{ recipe.href }}">{{ recipe.title }}</a></li>
{% endfor %}
View
@@ -1,7 +1,7 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Redis Cookbook</title>
+ <title>The Redis Cookbook</title>
<link rel="stylesheet" href="/css/style.css" />
</head>
<body>
@@ -13,19 +13,7 @@
</div>
<div class='main'>
-
- <h1>{{ meta.title }}</h1>
-
- <ul class='credit'>
- {% for name in meta.credit %}
- <li>{{ name }}</li>
- {% endfor %}
- </ul>
-
- <div class='content'>
- {{ content }}
- </div>
-
+ {{ content }}
</div>
<div class='footer'>
@@ -0,0 +1,6 @@
+<h1>{{ meta.title }}</h1>
+
+<div class='credit'>
+ Credit: {% for name in meta.credit %}{{ name }}{% unless forloop.last %}, {% endunless %}{% endfor %}
+</div>
+
View
@@ -25,7 +25,6 @@ p {
h1 {
font-size: 30px;
color: #6686BD;
- text-shadow: 1px 1px 1px #000;
}
h2 {
@@ -75,20 +74,18 @@ body pre {
width: 750px;
}
-.credit li {
- list-style-type: none;
+.credit {
font-style: italic;
}
-.content li {
+.index li {
list-style-image: url("/img/icon1.png");
list-style-type: square;
}
.footer {
clear: both;
- padding-left: 100px;
- padding-top: 25px;
+ padding: 25px 0 30px 100px;
border-top: 2px solid #999;
height: 40px;
}

0 comments on commit 1713916

Please sign in to comment.