Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

truncate posts in index

  • Loading branch information...
commit 46ddfb10b22380db1c9369fd2141c33c1cbbf528 1 parent 54f2c7e
@flyerhzm authored
View
1  Gemfile
@@ -4,3 +4,4 @@ gem "jekyll", "0.10.0"
gem "jekyll_ext"
gem "liquid", "2.2.2"
gem "rdiscount"
+gem "nokogiri"
View
2  Gemfile.lock
@@ -14,6 +14,7 @@ GEM
liquid (2.2.2)
maruku (0.6.0)
syntax (>= 1.0.0)
+ nokogiri (1.5.5)
rdiscount (1.6.8)
syntax (1.0.0)
@@ -24,4 +25,5 @@ DEPENDENCIES
jekyll (= 0.10.0)
jekyll_ext
liquid (= 2.2.2)
+ nokogiri
rdiscount
View
2  _config.yml
@@ -1,4 +1,4 @@
-paginate: 5
+paginate: 10
pygments: true
markdown: rdiscount
permalink: /:year/:month/:day/:title/
View
67 _plugins/html_filters.rb
@@ -0,0 +1,67 @@
+# encoding: utf-8
+require 'rubygems'
+require 'nokogiri'
+
+module Liquid
+ module StandardFilters
+
+ def truncatehtml(raw, max_length = 15, continuation_string = "...")
+ doc = Nokogiri::HTML(Iconv.conv('UTF8//TRANSLIT//IGNORE', 'UTF8', raw))
+ current_length = 0;
+ deleting = false
+ to_delete = []
+
+ depth_first(doc.children.first) do |node|
+
+ if !deleting && node.class == Nokogiri::XML::Text
+ current_length += node.text.length
+ end
+
+ if deleting
+ to_delete << node
+ end
+
+ if !deleting && current_length > max_length
+ deleting = true
+
+ trim_to_length = current_length - max_length + 1
+ node.content = node.text[0..trim_to_length] + continuation_string
+ end
+ end
+
+ to_delete.map(&:remove)
+
+ doc.inner_html
+ end
+
+ private
+
+ def depth_first(root, &block)
+ parent = root.parent
+ sibling = root.next
+ first_child = root.children.first
+
+ yield(root)
+
+ if first_child
+ depth_first(first_child, &block)
+ else
+ if sibling
+ depth_first(sibling, &block)
+ else
+ # back up to the next sibling
+ n = parent
+ while n && n.next.nil? && n.name != "document"
+ n = n.parent
+ end
+
+ # To the sibling - otherwise, we're done!
+ if n && n.next
+ depth_first(n.next, &block)
+ end
+ end
+ end
+ end
+
+ end
+end
View
13 about.html
@@ -5,16 +5,11 @@
<div class="full">
<h1 class="entry-title">About</h1>
<div class="entry-content full-content">
- <p>My name is Richard Huang, a senior software engineer and a
- ruby/jruby freelancer who has a strong focus on code quality, server
- performance and scalability.</p>
- <p>I have been an active member of the open source community,
- contributing to many projects, please go to
+ <p>My name is Richard Huang, a senior software engineer and a ruby/jruby freelancer who has a strong focus on code quality, server performance and scalability.</p>
+ <p>I have been an active member of the open source community, contributing to many projects, please go to
<a href="/projects.html">projects page</a> to get a full list.</p>
- <p>I spoke at RubyKaigi 2011, Railsconf 2012 and several times at
- ruby/rails conferences in China.</p>
- <p>I also have experience to work on a large, high-traffic platform,
- > 200 million users and > 300k rpm.</p>
+ <p>I spoke at RubyKaigi 2011, Railsconf 2012 and several times at ruby/rails conferences in China.</p>
+ <p>I have experience to work on a large, high-traffic platform, > 200 million users and > 300k rpm.</p>
<p> Feel free to contact me <em>flyerhzm at gmail dot com</em></p>
<div class="clear"></div>
</div>
View
3  index.html
@@ -8,7 +8,8 @@ <h1 class="entry-title">
<a href="{{ post.url }}" title="{{ post.title }}" rel="bookmark">{{ post.title }}</a>
</h1>
<div class="entry-content full-content">
- {{ post.content }}
+ {{ post.content | truncatehtml: 500 }}
+ <a href="{{ post.url }}" class="read_more">read more</a>
<div class="clear"></div>
</div>
<p class="alt-font tight">
View
3  stylesheets/screen.css
@@ -604,6 +604,9 @@ h1.page-title {
padding-top: 9px;
width: 550px;
}
+.full .full-content .read_more {
+ float: right;
+}
.full .pages-link,
.page-numbers {
font-weight:bold;

2 comments on commit 46ddfb1

@jhkueh

Hi, I'm having difficulty making @MattHall's truncatehtml plugin work on my github page: jhkueh.github.com. Was wondering if you could give me some pointers. Is this all that you do to get it to work for you on your github pages? Or do you need a different host (Amazon S3) for it to work?

@flyerhzm
Owner

actually I deployed my homepage on linode, github pages don't support custom jekyll plugins well.

Please sign in to comment.
Something went wrong with that request. Please try again.