From 29b5878cbfb785815f1411d671d5fdb7f70f4145 Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 14 Jun 2014 12:00:16 +0200 Subject: [PATCH] [WIP] Songs are now parsed using PlasTeX --- songbook_core/build.py | 3 ++- songbook_core/content/song.py | 21 ++++++++++++------- songbook_core/songs.py | 39 +++++++---------------------------- 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/songbook_core/build.py b/songbook_core/build.py index 31f94444..cbef29a6 100755 --- a/songbook_core/build.py +++ b/songbook_core/build.py @@ -14,7 +14,7 @@ from songbook_core import __DATADIR__ from songbook_core import content from songbook_core import errors from songbook_core.index import process_sxd -from songbook_core.songs import Song, SongbookContent +from songbook_core.songs import Song from songbook_core.templates import TexRenderer LOGGER = logging.getLogger(__name__) @@ -69,6 +69,7 @@ class Songbook(object): TODO Move this function elsewhere """ + return Song.sort = config['sort'] if 'titleprefixwords' in config: Song.prefixes = config['titleprefixwords'] diff --git a/songbook_core/content/song.py b/songbook_core/content/song.py index 447d20b3..fc371eaf 100644 --- a/songbook_core/content/song.py +++ b/songbook_core/content/song.py @@ -3,17 +3,18 @@ import glob import jinja2 +import logging import os from songbook_core.content import Content from songbook_core.files import recursive_find +from songbook_core.songs import Song -class Song(Content): - def __init__(self, filename): - self.filename = filename +LOGGER = logging.getLogger(__name__) +class SongRenderer(Content, Song): def begin_new_block(self, previous, __context): - return not isinstance(previous, Song) + return not isinstance(previous, SongRenderer) def begin_block(self, context): indexes = context.resolve("indexes") @@ -24,8 +25,13 @@ class Song(Content): def end_block(self, __context): return r'\end{songs}' - def render(self, __context): - return r'\input{{{}}}'.format(self.filename) + def render(self, context): + outdir = os.path.dirname(context['filename']) + if os.path.abspath(self.path).startswith(os.path.abspath(outdir)): + path = os.path.relpath(self.path, outdir) + else: + path = os.path.abspath(self.path) + return r'\input{{{}}}'.format(path) def parse(keyword, config, *arguments): songlist = [] @@ -45,7 +51,8 @@ def parse(keyword, config, *arguments): before = len(songlist) for songdir in [os.path.join(d, 'songs') for d in config['datadir']]: for filename in glob.iglob(os.path.join(songdir, elem)): - songlist.append(Song(filename)) + LOGGER.debug('Parsing file "{}"…'.format(filename)) + songlist.append(SongRenderer(filename)) if len(songlist) > before: break if len(songlist) == before: diff --git a/songbook_core/songs.py b/songbook_core/songs.py index 048e1ffa..5e280413 100644 --- a/songbook_core/songs.py +++ b/songbook_core/songs.py @@ -4,17 +4,12 @@ """Song management.""" from unidecode import unidecode -import glob import locale -import os.path import re -import logging from songbook_core.authors import processauthors from songbook_core.plastex import parsetex -LOGGER = logging.getLogger(__name__) - # pylint: disable=too-few-public-methods class Song(object): """Song management""" @@ -26,8 +21,10 @@ class Song(object): #: Dictionnaire des options pour le traitement des auteurs authwords = {"after": [], "ignore": [], "sep": []} - def __init__(self, path, languages, titles, args): - self.titles = titles + def __init__(self, filename): + # Data extraction from the song with plastex + data = parsetex(filename) + self.titles = data['titles'] self.normalized_titles = [ locale.strxfrm( unprefixed_title( @@ -36,11 +33,11 @@ class Song(object): ) ) for title - in titles + in self.titles ] - self.args = args - self.path = path - self.languages = languages + self.args = data['args'] + self.path = filename + self.languages = data['languages'] if "by" in self.args.keys(): self.normalized_authors = [ locale.strxfrm(author) @@ -87,23 +84,3 @@ def unprefixed_title(title, prefixes): return title -class SongbookContent(object): - """Manipulation et traitement de liste de chansons""" - - def __init__(self, datadirs): - self.songdirs = [os.path.join(d, 'songs') - for d in datadirs] - self.content = [] # Sorted list of the content - - def append_song(self, filename): - """Ajout d'une chanson à la liste - - Effets de bord : analyse syntaxique plus ou moins sommaire du fichier - pour en extraire et traiter certaines information (titre, langue, - album, etc.). - """ - LOGGER.debug('Parsing file "{}"…'.format(filename)) - # Data extraction from the song with plastex - data = parsetex(filename) - song = Song(filename, data['languages'], data['titles'], data['args']) - self.content.append(("song", song))