Browse Source

[WIP] Songs are now parsed using PlasTeX

pull/47/head
Louis 10 years ago
parent
commit
29b5878cbf
  1. 3
      songbook_core/build.py
  2. 21
      songbook_core/content/song.py
  3. 39
      songbook_core/songs.py

3
songbook_core/build.py

@ -14,7 +14,7 @@ from songbook_core import __DATADIR__
from songbook_core import content from songbook_core import content
from songbook_core import errors from songbook_core import errors
from songbook_core.index import process_sxd 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 from songbook_core.templates import TexRenderer
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
@ -69,6 +69,7 @@ class Songbook(object):
TODO Move this function elsewhere TODO Move this function elsewhere
""" """
return
Song.sort = config['sort'] Song.sort = config['sort']
if 'titleprefixwords' in config: if 'titleprefixwords' in config:
Song.prefixes = config['titleprefixwords'] Song.prefixes = config['titleprefixwords']

21
songbook_core/content/song.py

@ -3,17 +3,18 @@
import glob import glob
import jinja2 import jinja2
import logging
import os import os
from songbook_core.content import Content from songbook_core.content import Content
from songbook_core.files import recursive_find from songbook_core.files import recursive_find
from songbook_core.songs import Song
class Song(Content): LOGGER = logging.getLogger(__name__)
def __init__(self, filename):
self.filename = filename
class SongRenderer(Content, Song):
def begin_new_block(self, previous, __context): def begin_new_block(self, previous, __context):
return not isinstance(previous, Song) return not isinstance(previous, SongRenderer)
def begin_block(self, context): def begin_block(self, context):
indexes = context.resolve("indexes") indexes = context.resolve("indexes")
@ -24,8 +25,13 @@ class Song(Content):
def end_block(self, __context): def end_block(self, __context):
return r'\end{songs}' return r'\end{songs}'
def render(self, __context): def render(self, context):
return r'\input{{{}}}'.format(self.filename) 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): def parse(keyword, config, *arguments):
songlist = [] songlist = []
@ -45,7 +51,8 @@ def parse(keyword, config, *arguments):
before = len(songlist) before = len(songlist)
for songdir in [os.path.join(d, 'songs') for d in config['datadir']]: for songdir in [os.path.join(d, 'songs') for d in config['datadir']]:
for filename in glob.iglob(os.path.join(songdir, elem)): 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: if len(songlist) > before:
break break
if len(songlist) == before: if len(songlist) == before:

39
songbook_core/songs.py

@ -4,17 +4,12 @@
"""Song management.""" """Song management."""
from unidecode import unidecode from unidecode import unidecode
import glob
import locale import locale
import os.path
import re import re
import logging
from songbook_core.authors import processauthors from songbook_core.authors import processauthors
from songbook_core.plastex import parsetex from songbook_core.plastex import parsetex
LOGGER = logging.getLogger(__name__)
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class Song(object): class Song(object):
"""Song management""" """Song management"""
@ -26,8 +21,10 @@ class Song(object):
#: Dictionnaire des options pour le traitement des auteurs #: Dictionnaire des options pour le traitement des auteurs
authwords = {"after": [], "ignore": [], "sep": []} authwords = {"after": [], "ignore": [], "sep": []}
def __init__(self, path, languages, titles, args): def __init__(self, filename):
self.titles = titles # Data extraction from the song with plastex
data = parsetex(filename)
self.titles = data['titles']
self.normalized_titles = [ self.normalized_titles = [
locale.strxfrm( locale.strxfrm(
unprefixed_title( unprefixed_title(
@ -36,11 +33,11 @@ class Song(object):
) )
) )
for title for title
in titles in self.titles
] ]
self.args = args self.args = data['args']
self.path = path self.path = filename
self.languages = languages self.languages = data['languages']
if "by" in self.args.keys(): if "by" in self.args.keys():
self.normalized_authors = [ self.normalized_authors = [
locale.strxfrm(author) locale.strxfrm(author)
@ -87,23 +84,3 @@ def unprefixed_title(title, prefixes):
return title 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))

Loading…
Cancel
Save