-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rakefile
54 lines (43 loc) · 1.4 KB
/
Rakefile
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
# frozen_string_literal: true
require 'digest'
require 'yaml'
Poem = Struct.new(:题目, :朝代, :作者, :诗词, :脚注, :诗注, :小传, :标签) do
def self.from_yaml_file(file)
yaml = YAML.safe_load(File.read(file))
new(
yaml['题目'], yaml['朝代'], yaml['作者'],
yaml['诗词'] || [], yaml['脚注'] || [], yaml['诗注'], yaml['小传'], yaml['标签'] || []
)
end
def initialize(...)
super(...)
preprocess_footnotes
self.诗词 = 诗词.join('<br>')
self.脚注 = "<ol><li>#{脚注.join('</li><li>')}</li></ol>" unless 脚注.empty?
self.标签 = 标签.join(' ')
end
def preprocess_footnotes
pattern = /([[:alpha:]])\(([a-zāáǎàēéěèīíǐìōóǒòūúǔùüǖǘǚǜ]{1,5})\)/
self.脚注 = 脚注.map { _1.gsub(pattern, '<ruby>\1<rp>(</rp><rt>\2</rt><rp>)</rp></ruby>') }
end
def to_text_line
[
题目, 朝代, 作者, 诗词, 脚注, 诗注, 小传, 标签,
Digest::MD5.hexdigest([题目, 朝代, 作者].join("\t"))
].join("\t")
end
end
task :default do
fout = File.open('chinese-poetry.txt', 'w')
# Add headers
fout.puts <<~END_OF_DOC
#separator:Tab
#html:true
#columns:#{%w[题目 朝代 作者 诗词 脚注 诗注 小传].join("\t")}
#tags column:8
#guid column:9
END_OF_DOC
# Add poems
Dir['*/*.yml'].each { fout.puts Poem.from_yaml_file(_1).to_text_line }
fout.close
end