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 } .date { float: right }
div#posts { width: 80% article { width: 80%
; min-width: 400px ; min-width: 400px
; max-width: 680px ; max-width: 680px
; margin: 4em auto 2em ; margin: 2em auto 1em
; padding: 0 5% ; font-size: 1.2em
; font-size: 1.2em ; line-height: 1.4em
; line-height: 1.4em ; color: #222
}
article { color: #222
; padding-bottom: 1em
} }
article:last-child { padding-bottom: 0
; margin-bottom: 1em
}
article h1 { text-align: left article h1 { text-align: left
; font-size: 2em ; font-size: 2em
; font-weight: normal ; font-weight: normal

View file

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

39
blog.rb
View file

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

View file

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

View file

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

View file

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