-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTufte quote.rb
executable file
·68 lines (58 loc) · 1.65 KB
/
Tufte quote.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/bin/env ruby
# encoding: UTF-8
require 'uri'
require 'time'
lines = ARGF.readlines
quote_lines,rest = lines.partition{|line| line.start_with?( ">" ) }
href,rest =
rest.partition{|line|
if line.ascii_only? && URI.regexp =~ line
begin
URI(line) # if it's a valid URI
true # it will reach here
rescue URI::InvalidURIError => e
false # if not, return false
end
else
false
end
}
footer,cite,date = *rest
href = href.empty? ?
nil :
href.first
if (date.nil? || date.empty?) &! cite.nil?
begin
time = Time.parse cite
# will fail here if cite is not a date
# and the following 2 lines won't be run
date = cite
cite = nil
rescue ArgumentError => e
# do nothing
end
end
# To make sure
cite.force_encoding("ASCII-8BIT") unless cite.nil?
footer.force_encoding("ASCII-8BIT") unless footer.nil?
quote_lines.map!{|line| line.force_encoding("ASCII-8BIT") }
.map!{|line| line.sub /^\>\s*/, "" }
quote =
quote_lines.map{|para| para.gsub(/\s+/, " ") }
.map{|para| "<p>#{para}</p>" }
.join
if cite || href
cite = %Q!<cite>#{cite}</cite>!.gsub(%Q!\n!,'') unless cite.nil? || cite.empty?
cite_class = %Q! cite="#{href}"!.gsub(%Q!\n!,'') if href
if href
footer_text = %Q!<a href="#{href}">#{[footer,cite,date].reject{|x| x.nil? || x.empty?}.join(', ')}</a>!.gsub(%Q!\n!,'')
else
footer_text = "#{footer}#{cite}#{date}"
end
else
footer_text = %Q!#{[footer,date].reject{|x| x.nil? || x.empty?}.join(', ').gsub(%Q!\n!,'')}!
end
print <<TEMPLATE
<blockquote#{cite_class}>#{quote}<footer>#{footer_text}</footer>
</blockquote>
TEMPLATE