From 909d8120d0e3a8d1daa764cc4c98f468815e5c7c Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Wed, 21 Oct 2015 12:55:33 +0200 Subject: [PATCH] Complete the transition to support only language code --- patacrep/content/song.py | 6 +-- patacrep/data/templates/songs.tex | 10 ++--- patacrep/latex/__init__.py | 22 +++++++++++ patacrep/songs/__init__.py | 38 +------------------ patacrep/songs/chordpro/__init__.py | 3 +- patacrep/songs/chordpro/ast.py | 1 + patacrep/songs/chordpro/data/html/song_header | 4 +- patacrep/songs/chordpro/data/latex/song | 4 +- patacrep/songs/latex/__init__.py | 15 ++++++-- patacrep/templates.py | 3 +- test/test_chordpro/lang.source | 2 +- test/test_chordpro/newline.html | 2 +- 12 files changed, 55 insertions(+), 55 deletions(-) diff --git a/patacrep/content/song.py b/patacrep/content/song.py index 42b4abba..c5455c92 100755 --- a/patacrep/content/song.py +++ b/patacrep/content/song.py @@ -60,8 +60,8 @@ def parse(keyword, argument, contentlist, config): Return a list of Song() instances. """ plugins = config['_song_plugins'] - if '_languages' not in config: - config['_languages'] = set() + if '_langs' not in config: + config['_langs'] = set() songlist = [] for songdir in config['_songdir']: if contentlist: @@ -92,7 +92,7 @@ def parse(keyword, argument, contentlist, config): datadir=songdir.datadir, )) songlist.append(renderer) - config["_languages"].add(renderer.song.language) + config["_langs"].add(renderer.song.lang) if len(songlist) > before: break if len(songlist) == before: diff --git a/patacrep/data/templates/songs.tex b/patacrep/data/templates/songs.tex index 5f7c8af6..957e610e 100644 --- a/patacrep/data/templates/songs.tex +++ b/patacrep/data/templates/songs.tex @@ -52,7 +52,7 @@ "mandatory": true }, "lang": {"description": {"english": "Language", "french": "Langue"}, - "default": {"en": "english", "fr": "french"} + "default": {"english": "en", "french": "fr"} }, "titleprefixwords": {"description": {"english": "Ignore some words in the beginning of song titles", "french": "Ignore des mots dans le classement des chansons"}, @@ -80,11 +80,11 @@ (* block songbookpreambule *) (( super() )) -(* for language in _languages -*) - \PassOptionsToPackage{((language))}{babel} +(* for lang in _langs -*) + \PassOptionsToPackage{(( lang2babel(lang) ))}{babel} (* endfor *) -\usepackage[(( lang2language(lang) ))]{babel} -\lang{(( lang2language(lang) ))} +\usepackage[(( lang2babel(lang) ))]{babel} +\lang{(( lang2babel(lang) ))} \usepackage{graphicx} \graphicspath{ % diff --git a/patacrep/latex/__init__.py b/patacrep/latex/__init__.py index 2c6db73a..108ea445 100644 --- a/patacrep/latex/__init__.py +++ b/patacrep/latex/__init__.py @@ -5,4 +5,26 @@ This module uses an LALR parser to try to parse LaTeX code. LaTeX language will work on simple cases, but not on complex ones. """ +import logging +from collections import OrderedDict + from patacrep.latex.syntax import tex2plain, parse_song + +LOGGER = logging.getLogger(__name__) + +BABEL_LANGUAGES = OrderedDict(( + ('fr', 'french'), + ('en', 'english'), + ('de', 'german'), + ('es', 'spanish'), + ('it', 'italian'), + ('pt', 'portuguese'), +)) + +def lang2babel(lang): + try: + return BABEL_LANGUAGES[lang] + except KeyError: + available = ", ".join(BABEL_LANGUAGES.keys()) + LOGGER.error('Unknown lang code: ' + lang + '. Supported: ' + available) + return 'english' diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index dcea0e27..63be6f62 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -7,30 +7,12 @@ import logging import os import pickle import re -from collections import OrderedDict from patacrep.authors import process_listauthors from patacrep import files, encoding LOGGER = logging.getLogger(__name__) -BABEL_LANGUAGES = OrderedDict(( - ('fr', 'french'), - ('en', 'english'), - ('de', 'german'), - ('es', 'spanish'), - ('it', 'italian'), - ('pt', 'portuguese'), -)) - -def lang2language(lang): - try: - return BABEL_LANGUAGES[lang] - except KeyError: - available = ", ".join(BABEL_LANGUAGES.keys()) - LOGGER.error('Unknown lang code: ' + lang + '. Supported: ' + available) - return 'english' - def cached_name(datadir, filename): """Return the filename of the cache version of the file.""" fullpath = os.path.abspath(os.path.join(datadir, '.cache', filename)) @@ -107,7 +89,7 @@ class Song: "cached", "data", "subpath", - "language", + "lang", "authors", "_filehash", "_version", @@ -204,8 +186,7 @@ class Song: - titles: the list of (raw) titles. This list will be processed to remove prefixes. - - language: the main language of the song, as language recognized by - the LaTeX babel package. + - lang: the main language of the song, as language code.. - authors: the list of (raw) authors. This list will be processed to 'clean' it (see function :func:`patacrep.authors.processauthors`). - data: song metadata. Used (among others) to sort the songs. @@ -264,21 +245,6 @@ class Song: filepath = self.search_file(filename, ['', '.ly']) return filepath if none_if_not_found or filepath else filename - @property - def language(self): - """Return the string corresponding to the babel (LaTeX) language""" - return lang2language(self.lang) - - @language.setter - def language(self, language): - """Set the language code""" - for lang, babel_language in BABEL_LANGUAGES.items(): - if language == babel_language: - self.lang = lang - return - # TODO: raise a nice error - print('Unsupported language:' + language) - def unprefixed_title(title, prefixes): """Remove the first prefix of the list in the beginning of title (if any). """ diff --git a/patacrep/songs/chordpro/__init__.py b/patacrep/songs/chordpro/__init__.py index e0a4be2e..ca597af0 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -9,6 +9,7 @@ from patacrep import encoding, files from patacrep.songs import Song from patacrep.songs.chordpro.syntax import parse_song from patacrep.templates import Renderer +from patacrep.latex import lang2babel class ChordproSong(Song): """Chordpros song parser.""" @@ -57,7 +58,6 @@ class ChordproSong(Song): templatedirs = [] context = { - 'language': self.language, 'lang': self.lang, "titles": self.titles, "authors": self.authors, @@ -72,6 +72,7 @@ class ChordproSong(Song): )) jinjaenv.filters['search_image'] = self.search_image jinjaenv.filters['search_partition'] = self.search_partition + jinjaenv.filters['lang2babel'] = lang2babel try: return Renderer( diff --git a/patacrep/songs/chordpro/ast.py b/patacrep/songs/chordpro/ast.py index c0497c48..a02c93bb 100644 --- a/patacrep/songs/chordpro/ast.py +++ b/patacrep/songs/chordpro/ast.py @@ -31,6 +31,7 @@ DIRECTIVE_SHORTCUTS = { "c": "comment", "gc": "guitar_comment", "cover": "cov", + "language": "lang", } def directive_name(text): diff --git a/patacrep/songs/chordpro/data/html/song_header b/patacrep/songs/chordpro/data/html/song_header index 963ef686..bd401864 100644 --- a/patacrep/songs/chordpro/data/html/song_header +++ b/patacrep/songs/chordpro/data/html/song_header @@ -17,8 +17,8 @@ (* endif *) (* endfor *) -(* if language is defined -*) - Language: (( language ))
+(* if lang is defined -*) + Lang: (( lang ))
(* endif *) (* include 'content_metadata_cover' *) diff --git a/patacrep/songs/chordpro/data/latex/song b/patacrep/songs/chordpro/data/latex/song index 6f51d673..d106c2e8 100644 --- a/patacrep/songs/chordpro/data/latex/song +++ b/patacrep/songs/chordpro/data/latex/song @@ -1,5 +1,5 @@ -(* if language is defined -*) - \selectlanguage{((language))} +(* if lang is defined -*) + \selectlanguage{(( lang2babel(lang) ))} (* endif *) (*- if metadata.columns is defined *) diff --git a/patacrep/songs/latex/__init__.py b/patacrep/songs/latex/__init__.py index 2371c7d9..620475a2 100644 --- a/patacrep/songs/latex/__init__.py +++ b/patacrep/songs/latex/__init__.py @@ -8,19 +8,19 @@ will work on simple cases, but not on complex ones. import os from patacrep import files, encoding -from patacrep.latex import parse_song +from patacrep.latex import parse_song, BABEL_LANGUAGES from patacrep.songs import Song class LatexSong(Song): """LaTeX song parser.""" def _parse(self, __config): - """Parse content, and return the dictinory of song data.""" + """Parse content, and return the dictionary of song data.""" with encoding.open_read(self.fullpath, encoding=self.encoding) as song: self.data = parse_song(song.read(), self.fullpath) self.titles = self.data['@titles'] del self.data['@titles'] - self.language = self.data['@language'] + self.set_lang(self.data['@language']) del self.data['@language'] if "by" in self.data: self.authors = [self.data['by']] @@ -38,6 +38,15 @@ class LatexSong(Song): )) return r'\import{{{}/}}{{{}}}'.format(os.path.dirname(path), os.path.basename(path)) + def set_lang(self, language): + """Set the language code""" + for lang, babel_language in BABEL_LANGUAGES.items(): + if language == babel_language: + self.lang = lang + return + # TODO: add as custom language + LOGGER.error('Unsupported language:' + language) + SONG_PARSERS = { 'is': LatexSong, 'sg': LatexSong, diff --git a/patacrep/templates.py b/patacrep/templates.py index 204d0b7a..a3ac78b9 100644 --- a/patacrep/templates.py +++ b/patacrep/templates.py @@ -9,6 +9,7 @@ import re import json from patacrep import errors, files, songs +from patacrep.latex import lang2babel import patacrep.encoding _LATEX_SUBS = ( @@ -84,7 +85,7 @@ class Renderer: self.jinjaenv.trim_blocks = True self.jinjaenv.lstrip_blocks = True self.jinjaenv.globals["path2posix"] = files.path2posix - self.jinjaenv.globals["lang2language"] = songs.lang2language + self.jinjaenv.globals["lang2babel"] = lang2babel self.template = self.jinjaenv.get_template(template) diff --git a/test/test_chordpro/lang.source b/test/test_chordpro/lang.source index ac284b64..c9a4c305 100644 --- a/test/test_chordpro/lang.source +++ b/test/test_chordpro/lang.source @@ -1 +1 @@ -{language: french} +{language: fr} diff --git a/test/test_chordpro/newline.html b/test/test_chordpro/newline.html index 1fbb02a3..aca33fa6 100644 --- a/test/test_chordpro/newline.html +++ b/test/test_chordpro/newline.html @@ -1,4 +1,4 @@ -Language: english
+Lang: en