| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4 |
# % |
| 5 |
# This file is part of the 'lilyglyphs' LaTeX package. % |
| 6 |
# ========== % |
| 7 |
# % |
| 8 |
# https://github.com/openlilylib/lilyglyphs % |
| 9 |
# http://www.openlilylib.org/lilyglyphs % |
| 10 |
# % |
| 11 |
# Copyright 2012-2020 Urs Liska and others, [email protected] % |
| 12 |
# % |
| 13 |
# 'lilyglyphs' is free software: you can redistribute it and/or modify % |
| 14 |
# it under the terms of the LaTeX Project Public License, either % |
| 15 |
# version 1.3 of this license or (at your option) any later version. % |
| 16 |
# You may find the latest version of this license at % |
| 17 |
# http://www.latex-project.org/lppl.txt % |
| 18 |
# more information on % |
| 19 |
# http://latex-project.org/lppl/ % |
| 20 |
# and version 1.3 or later is part of all distributions of LaTeX % |
| 21 |
# version 2005/12/01 or later. % |
| 22 |
# % |
| 23 |
# This work has the LPPL maintenance status 'maintained'. % |
| 24 |
# The Current Maintainer of this work is Urs Liska (see above). % |
| 25 |
# % |
| 26 |
# This work consists of the files listed in the file 'manifest.txt' % |
| 27 |
# which can be found in the 'license' directory. % |
| 28 |
# % |
| 29 |
# This program is distributed in the hope that it will be useful, % |
| 30 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of % |
| 31 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. % |
| 32 |
# % |
| 33 |
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 34 |
|
| 35 |
# ######################################################################## |
| 36 |
# # |
| 37 |
# lilyglyphs_common.py # |
| 38 |
# # |
| 39 |
# Common functionality for the Python scripts in lilyglyphs # |
| 40 |
# # |
| 41 |
# ######################################################################## |
| 42 |
|
| 43 |
import os, sys, datetime, subprocess, argparse |
| 44 |
|
| 45 |
# ################ |
| 46 |
# Global variables |
| 47 |
|
| 48 |
definitions_file = [] |
| 49 |
version_string = '0.2.2' |
| 50 |
|
| 51 |
# ###################### |
| 52 |
# Common CL arguments |
| 53 |
common_arguments = argparse.ArgumentParser(add_help=False) |
| 54 |
common_arguments.add_argument('-v', '--version', |
| 55 |
action='version', |
| 56 |
version='%(prog)s ' + version_string) |
| 57 |
|
| 58 |
def is_file(filename): |
| 59 |
if os.path.exists(filename): |
| 60 |
return filename |
| 61 |
else: |
| 62 |
msg = "file %s not found" % filename |
| 63 |
raise argparse.ArgumentTypeError(msg) |
| 64 |
|
| 65 |
|
| 66 |
# ########### |
| 67 |
# Directories |
| 68 |
dir_defs = 'definitions' |
| 69 |
dir_lysrc = 'generated_src' |
| 70 |
dir_pdfs = 'pdfs' |
| 71 |
dir_cmd = 'generated_cmd' |
| 72 |
|
| 73 |
# LilyPond commands |
| 74 |
in_cmds = {} |
| 75 |
# LilyPond source files (corresponds to in_cmds) |
| 76 |
# stores basenames without path and extension |
| 77 |
lily_files = [] |
| 78 |
# LaTeX commands |
| 79 |
latex_cmds = {} |
| 80 |
|
| 81 |
# ####### |
| 82 |
# Strings |
| 83 |
|
| 84 |
lilyglyphs_copyright_string = """ |
| 85 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 86 |
% % |
| 87 |
% This file is part of the 'lilyglyphs' LaTeX package. % |
| 88 |
% ========== % |
| 89 |
% % |
| 90 |
% https://github.com/openlilylib/lilyglyphs % |
| 91 |
% http://www.openlilylib.org/lilyglyphs % |
| 92 |
% % |
| 93 |
% Copyright 2012-2020 Urs Liska and others, [email protected] % |
| 94 |
% % |
| 95 |
% 'lilyglyphs' is free software: you can redistribute it and/or modify % |
| 96 |
% it under the terms of the LaTeX Project Public License, either % |
| 97 |
% version 1.3 of this license or (at your option) any later version. % |
| 98 |
% You may find the latest version of this license at % |
| 99 |
% http://www.latex-project.org/lppl.txt % |
| 100 |
% more information on % |
| 101 |
% http://latex-project.org/lppl/ % |
| 102 |
% and version 1.3 or later is part of all distributions of LaTeX % |
| 103 |
% version 2005/12/01 or later. % |
| 104 |
% % |
| 105 |
% This work has the LPPL maintenance status 'maintained'. % |
| 106 |
% The Current Maintainer of this work is Urs Liska (see above). % |
| 107 |
% % |
| 108 |
% This work consists of the files listed in the file 'manifest.txt' % |
| 109 |
% which can be found in the 'license' directory. % |
| 110 |
% % |
| 111 |
% This program is distributed in the hope that it will be useful, % |
| 112 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of % |
| 113 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. % |
| 114 |
% % |
| 115 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 116 |
""" |
| 117 |
|
| 118 |
latexfile_start_comment = """ |
| 119 |
% This file contains definitions for the new commands |
| 120 |
% along with test code for them. |
| 121 |
% You can test the commands in the context of continuous text |
| 122 |
% and adjust their design time options. |
| 123 |
% Afterwards you should manually move the commands to |
| 124 |
% the appropriate .inp files, |
| 125 |
% because this file will be overwritten by the next run |
| 126 |
% of SCRIPT_NAME! |
| 127 |
% If you want to keep this file for reference |
| 128 |
% you should save it with a new name. |
| 129 |
% |
| 130 |
% There also is a table containing entries for use in the lilyglyph manual. |
| 131 |
% You can either copy the whole table to the appropriate |
| 132 |
% place in lilyglyphs.tex or just copy individual table rows. |
| 133 |
|
| 134 |
\\documentclass{scrartcl} |
| 135 |
\\usepackage{lilyglyphsStyle} |
| 136 |
|
| 137 |
%%%%%%%%%%%%%%%%%%%%%%%%% |
| 138 |
% new command definitions |
| 139 |
|
| 140 |
""" |
| 141 |
|
| 142 |
latexfile_begin_document = """ |
| 143 |
|
| 144 |
\\begin{document} |
| 145 |
|
| 146 |
%%%%%%%%%%%%% |
| 147 |
% Text output |
| 148 |
|
| 149 |
\\section*{New \\lilyglyphs{} commands} |
| 150 |
""" |
| 151 |
|
| 152 |
latexfile_reftable = """ |
| 153 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 154 |
% Reference table to be used in the manual |
| 155 |
% (use complete or single lines) |
| 156 |
|
| 157 |
\\begin{reftable}{New commands}{newcommands} |
| 158 |
""" |
| 159 |
|
| 160 |
latexfile_testcode = """\\end{reftable} |
| 161 |
|
| 162 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 163 |
% Test code for fine-tuning the new commands |
| 164 |
""" |
| 165 |
|
| 166 |
# ############## |
| 167 |
# Code templates |
| 168 |
|
| 169 |
# template string to build the test code for the commands |
| 170 |
# 'CMD' will be replaced by the actual command_name |
| 171 |
testcode_template = """ |
| 172 |
|
| 173 |
\\noindent\\textbf{\\textsf{Continuous text for} \\cmd{CMD}:}\\\\ |
| 174 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, |
| 175 |
sed \\CMD{} do eiusmod tempor incididunt ut labore et dolore magna aliqua \\CMD.\\\\ |
| 176 |
\\CMD{} Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip |
| 177 |
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
| 178 |
cillum dolore eu fugiat nulla pariatur \\CMD. |
| 179 |
\\CMD{} Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
| 180 |
|
| 181 |
\\bigskip |
| 182 |
""" |
| 183 |
|
| 184 |
# Default values for optional argument in generated commands |
| 185 |
DEF_SCALE = '1' |
| 186 |
DEF_RAISE = '0' |
| 187 |
|
| 188 |
# template strings to build the command from |
| 189 |
# 'CMD' will be replaced by the actual command_name |
| 190 |
# 'ELEM' will be replaced by the actual content element to be rendered |
| 191 |
|
| 192 |
cmd_templates = {} |
| 193 |
cmd_templates['image'] = """\\newcommand*{\\CMD}[1][]{% |
| 194 |
\\setkeys{lilyDesignOptions}{scale=SCALE,raise=RAISE}% |
| 195 |
\\lilyPrintImage[#1]{ELEM}% |
| 196 |
} |
| 197 |
|
| 198 |
""" |
| 199 |
|
| 200 |
cmd_templates['glyphname'] = """\\newcommand*{\\CMD}[1][]{% |
| 201 |
\\setkeys{lilyDesignOptions}{scale=SCALE,raise=RAISE}% |
| 202 |
\\lilyPrint[#1]{\\lilyGetGlyph{ELEM}}% |
| 203 |
} |
| 204 |
|
| 205 |
""" |
| 206 |
|
| 207 |
cmd_templates['number'] = """\\newcommand*{\\CMD}[1][]{% |
| 208 |
\\setkeys{lilyDesignOptions}{scale=SCALE,raise=RAISE}% |
| 209 |
\\lilyPrint[#1]{\\lilyGetGlyphByNumber{ELEM}}% |
| 210 |
} |
| 211 |
|
| 212 |
""" |
| 213 |
|
| 214 |
cmd_templates['dynamics'] = """\\newcommand{\\CMD}[1][]{% |
| 215 |
\\mbox{% |
| 216 |
\\lilyDynamics[#1]{ELEM}% |
| 217 |
}% |
| 218 |
} |
| 219 |
|
| 220 |
""" |
| 221 |
|
| 222 |
cmd_templates['text'] = """\\newcommand{\\CMD}[1][]{% |
| 223 |
\\setkeys{lilyDesignOptions}{scale=SCALE,raise=RAISE}% |
| 224 |
\\mbox{% |
| 225 |
\\lilyText[#1]{ELEM}% |
| 226 |
}% |
| 227 |
} |
| 228 |
|
| 229 |
""" |
| 230 |
|
| 231 |
def cleanup_lily_files(): |
| 232 |
"""Removes unneccessary files from LilyPond compilation, |
| 233 |
rename and remove the preview PDF files to the right directory.""" |
| 234 |
|
| 235 |
print('Clean up directories') |
| 236 |
|
| 237 |
# iterate through dir_lysrc |
| 238 |
os.chdir(dir_lysrc) |
| 239 |
for entry in os.listdir('.'): |
| 240 |
if os.path.isfile(entry): |
| 241 |
name, extension = os.path.splitext(entry) |
| 242 |
#remove unnecessary files |
| 243 |
if not extension in ['.pdf', '.ly']: |
| 244 |
os.remove(entry) |
| 245 |
if extension == '.pdf': |
| 246 |
# remove full-page pdf |
| 247 |
if '.preview' in name: |
| 248 |
newfile = entry.replace('.preview.', '.') |
| 249 |
newfile = os.path.join('..', dir_pdfs, newfile) |
| 250 |
# rename/move small 'preview' pdf |
| 251 |
os.rename(entry, newfile) |
| 252 |
else: |
| 253 |
os.remove(entry) |
| 254 |
os.chdir('..') |
| 255 |
|
| 256 |
def compile_lily_files(): |
| 257 |
"""Compiles LilyPond files to """ |
| 258 |
print('Compile with LilyPond:') |
| 259 |
for file in lily_files: |
| 260 |
args = [] |
| 261 |
args.append("lilypond") |
| 262 |
args.append("-o") |
| 263 |
args.append(dir_lysrc) |
| 264 |
args.append("-dpreview") |
| 265 |
args.append("-dno-point-and-click") |
| 266 |
args.append(os.path.join(dir_lysrc, file + ".ly")) |
| 267 |
subprocess.call(args) |
| 268 |
print('') |
| 269 |
|
| 270 |
def generate_latex_commands(): |
| 271 |
"""Generates the templates for the commands in a new LaTeX file. |
| 272 |
These should manually be moved to the appropriate .inp files |
| 273 |
in lilyglyphs""" |
| 274 |
|
| 275 |
# iterate over the list of commands |
| 276 |
for cmd_name in in_cmds: |
| 277 |
latex_cmds[cmd_name] = {} |
| 278 |
|
| 279 |
# create LaTeX command |
| 280 |
cmd = [] |
| 281 |
for line in in_cmds[cmd_name]['comment']: |
| 282 |
cmd.append('% ' + line + '\n') |
| 283 |
cmd.append(signature() + '\n') |
| 284 |
template = cmd_templates[in_cmds[cmd_name]['type']] |
| 285 |
template = template.replace('CMD', cmd_name) |
| 286 |
if 'scale' in in_cmds[cmd_name]: |
| 287 |
scale = in_cmds[cmd_name]['scale'] |
| 288 |
else: |
| 289 |
scale = DEF_SCALE |
| 290 |
template = template.replace('SCALE', scale) |
| 291 |
if 'raise' in in_cmds[cmd_name]: |
| 292 |
rais = in_cmds[cmd_name]['raise'] |
| 293 |
else: |
| 294 |
rais = DEF_RAISE |
| 295 |
template = template.replace('RAISE', rais) |
| 296 |
cmd.append(template.replace('ELEM', in_cmds[cmd_name]['element'])) |
| 297 |
latex_cmds[cmd_name]['cmd'] = cmd |
| 298 |
|
| 299 |
# create LaTeX test code |
| 300 |
tc = [] |
| 301 |
tc.append(testcode_template.replace('CMD', cmd_name)) |
| 302 |
latex_cmds[cmd_name]['testcode'] = tc |
| 303 |
|
| 304 |
def read_input_file(in_file): |
| 305 |
"""Reads the input source file and stores it |
| 306 |
in the global variable definitions_file""" |
| 307 |
global definitions_file |
| 308 |
|
| 309 |
in_file = os.path.normpath(in_file) |
| 310 |
|
| 311 |
print('Read input file ' + in_file) |
| 312 |
|
| 313 |
# check for existence of input file |
| 314 |
if not os.path.exists(in_file): |
| 315 |
print('File ' + in_file + ' not found.') |
| 316 |
print('Please specify an input file') |
| 317 |
sys.exit(2) |
| 318 |
|
| 319 |
fin = open(in_file, 'r') |
| 320 |
for line in fin: |
| 321 |
definitions_file.append(line.rstrip(' \n')) |
| 322 |
fin.close() |
| 323 |
|
| 324 |
def script_name(): |
| 325 |
dummy, result = os.path.split(sys.argv[0]) |
| 326 |
return result |
| 327 |
|
| 328 |
def signature(): |
| 329 |
"""Returns a signature to be inserted in an output file""" |
| 330 |
return '% created by ' + script_name() + ' on ' + str(datetime.date.today()) |
| 331 |
|
| 332 |
def write_latex_file(file_name): |
| 333 |
fout = open(file_name, 'w') |
| 334 |
fout.write('% New Glyphs for the lilyglyphs package\n') |
| 335 |
fout.write(signature() + '\n') |
| 336 |
fout.write(latexfile_start_comment.replace('SCRIPT_NAME', script_name())) |
| 337 |
|
| 338 |
# write out command definitions |
| 339 |
sorted_cmds = sorted(latex_cmds.keys()) |
| 340 |
for cmd_name in sorted_cmds: |
| 341 |
for line in latex_cmds[cmd_name]['cmd']: |
| 342 |
fout.write(line) |
| 343 |
|
| 344 |
fout.write(latexfile_begin_document) |
| 345 |
fout.write(signature()[2:]+ '\n') |
| 346 |
fout.write(latexfile_reftable) |
| 347 |
|
| 348 |
# write out the reference table |
| 349 |
row_template = '\\CMD & \\cmd{CMD} & description\\\\' |
| 350 |
for cmd_name in sorted_cmds: |
| 351 |
fout.write(row_template.replace('CMD', cmd_name) + '\n') |
| 352 |
fout.write(latexfile_testcode) |
| 353 |
|
| 354 |
# write out the test code |
| 355 |
for cmd_name in sorted_cmds: |
| 356 |
for line in latex_cmds[cmd_name]['testcode']: |
| 357 |
fout.write(line) |
| 358 |
|
| 359 |
fout.write('\\end{document}\n') |
| 360 |
fout.close() |