| 1 |
#! /usr/bin/env texlua |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
VERSION = "0.9.3" |
| 6 |
FILE_SUFFIX = "_chtr" |
| 7 |
PMX_CMD = "pmxab" |
| 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 |
function usage() |
| 46 |
print("Usage: [texlua] pmxchords.lua basename[.pmx] ") |
| 47 |
print(" 1. transpose chords into new file basename" .. FILE_SUFFIX .. ".pmx") |
| 48 |
print(" 2. call pmxab on transposed file basename" .. FILE_SUFFIX .. ".pmx") |
| 49 |
print(" 3. rename basename" .. FILE_SUFFIX .. ".tex to basename.tex") |
| 50 |
print("options: -v version") |
| 51 |
print(" -h help") |
| 52 |
print(" -s stop after fist step. e.g. generate only transposed basename" .. FILE_SUFFIX .. ".pmx file") |
| 53 |
end |
| 54 |
|
| 55 |
function whoami() |
| 56 |
print("This is pmxchords.lua version " .. VERSION) |
| 57 |
end |
| 58 |
|
| 59 |
whoami() |
| 60 |
if #arg == 0 then |
| 61 |
usage() |
| 62 |
os.exit(0) |
| 63 |
end |
| 64 |
|
| 65 |
kpse.set_program_name("luatex") |
| 66 |
require"ChordsTr" |
| 67 |
|
| 68 |
function parseInputSignature(line) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
local res = string.match(line, "^%s*[0-9]+%s+[0-9]+%s+[0-9]+%s+[0-9]+%s+[0-9]+%s+[0-9]+%s+[0-9]+%s+([%+%-]?[0-9]+)%s*$") |
| 74 |
return tonumber(res) |
| 75 |
end |
| 76 |
|
| 77 |
function parseOutputSignature(line) |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
local res = string.match(line, "^K[%+%-]?[0-9]+([%+%-]?[0-9]+)") |
| 83 |
return tonumber(res) |
| 84 |
end |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
function lineWithoutComment(line) |
| 91 |
local i = string.find(line, "%%") |
| 92 |
if (i == nil) then |
| 93 |
return line |
| 94 |
end |
| 95 |
return line:sub(0, i - 1) |
| 96 |
end |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
function checkChordsAlreadyStarted(line) |
| 108 |
local i = string.find(lineWithoutComment(line), ChordsTr.CHORDS_BEGIN) |
| 109 |
if (i ~= nil) then |
| 110 |
return true |
| 111 |
end |
| 112 |
return false; |
| 113 |
end |
| 114 |
|
| 115 |
function handleErr(msg) |
| 116 |
io.stderr:write(msg .. "\n") |
| 117 |
os.exit(2) |
| 118 |
end |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
function getBaseFileName(fileName) |
| 124 |
if fileName ~= "" and string.sub(fileName, -4, -1) == ".pmx" then |
| 125 |
return string.sub(fileName, 1, -5) |
| 126 |
else |
| 127 |
return fileName |
| 128 |
end |
| 129 |
end |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
function makeChordsTransposition(baseName) |
| 138 |
local inputFileName = baseName .. ".pmx" |
| 139 |
local inputFile = io.open(inputFileName, "r") |
| 140 |
if (inputFile == nil) then |
| 141 |
handleErr("File does not exist: " .. inputFileName) |
| 142 |
end |
| 143 |
|
| 144 |
local outputBaseName = baseName .. FILE_SUFFIX |
| 145 |
local outputFileName = outputBaseName .. ".pmx" |
| 146 |
local outputFile = io.open(outputFileName, "w") |
| 147 |
if (outputFile == nil) then |
| 148 |
handleErr("Can not create temporary file: " .. outputFileName) |
| 149 |
end |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
local iSig = nil |
| 157 |
local chTr = nil |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
local chordsAlreadyStarted = false; |
| 162 |
|
| 163 |
for line in inputFile:lines() do |
| 164 |
if (chTr ~= nil) then |
| 165 |
outputFile:write(chTr:lineTranspose(line) .. "\n") |
| 166 |
else |
| 167 |
outputFile:write(line .. "\n") |
| 168 |
end |
| 169 |
if (iSig == nil) then |
| 170 |
iSig = parseInputSignature(line) |
| 171 |
if (iSig ~= nil) then |
| 172 |
io.stderr:write("Chords: input signature detected: " .. iSig .. "\n") |
| 173 |
end |
| 174 |
elseif (chTr == nil and not chordsAlreadyStarted) then |
| 175 |
local oSig = parseOutputSignature(line) |
| 176 |
if (oSig ~= nil) then |
| 177 |
io.stderr:write("Chords: output signature detected: " .. oSig .. "\n") |
| 178 |
chTr = ChordsTr(iSig, oSig) |
| 179 |
elseif (checkChordsAlreadyStarted(line)) then |
| 180 |
io.stderr:write("Chords: notes already started. Chords transposition is omitted.\n") |
| 181 |
chordsAlreadyStarted = true; |
| 182 |
end |
| 183 |
end |
| 184 |
end |
| 185 |
inputFile:close() |
| 186 |
outputFile:close() |
| 187 |
|
| 188 |
return outputBaseName |
| 189 |
end |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
function pmxabProcess(baseName, outputBaseName) |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
os.execute(PMX_CMD .. " " .. outputBaseName) |
| 201 |
|
| 202 |
local pmxaerr = io.open("pmxaerr.dat", "r") |
| 203 |
if (not pmxaerr) then |
| 204 |
handleErr("No log file.") |
| 205 |
end |
| 206 |
local linebuf = pmxaerr:read() |
| 207 |
local err = tonumber(linebuf) |
| 208 |
pmxaerr:close() |
| 209 |
if (err ~= 0) then |
| 210 |
handleErr("PMX process fail! " .. outputBaseName .. ".pmx") |
| 211 |
end |
| 212 |
|
| 213 |
os.rename(outputBaseName .. ".tex", baseName .. ".tex") |
| 214 |
|
| 215 |
|
| 216 |
os.remove(outputBaseName .. ".pmx") |
| 217 |
os.remove(outputBaseName .. ".pml") |
| 218 |
end |
| 219 |
|
| 220 |
narg = 1 |
| 221 |
onlyTranspose = false |
| 222 |
|
| 223 |
repeat |
| 224 |
local this_arg = arg[narg] |
| 225 |
if this_arg == "-v" then |
| 226 |
os.exit(0) |
| 227 |
elseif this_arg == "-h" then |
| 228 |
usage() |
| 229 |
os.exit(0) |
| 230 |
elseif this_arg == "-s" then |
| 231 |
onlyTranspose = true |
| 232 |
else |
| 233 |
local baseName = getBaseFileName(this_arg) |
| 234 |
local outputBaseName = makeChordsTransposition(baseName) |
| 235 |
|
| 236 |
if (onlyTranspose) then |
| 237 |
os.exit(0) |
| 238 |
end |
| 239 |
pmxabProcess(baseName, outputBaseName) |
| 240 |
end |
| 241 |
|
| 242 |
narg = narg + 1 |
| 243 |
until narg > #arg |
| 244 |
|