Engine for LaTeX songbooks http://www.patacrep.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.4 KiB

#!/usr/bin/python
# -*- coding: utf-8 -*-
import glob
import sys
import fileinput
import re
re.LOCALE
# the dictionary has target_word:replacement_word pairs
word_dic = {
##: oe inclusion
"coeur": "cœur",
"boeuf": "bœuf",
"oeuvre": "œuvre",
"soeur": "sœur",
"noeud": "nœud",
"oeil": "œil",
"voeu": "vœu",
"oeuf": "œuf",
"oe{}": "œ",
##: Contractions
"ptit": "p'tit",
##: Punctuation
"": "'",
"Ca ": "Ça ",
"\\musicnote{Intro": "\\musicnote{intro",
"\\musicnote{Outro": "\\musicnote{outro",
"...": "{\\dots}",
"": "{\\dots}",
"say: ``":"say, ``",
"says: ``":"says, ``",
"said: ``":"said, ``",
14 years ago
#replace tabs with two spaces
" ": " ",
##: Typo
"New-York": "New York",
14 years ago
" i ": " I ",
"avant hier": "avant-hier",
##: Conversion to anglo-saxon chords
"Lam ": "Am ",
"La7": "A7",
"Lasus2": "Asus2",
"Sim ": "Bm ",
"Sim}": "Bm}",
"Sim]": "Bm]",
"Si7": "B7",
"Dom ": "Cm ",
"Do7": "C7",
"Do9": "C9",
"": "D ",
"Rém ": "Dm ",
"Rém]": "Dm]",
"Ré7": "D7",
"Ré#": "D#",
"Mim ": "Em ",
"Mim]": "Em]",
"Mim7": "Em7",
"Mim}": "Em}",
"Mi7": "E7",
"Mi7sus4": "E7sus4",
"Fa ": "F ",
"Fa}": "F}",
"Fa\\": "F\\",
"Fam ": "Fm ",
"Fa7": "F7",
"Sol ": "G ",
"Sol]": "G]",
"Solm ": "Gm ",
"Solm]": "Gm]",
"Sol7": "G7",
"/La": "/A",
"/Si": "/B",
"/Do": "/C",
"/Ré": "/D",
"/Mi": "/E",
"/Fa": "/F",
"/Sol": "/G",
"gtab{La": "gtab{A",
"gtab{Si": "gtab{B",
"gtab{Do": "gtab{C",
"gtab{": "gtab{D",
"gtab{Mi": "gtab{E",
"gtab{Fa": "gtab{F",
"gtab{Sol": "gtab{G",
"\\[La": "\\[A",
"\\[Si": "\\[B",
"\\[Do": "\\[C",
"\\[Ré": "\\[D",
"\\[Mi": "\\[E",
"\\[Fa": "\\[F",
"\\[Sol": "\\[G",
"\\[Re": "\\[D",
"b]": "&]",
"b7]": "&7]",
#C
"032010": "X32010",
#A
"002220": "X02220",
"002020": "X02020",
"002210": "X02210",
#D
"000232": "XX0232",
"X00232": "XX0232",
"000212": "XX0212",
"000231": "XX0231",
"X00231": "XX0231",
#B
"021202": "X21202",
### end of rules
}
# Process song files
songfiles = glob.glob('songs/*/*.sg')
for filename in songfiles:
with open(filename, 'r+') as songfile:
data = songfile.read()
#replace words
for search, replace in word_dic.items():
data = data.replace(search, replace)
#no dots for acronyms
# data = re.sub("(?P<capital_letter>[A-Z])\.","\g<capital_letter>", data)
#language based typographical rules
if (re.compile("selectlanguage{french}").search(data)):
#ensure non-breaking spaces before symbols ? ! ; :
data = re.sub("(?P<last_char>\S)(?P<symbol>[!?;:])","\g<last_char> \g<symbol>", data)
# ... except for gtabs macros with capos
data = re.sub("(?P<gtab>gtab.*)\s:","\g<gtab>:", data)
# and apply a second time for cases like \gtab{Gm}{10:X02210:}
data = re.sub("(?P<gtab>gtab.*)\s:","\g<gtab>:", data)
#ensure no spaces after symbols (
data = re.sub("(?P<symbol>[\(])\s(?P<next_char>\S)","\g<symbol>\g<next_char>", data)
#convert inverted commas
data = re.sub("``","{\\og}", data)
data = re.sub("''","{\\\\fg}", data)
elif (re.compile("selectlanguage{english}").search(data)):
#print "english song"
#ensure no spaces before symbols ? ! ; : )
data = re.sub("(?P<last_char>\S)\s(?P<symbol>[!?;:\)])","\g<last_char>\g<symbol>", data)
#ensure no spaces after symbols (
data = re.sub("(?P<symbol>[\(])\s(?P<next_char>\S)","\g<symbol>\g<next_char>", data)
elif (re.compile("selectlanguage{spanish}").search(data)):
#print "spanish song"
#ensure no spaces before symbols ? ! ; : )
data = re.sub("(?P<last_char>\S)\s(?P<symbol>[!?;:\)])","\g<last_char>\g<symbol>", data)
#ensure no spaces after symbols ¿ ¡ (
data = re.sub("(?P<symbol>[¿¡\(])\s(?P<next_char>\S)","\g<symbol>\g<next_char>", data)
elif (re.compile("selectlanguage{portuguese}").search(data)):
#convert inverted commas
data = re.sub("``","{\\og}", data)
data = re.sub("''","{\\\\fg}", data)
else :
print "Warning: language is not defined for song : " + filename
lines = data.split('\n')
for index, line in enumerate(lines):
#remove trailing spaces and punctuation
line = line.rstrip().rstrip(',.;').rstrip()
#remove multi-spaces within lines
line = re.sub("(?P<last_char>\S)\s{2,}","\g<last_char> ", line)
lines[index] = line
data = "\n".join(lines)
songfile.seek(0)
songfile.write(data)
songfile.truncate()