kommersbuch/src/songs.lua

38 lines
1.0 KiB
Lua
Raw Normal View History

2017-11-08 10:07:22 +00:00
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)
2020-05-16 09:33:14 +00:00
local files = {}
2017-11-08 10:07:22 +00:00
for file in lfs.dir(dir) do
2020-05-16 09:33:14 +00:00
table.insert(files, file)
end
table.sort(files)
for i = 1, #files do
local file = files[i]
2017-11-08 10:07:22 +00:00
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 .. '}')
2020-07-16 12:34:05 +00:00
tex.sprint('\\lilypondfile{' .. dir .. '/' .. file .. '}')
2017-11-08 10:07:22 +00:00
tex.sprint('~\\\\')
tex.sprint('~\\\\')
tex.sprint('\\input{' .. dir .. '/' .. file:gsub("%.ly", ".tex") .. '}')
end
end
end