Browse code

Improve handling of meta description

Frederic Hemberger authored on 21/10/2011 at 13:00:32
Showing 2 changed files
... ...
@@ -8,7 +8,7 @@
8 8
   <meta name="author" content="{{ site.author }}">
9 9
 
10 10
   {% capture description %}{% if page.description %}{{ page.description }}{% else %}{{ content | raw_content }}{% endif %}{% endcapture %}
11
-  <meta name="description" content="{{ description | strip_newlines | strip_html | truncate:150 }}">
11
+  <meta name="description" content="{{ description | strip_html | condense_spaces | truncate:150 }}">
12 12
   {% if page.keywords %}<meta name="keywords" content="{{ page.keywords }}">{% endif %}
13 13
 
14 14
   <!-- http://t.co/dKP3o1e -->
... ...
@@ -79,6 +79,33 @@ module OctopressLiquidFilters
79 79
     end
80 80
   end
81 81
 
82
+  # Improved version of Liquid's truncate:
83
+  # - Doesn't cut in the middle of a word.
84
+  # - Uses typographically correct ellipsis (…) insted of '...'
85
+  def truncate(input, length)
86
+    if input.length > length && input[0..(length-1)] =~ /(.+)\b.+$/im
87
+      $1.strip + ' &hellip;'
88
+    else
89
+      input
90
+    end
91
+  end
92
+
93
+  # Improved version of Liquid's truncatewords:
94
+  # - Uses typographically correct ellipsis (…) insted of '...'
95
+  def truncatewords(input, length)
96
+    truncate = input.split(' ')
97
+    if truncate.length > length
98
+      truncate[0..length-1].join(' ').strip + ' &hellip;'
99
+    else
100
+      input
101
+    end
102
+  end
103
+
104
+  # Condenses multiple spaces and tabs into a single space
105
+  def condense_spaces(input)
106
+    input.gsub(/\s{2,}/, ' ')
107
+  end
108
+
82 109
   # Removes trailing forward slash from a string for easily appending url segments
83 110
   def strip_slash(input)
84 111
     if input =~ /(.+)\/$|^\/$/