Last active
November 3, 2016 12:10
-
-
Save haffla/b74e0b8036a2dd3683ad6d99d960f914 to your computer and use it in GitHub Desktop.
Truncate only text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def truncateOnlyText t, p | |
r = %r{<[^<]+>[^<]+</[^<]+>} | |
h = %r{<[^<]+>} | |
k = /(?<=>).+(?=<)/ | |
z = [] | |
s = t.gsub(h, '')[0,p] | |
t.scan(r) do |c| | |
z << [c, $~.offset(0)[0]] | |
end | |
p z | |
z.take_while{ |m| s.index(m[0].match(k)[0]) }.each do |m| | |
s.sub!(m[0].match(k)[0], m[0]) | |
end | |
s | |
end | |
p truncateOnlyText "Welcome to the <a href=\"/hotels\">hotel</a> California.", 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment