Browse Source

Ajout automatique des langues dans Babel

remotes/origin/next
Louis 12 years ago
parent
commit
6dd81e9aa5
  1. 16
      parse.py
  2. 17
      songbook.py
  3. 7
      templates/ancient.tmpl
  4. 7
      templates/minimal.tmpl
  5. 8
      templates/patacrep.tmpl
  6. 31
      utils/plastex.py

16
parse.py

@ -1,16 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
FILENAME = "naheulbeuk.tex"
import plasTeX
from plasTeX.TeX import TeX
doc = TeX(file = FILENAME).parse()
language_list = set()
for node in doc.allChildNodes:
if node.nodeName == "selectlanguage":
language_list.add(node.argSource)
print language_list

17
songbook.py

@ -12,17 +12,20 @@ import locale
import platform import platform
from utils.utils import recursiveFind from utils.utils import recursiveFind
from utils.plastex import parsetex
reTitle = re.compile('(?<=beginsong\\{)(.(?<!\\}]))+') reTitle = re.compile('(?<=beginsong\\{)(.(?<!\\}]))+')
reArtist = re.compile('(?<=by=)(.(?<![,\\]\\}]))+') reArtist = re.compile('(?<=by=)(.(?<![,\\]\\}]))+')
reAlbum = re.compile('(?<=album=)(.(?<![,\\]\\}]))+') reAlbum = re.compile('(?<=album=)(.(?<![,\\]\\}]))+')
class Song: class Song:
def __init__(self, title, artist, album, path): def __init__(self, title, artist, album, path, languages):
self.title = title self.title = title
self.artist = artist self.artist = artist
self.album = album self.album = album
self.path = path self.path = path
self.languages = languages
def __repr__(self): def __repr__(self):
return repr((self.title, self.artist, self.album, self.path)) return repr((self.title, self.artist, self.album, self.path))
@ -86,7 +89,11 @@ class SongsList:
album = match.group(0) album = match.group(0)
else: else:
album = '' album = ''
self.songs.append(Song(title, artist, album, path))
# Exécution de PlasTeX
data = parsetex(path)
self.songs.append(Song(title, artist, album, path, **data))
def append_list(self, filelist): def append_list(self, filelist):
"""Ajoute une liste de chansons à la liste """Ajoute une liste de chansons à la liste
@ -113,6 +120,10 @@ class SongsList:
result = [ '\\input{{{0}}}'.format(song.path.replace("\\","/").strip()) for song in self.songs ] result = [ '\\input{{{0}}}'.format(song.path.replace("\\","/").strip()) for song in self.songs ]
return '\n'.join(result) return '\n'.join(result)
def languages(self):
"""Renvoie la liste des langues utilisées par les chansons"""
return set().union(*[set(song.languages) for song in self.songs])
def parseTemplate(template): def parseTemplate(template):
embeddedJsonPattern = re.compile(r"^%%:") embeddedJsonPattern = re.compile(r"^%%:")
f = open(template) f = open(template)
@ -188,6 +199,8 @@ def makeTexFile(sb, library, output):
songslist = SongsList(library, prefixes) songslist = SongsList(library, prefixes)
songslist.append_list(songs) songslist.append_list(songs)
sb["languages"] = ",".join(songslist.languages())
# output relevant fields # output relevant fields
out = open(output, 'w') out = open(output, 'w')
out.write('%% This file has been automatically generated, do not edit!\n') out.write('%% This file has been automatically generated, do not edit!\n')

7
templates/ancient.tmpl

@ -42,14 +42,17 @@
%%: {"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"}, %%: {"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"},
%%: {"name":"songnumberbgcolor", "description":"Number Shade", "type":"color", "default":"#D1E4AE"}, %%: {"name":"songnumberbgcolor", "description":"Number Shade", "type":"color", "default":"#D1E4AE"},
%%: {"name":"notebgcolor", "description":"Note Shade", "type":"color", "default":"#FFFDB3"}, %%: {"name":"notebgcolor", "description":"Note Shade", "type":"color", "default":"#FFFDB3"},
%%: {"name":"indexbgcolor", "description":"Index Shade", "type":"color", "default":"#D1E4AE"} %%: {"name":"indexbgcolor", "description":"Index Shade", "type":"color", "default":"#D1E4AE"},
%%: {"name":"languages", "description":"List of languages used by songs", "default":""}
%%:] %%:]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% begin document % begin document
\makeatletter\def\input@path{{tex/}} \makeatletter\def\input@path{{tex/}}
\documentclass[\getbooktype,\getinstruments,\getbookoptions,\getmainfontsize]{crepbook} \documentclass[\getbooktype,\getinstruments,\getbookoptions,\getmainfontsize]{crepbook}
\usepackage[utf8]{inputenc} \usepackage[utf8]{inputenc}
\usepackage[italian,portuguese,english,spanish,french]{babel} \PassOptionsToPackage{\getlanguages}{babel}
\PassOptionsToPackage{\getlang}{babel}
\usepackage{babel}
\usepackage[T1]{fontenc} \usepackage[T1]{fontenc}
\usepackage{venturisold} \usepackage{venturisold}

7
templates/minimal.tmpl

@ -30,7 +30,8 @@
%%: {"name":"instruments", "description":"Instruments", "type":"flag", "values":["guitar","ukulele"], "join":",", "mandatory":true, "default":["guitar"]}, %%: {"name":"instruments", "description":"Instruments", "type":"flag", "values":["guitar","ukulele"], "join":",", "mandatory":true, "default":["guitar"]},
%%: {"name":"bookoptions", "description":"Options", "type":"flag", "values":["diagram","importantdiagramonly","lilypond","pictures","tabs","repeatchords","onesongperpage"], "join":",", "mandatory":true, "default":["pictures"]}, %%: {"name":"bookoptions", "description":"Options", "type":"flag", "values":["diagram","importantdiagramonly","lilypond","pictures","tabs","repeatchords","onesongperpage"], "join":",", "mandatory":true, "default":["pictures"]},
%%: {"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"}, %%: {"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"},
%%: {"name":"titleprefixwords", "description":"Ignore some words in the beginning of song titles"} %%: {"name":"titleprefixwords", "description":"Ignore some words in the beginning of song titles"},
%%: {"name":"languages", "description":"List of languages used by songs", "default":""}
%%:] %%:]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% begin document % begin document
@ -39,7 +40,9 @@
\usepackage[utf8]{inputenc} \usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} \usepackage[T1]{fontenc}
\usepackage{lmodern} \usepackage{lmodern}
\usepackage[italian,portuguese,spanish,english,french]{babel} \PassOptionsToPackage{\getlanguages}{babel}
\PassOptionsToPackage{\getlang}{babel}
\usepackage{babel}
\lang{\getlang} \lang{\getlang}
\graphicspath{ {img/}, {\getLibraryImgDirectory}, {\getLibraryLilypondDirectory}, {\getCacheDirectory} } \graphicspath{ {img/}, {\getLibraryImgDirectory}, {\getLibraryLilypondDirectory}, {\getCacheDirectory} }

8
templates/patacrep.tmpl

@ -43,7 +43,8 @@
%%: {"name":"songnumberbgcolor", "description":"Number Shade", "type":"color", "default":"#D1E4AE"}, %%: {"name":"songnumberbgcolor", "description":"Number Shade", "type":"color", "default":"#D1E4AE"},
%%: {"name":"notebgcolor", "description":"Note Shade", "type":"color", "default":"#D1E4AE"}, %%: {"name":"notebgcolor", "description":"Note Shade", "type":"color", "default":"#D1E4AE"},
%%: {"name":"indexbgcolor", "description":"Index Shade", "type":"color", "default":"#D1E4AE"}, %%: {"name":"indexbgcolor", "description":"Index Shade", "type":"color", "default":"#D1E4AE"},
%%: {"name":"titleprefixwords", "description":"Ignore some words in the beginning of song titles"} %%: {"name":"titleprefixwords", "description":"Ignore some words in the beginning of song titles"},
%%: {"name":"languages", "description":"List of languages used by songs", "default":""}
%%:] %%:]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% begin document % begin document
@ -52,7 +53,10 @@
\usepackage[utf8]{inputenc} \usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} \usepackage[T1]{fontenc}
\usepackage{lmodern} \usepackage{lmodern}
\usepackage[italian,portuguese,spanish,english,french]{babel}
\PassOptionsToPackage{\getlanguages}{babel}
\PassOptionsToPackage{\getlang}{babel}
\usepackage{babel}
\title{\gettitle} \title{\gettitle}
\author{\getauthor} \author{\getauthor}

31
utils/plastex.py

@ -0,0 +1,31 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import plasTeX
from plasTeX.TeX import TeX
def parsetex(filename):
"""Analyse syntaxique d'un fichier .sg
Renvoie un dictionnaire contenant les métadonnées lues dans le fichier. Les
clefs sont :
- languages: l'ensemble des langages utilisés (recherche des
\selectlanguages{}).
"""
# Chargement du fichier .sg (.tex), et des modules nécessaires
tex = TeX(file = filename)
tex.disableLogging()
tex.ownerDocument.context.loadPackage(tex, "babel")
# Analyse syntaxique
doc= tex.parse()
# Extraction des données
data = {
"languages": set(),
}
for node in doc.allChildNodes:
if node.nodeName == "selectlanguage":
data["languages"].add(node.argSource)
return data
Loading…
Cancel
Save