Skip to content

Commit

Permalink
Factor out common code from lilypond_fragment and lilypond_file
Browse files Browse the repository at this point in the history
I'm sure that can be brought even further, but I don't fully understand
the implications of the difference with fullpage or not.
Maybe there's no reason not to give the fullpage option to encoded
fragments as well. (what about a fragment that generates five exercises by repeating a permutated pattern? This could very well be included inline and span multiple pages)
  • Loading branch information
uliska committed Jan 15, 2018
1 parent f476a3b commit 923a28a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lyluatex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,21 @@ function flattenContent(original_content)
return content
end
function extract_size_arguments(line_width, staffsize)
line_width = {['n'] = line_width:match('%d+'), ['u'] = line_width:match('%a+')}
staffsize = calc_staffsize(staffsize)
return line_width, staffsize
end
function hash_output_filename(ly_code,line_width, staffsize)
return TMP..'/'..string.gsub(md5.sumhexa(flattenContent(ly_code))..'-'..staffsize..'-'..line_width.n..line_width.u, '%.', '-')
end
function lilypond_fragment(ly_code, line_width, staffsize)
N = N + 1
line_width = {['n'] = line_width:match('%d+'), ['u'] = line_width:match('%a+')}
staffsize = calc_staffsize(staffsize)
line_width, staffsize = extract_size_arguments(line_width, staffsize)
ly_code = ly_code:gsub('\\par ', '\n'):gsub('\\([^%s]*) %-([^%s])', '\\%1-%2')
local output =
TMP..'/'..string.gsub(md5.sumhexa(flattenContent(ly_code))..'-'..staffsize..'-'..line_width.n..line_width.u, '%.', '-')
local output = hash_output_filename(ly_code, line_width, staffsize)
if not lfs.isfile(output..'-systems.tex') then
run_lilypond(lilypond_fragment_header(staffsize, line_width)..'\n'..ly_code, output, true)
end
Expand All @@ -59,16 +66,15 @@ end
function lilypond_file(input_file, currfiledir, line_width, staffsize, fullpage)
line_width = {['n'] = line_width:match('%d+'), ['u'] = line_width:match('%a+')}
staffsize = calc_staffsize(staffsize)
line_width, staffsize = extract_size_arguments(line_width, staffsize)
filename = splitext(input_file, 'ly')
input_file = currfiledir..filename..'.ly'
if not lfs.isfile(input_file) then input_file = kpse.find_file(filename..'.ly') end
if not lfs.isfile(input_file) then err("File %s.ly doesn't exist.", filename) end
local i = io.open(input_file, 'r')
ly_code = i:read('*a')
i:close()
local output = TMP..'/'..string.gsub(md5.sumhexa(flattenContent(ly_code))..'-'..staffsize..'-'..line_width.n..line_width.u..'-', '%.', '-')
local output = hash_output_filename(ly_code, line_width, staffsize)
if fullpage then output = output..'-fullpage' end
if not lfs.isfile(output..'-systems.tex') then
if fullpage then
Expand Down

0 comments on commit 923a28a

Please sign in to comment.