-
Notifications
You must be signed in to change notification settings - Fork 3
/
Rakefile
95 lines (76 loc) · 2.25 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
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
85
86
87
88
89
90
91
92
93
94
95
require 'bundler/setup'
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task 'build-opal' do
require 'opal'
require 'erb'
require 'json'
b = Opal::Builder.new
b.append_paths __dir__ + '/lib'
b.append_paths __dir__ + '/opal'
b.use_gem 'htmlentities'
build = b.build('latexmath-opal').to_s
# This is a hack intended for ExecJS to work properly.
build = build.gsub('Object freezing is not supported by Opal', '')
FileUtils.mkdir_p 'dist'
File.write('dist/latexmath.js', build)
end
task 'spec-opal' => 'build-opal' do
ENV['TEST_OPAL'] = '1'
Rake::Task['spec'].execute
ENV.delete 'TEST_OPAL'
end
def read_symbols(input)
@symbols = {}
File.readlines(input).each do |line|
next if line.start_with?('#')
columns = line.chop.split('^')
_unicode = columns[0]
latex = columns[2]
unicode_math = columns[3]
@symbols[latex] = _unicode if latex && [email protected]?(latex)
@symbols[unicode_math] = _unicode if unicode_math && [email protected]?(unicode_math)
matches = columns[-1].match(/=\s+(\\[^,^ ]+),?/)
if matches
@symbols[matches[1]] = _unicode unless @symbols[matches[1]]
end
end
@symbols
end
directory 'lib/latexmath/constants'
file 'lib/latexmath/constants/symbols.rb' => [
'lib/latexmath/constants',
'lib/unimathsymbols.txt'
] do |file|
symbols = read_symbols(file.prerequisites.last)
File.open(file.name, "w+") do |f|
f.puts <<~HERE
module Latexmath
module Constants
SYMBOLS = {
HERE
@symbols.each_pair do |k,v|
f.puts(" '#{k.to_s}' => '#{v}',")
end
f.puts <<~HERE
}.freeze
end
end
HERE
end
end
Rake::Task['build'].enhance ['lib/latexmath/constants/symbols.rb']
Rake::Task[:spec].enhance ['lib/latexmath/constants/symbols.rb']
desc 'Generate and write MathML fixtures'
task 'create-mathml-fixtures' do
require_relative 'lib/latexmath'
records = Dir.glob('spec/fixtures/*/*.tex')
records.each do |file|
tex = File.read("./#{file}")
tokens = Latexmath::Tokenizer.new(tex).tokenize
aggr = Latexmath::Aggregator.new(tokens).aggregate
File.write("./#{file.chomp('.tex')}.html", Latexmath::Converter.new(aggr).convert)
end
end
task default: [:spec, 'spec-opal']