m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/lib/util.rb
blob: 5509aadc866e494bbd342251bdd6e25a3072dd44 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
SongsDir = 'songs'
SrcDir = 'src'
BuildDir = 'public'
TmpDir = 'tmp'
TemplatesDir = 'templates'

def song filename
  File.join SongsDir, filename
end

def build filename
  File.join BuildDir, filename
end

def tmp filename
  File.join TmpDir, filename
end

def src filename
  File.join SrcDir, filename
end

def song_lyrics song_id
  song "#{song_id}.txt"
end

def song_ly song_id
  song "#{song_id}.ly"
end

def song_meta song_id
  song "#{song_id}.yaml"
end

def song_partial_tex song_id
  song "#{song_id}-partial.tex"
end

def song_tex song_id
  song "#{song_id}.tex"
end

def song_sheet_pdf song_id
  song "#{song_id}.cropped.pdf"
end

def song_tmp_svg song_id
  song "#{song_id}.cropped.svg"
end

def song_pdf song_id
  song "#{song_id}.pdf"
end

def song_sheet_svg song_id
  song "#{song_id}.svg"
end

def song_svg song_id
  song "#{song_id}.svg"
end

def song_html song_id
  song "#{song_id}.html"
end

def template filename
  ERB.new(File.read(filename), trim_mode: '-')
end

def template_file filename
  File.join TemplatesDir, filename
end

def tex_template name
  template(template_file "#{name}.tex.erb")
end

def html_template name
  template(template_file "#{name}.html.erb")
end

def page_template
  html_template 'template'
end

def path_to asset_id
  P.path_to asset_id
end

def normalize title
  title.downcase.gsub /[[:punct:]]|[[:space:]]/, ''
end

def indexify title
  alphabet = "aąbcćdeęfghijklłmnńoóprsśtuwyzźż".chars
  normalized = normalize title
  normalized.chars.map do |c|
    index = alphabet.find_index c
    if index.nil?
      raise "Letter not in alphabet: #{c}"
    end

    index
  end
end

def polish_compare a, b
  indices_a = indexify a
  indices_b = indexify b
  indices_a <=> indices_b
end