kommersbuch/src/songs.lua

38 lines
1.0 KiB
Lua

require "lfs"
local open = io.open
local function read_file(path)
local file = open(path, "rb") -- r read mode and b binary mode
if not file then return nil end
local content = file:read "*a" -- *a or *all reads the whole file
file:close()
return content
end
function string.ends(String,End)
return End=='' or string.sub(String,-string.len(End))==End
end
function find_songs(dir)
local files = {}
for file in lfs.dir(dir) do
table.insert(files, file)
end
table.sort(files)
for i = 1, #files do
local file = files[i]
if string.ends(file, ".ly") then
local ly_content = read_file(dir .. '/' .. file)
local ly_title = string.match(ly_content, 'title = "(.-)"')
tex.sprint('\\unchapter{' .. ly_title .. '}')
tex.sprint('\\lilypondfile{' .. dir .. '/' .. file .. '}')
tex.sprint('~\\\\')
tex.sprint('~\\\\')
tex.sprint('\\input{' .. dir .. '/' .. file:gsub("%.ly", ".tex") .. '}')
end
end
end