support link posts

This commit is contained in:
Sami Samhuri 2011-11-27 17:33:30 -08:00
parent ef0c83024d
commit 05d7c91670
9 changed files with 75 additions and 62 deletions

View file

@ -35,23 +35,15 @@ h1 { margin: 0
.date { float: right }
div#posts { width: 80%
; min-width: 400px
; max-width: 680px
; margin: 4em auto 2em
; padding: 0 5%
; font-size: 1.2em
; line-height: 1.4em
}
article { color: #222
; padding-bottom: 1em
article { width: 80%
; min-width: 400px
; max-width: 680px
; margin: 2em auto 1em
; font-size: 1.2em
; line-height: 1.4em
; color: #222
}
article:last-child { padding-bottom: 0
; margin-bottom: 1em
}
article h1 { text-align: left
; font-size: 2em
; font-weight: normal

View file

@ -67,15 +67,10 @@
#blog article h1 { font-size: 1.2em }
div#posts { width: 100%
; margin: 0
; padding: 0
; border-left: none
; padding-left: 10px
}
article { margin: 0
; padding: 0
article { width: 100%
; padding-left: 10px
; margin: 0
; padding: 0 0 0 10px
; font-size: 0.8em
}

39
blog.rb
View file

@ -49,29 +49,33 @@ class Blag
def generate_index
# generate landing page
index_template = File.read(File.join('templates', 'blog', 'index.html'))
post = posts.first
template = post[:link] ? link_template : post_template
values = { :posts => posts,
:post => posts.first,
:article => Mustache.render(article_template, posts.first),
:post => post,
:article => Mustache.render(template, post),
:previous => posts[1],
:filename => posts.first[:filename],
:comments => posts.first[:comments]
:filename => post[:filename],
:comments => post[:comments]
}
index_html = Mustache.render(index_template, values)
File.open(File.join(@dest, 'index.html'), 'w') {|f| f.puts(index_html) }
end
def generate_posts
template = File.read(File.join('templates', 'blog', 'post.html'))
page_template = File.read(File.join('templates', 'blog', 'post.html'))
posts.each_with_index do |post, i|
template = post[:link] ? link_template : post_template
values = { :title => post[:title],
:article => Mustache.render(article_template, post),
:link => post[:link],
:article => Mustache.render(template, post),
:previous => i < posts.length - 1 && posts[i + 1],
:next => i > 0 && posts[i - 1],
:filename => post[:filename],
:comments => post[:comments],
:keywords => (DefaultKeywords + post[:tags]).join(',')
}
post[:html] = Mustache.render(template, values)
post[:html] = Mustache.render(page_template, values)
File.open(File.join(@dest, post[:filename]), 'w') {|f| f.puts(post[:html]) }
end
end
@ -93,7 +97,7 @@ class Blag
post = { :filename => filename.sub(prefix, '').sub(/\.m(ark)?d(own)?$/i, '.html') }
loop do
line = lines.shift.strip
m = line.match(/(\w+):/)
m = line.match(/^(\w+):/)
if m && param = m[1].downcase
post[param.to_sym] = line.sub(Regexp.new('^' + param + ':\s*', 'i'), '').strip
elsif line.match(/^----\s*$/)
@ -103,12 +107,13 @@ class Blag
puts "ignoring unknown header: #{line}"
end
end
post[:url] = @url + '/' + post[:filename]
post[:content] = lines.join
post[:rss_html] = Mustache.render(post_rss_template, {:post => post})
template = post[:link] ? link_rss_template : post_rss_template
post[:rss_html] = Mustache.render(template, {:post => post})
post[:body] = RDiscount.new(post[:content]).to_html
post[:rfc822] = Time.parse(post[:date]).rfc822
post[:tags] = (post[:tags] || '').split(/\s*,\s*/).map(&:strip)
post[:url] = @url + '/' + post[:filename]
# comments on by default
post[:comments] = true if post[:comments].nil?
post
@ -121,8 +126,12 @@ class Blag
private
def article_template
@article_template ||= File.read(File.join('templates', 'blog', 'article.mustache'))
def post_template
@article_template ||= File.read(File.join('templates', 'blog', 'post.mustache'))
end
def link_template
@article_template ||= File.read(File.join('templates', 'blog', 'link.mustache'))
end
def blog_file
@ -133,6 +142,10 @@ class Blag
@post_rss_template ||= File.read(File.join('templates', 'blog', 'post.rss.html'))
end
def link_rss_template
@link_rss_template ||= File.read(File.join('templates', 'blog', 'link.rss.html'))
end
def read_blog
blog = JSON.parse(File.read(blog_file))
@title = blog['title']
@ -166,7 +179,7 @@ class Blag
xml.description post[:rss_html]
xml.pubDate post[:rfc822]
xml.author post[:author]
xml.link post[:url]
xml.link post[:link] || post[:url]
xml.guid post[:url]
end
end

View file

@ -16,20 +16,17 @@
<nav id="index" class="hidden">
<ul>
{{#posts}}
<li><a href="{{filename}}">{{title}}</a> <span class="date">{{date}}</span></li>
<li><a href="{{url}}">{{title}}</a> <span class="date">{{date}}</span></li>
{{/posts}}
</ul>
</nav>
<div id="posts">
{{{article}}}
</div>
<p class="sep">&#x0f04;</p>
{{{article}}}
<div id="around">
{{#previous}}
<a id="prev" href="{{filename}}">&larr; {{title}}</a>
<a id="prev" href="{{url}}">&larr; {{title}}</a>
{{/previous}}
{{#next}}
<a id="next" href="{{filename}}">{{title}} &rarr;</a>
<a id="next" href="{{url}}">{{title}} &rarr;</a>
{{/next}}
<br style="clear:both">
</div>

View file

@ -0,0 +1,9 @@
<article>
<header>
<h1><a href="{{link}}">&rarr; {{title}}</a></h1>
<time>{{date}}</time>
</header>
{{{body}}}
</article>
<p><a href="{{url}}">&infin;</a></p>
<p class="sep">&#x0f04;</p>

View file

@ -0,0 +1,11 @@
<article>
{{#post}}
<header>
<h1><a href="{{link}}">&rarr; {{title}}</a></h1>
<time>{{date}}</time>
</header>
{{{html}}}
{{/post}}
</article>
<p><a href="{{#post}}{{url}}{{/post}}">&infin;</a></a>
<p class="sep">&#x0f04;</p>

View file

@ -13,16 +13,13 @@
<header>
<h1><a href="index.html">sjs' blog</a></h1>
</header>
<div id="posts">
{{{article}}}
</div>
<p class="sep">&#x0f04;</p>
{{{article}}}
<div id="around">
{{#previous}}
<a id="prev" href="{{filename}}">&larr; {{title}}</a>
<a id="prev" href="{{url}}">&larr; {{title}}</a>
{{/previous}}
{{#next}}
<a id="next" href="{{filename}}">{{title}} &rarr;</a>
<a id="next" href="{{url}}">{{title}} &rarr;</a>
{{/next}}
<br style="clear:both">
</div>

View file

@ -1,7 +1,8 @@
<article>
<header>
<h1><a href="{{filename}}">{{title}}</a></h1>
<h1><a href="{{url}}">{{title}}</a></h1>
<time>{{date}}</time>
</header>
{{{body}}}
</article>
<p class="sep">&#x0f04;</p>

View file

@ -1,12 +1,10 @@
<div id="posts">
<article>
{{#post}}
<header>
<h1><a href="{{filename}}">{{title}}</a></h1>
<time>{{date}}</time>
</header>
{{{html}}}
{{/post}}
</article>
</div>
<article>
{{#post}}
<header>
<h1><a href="{{url}}">{{title}}</a></h1>
<time>{{date}}</time>
</header>
{{{html}}}
{{/post}}
</article>
<p class="sep">&#x0f04;</p>