This repository was archived by the owner on Nov 6, 2020. It is now read-only.
forked from aucor/jekyll-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyphenate.rb
52 lines (39 loc) · 1.58 KB
/
hyphenate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Adapted from [Jekyll plugins](https://github.com/aucor/jekyll-plugins) by Aucor
# https://github.com/aucor/jekyll-plugins/blob/master/hyphenate.rb
require 'nokogiri'
require 'text/hyphen'
module Jekyll
module HyphenateFilter
def hyphenate(content)
# Initialize Hyphen
# (you can change the language as you wish, we're from Finland ;)
# note: english is en_us or en_uk not just en
hh = Text::Hyphen.new(:language => 'en_uk', :left => 2, :right => 2)
# make sure we find at least one text node
content = '<span>'+content+'</span>'
# Parse html with Nokogiri
fragment = Nokogiri::HTML::DocumentFragment.parse(content)
# Loop through every text node
fragment.search('*//text()').each do |elem|
content = elem.content
# Loop through every word
elem.content.split.each do |w|
# Replace original word with a hyphenated one
# unless it is the last word in a paragraph
if w != elem.content.split.last
# hack: nokogiri circumscribes '&' with '&'
content = content.gsub(w, hh.visualize(w, '#shy;'))
end
end
# Replace original paragraph with a hyphenated one
elem.content = content
end
# reintroduce '­' instead of '#shy;'
retval = fragment.inner_html
retval = retval.gsub('#shy;', '­')
# Return hyphenated html
retval
end
end
end
Liquid::Template.register_filter(Jekyll::HyphenateFilter)