-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2latex.rb
executable file
·84 lines (64 loc) · 1.73 KB
/
csv2latex.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/ruby
# encoding: UTF-8
require 'csv'
require 'tmpdir'
require 'securerandom'
require './transcriber'
input, output = ARGV
chapter = '0-9'
text = ''
MAP = {
'ɖ'=>'\:d',
'ɣ'=>'G',
'ʈ'=>'\:t',
'ɭ'=>'\:l',
'ʰ'=>'\super h'
}
images = []
#text = File.read(input).gsub(/\\"/,'""')
CSV.foreach(input) do |data|
word, transcription, speech = data
if(word == nil || transcription == nil || speech == nil)
next
end
word.downcase!
word.strip!
transcription.downcase!
transcription.strip!
speech.downcase!
speech.strip!
if chapter != word[0] && word[0] >= 'a'
chapter = word[0]
#text += "\\chapter*{#{chapter.upcase}}\n"
end
ipa = ''
transcription.each_char do|i|
if MAP[i] != nil
ipa += MAP[i]
else
ipa += i
end
end
texfile = Dir.tmpdir + File::SEPARATOR + SecureRandom.hex + '.tex'
text += "\\begin{minipage}{\\textwidth}\n"
text += "\\dict{#{word}}{#{speech}}{#{ipa}}\n"
imgfile = Dir.tmpdir + File::SEPARATOR + SecureRandom.hex + 'transcript.png'
if Transcriber.transcribe(transcription.gsub('ʰ',''), imgfile)
text += "\\newline\n"
text += "\\begingroup\n"
text += " \\centering\n"
text += " \\includegraphics[height=0.7cm,width=\\linewidth,keepaspectratio]{#{imgfile}}\n"
text += "\\endgroup\n"
images.push imgfile
else
puts "#{word} has an invalid phoneme in #{transcription}"
end
text += "\\end{minipage}\n"
end
texfile = Dir.tmpdir + File::SEPARATOR + SecureRandom.hex + '.tex'
texout = File.read("layout.tex").sub("%REPLACE_ME%", text)
File.write(texfile, texout)
`pdflatex --jobname=#{output} --interaction nonstopmode -halt-on-error -file-line-error #{texfile}`
images.each{|img|
File.delete img
}