From 60a22d223593bdb0eee79067546480248986a894 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Mon, 19 Oct 2015 20:39:49 +0200 Subject: [PATCH] Differentiate language code (lang) and babel language (language) --- examples/example-all.sb | 2 +- examples/songs/greensleeves.sgc | 2 +- examples/songs/tests/chords.sgc | 2 +- examples/songs/tests/errors.sgc | 2 +- patacrep/build.py | 2 +- patacrep/data/templates/songs.tex | 10 +++---- patacrep/songs/__init__.py | 30 +++++++++++++++++++ patacrep/songs/chordpro/__init__.py | 5 +++- patacrep/songs/chordpro/ast.py | 2 +- .../songs/chordpro/data/chordpro/song_header | 4 +-- patacrep/templates.py | 3 +- test/test_chordpro/00.sgc | 2 +- test/test_chordpro/01.sgc | 2 +- test/test_chordpro/02.sgc | 2 +- test/test_chordpro/03.sgc | 2 +- test/test_chordpro/04.sgc | 2 +- test/test_chordpro/05.sgc | 2 +- test/test_chordpro/06.sgc | 2 +- test/test_chordpro/07.sgc | 2 +- test/test_chordpro/08.sgc | 2 +- test/test_chordpro/09.sgc | 2 +- test/test_chordpro/10.sgc | 2 +- test/test_chordpro/11.sgc | 2 +- test/test_chordpro/12.sgc | 2 +- test/test_chordpro/13.sgc | 2 +- test/test_chordpro/21.sgc | 2 +- test/test_chordpro/22.sgc | 2 +- test/test_chordpro/23.sgc | 2 +- test/test_chordpro/24.sgc | 2 +- test/test_chordpro/25.sgc | 2 +- test/test_chordpro/26.sgc | 2 +- test/test_chordpro/27.sgc | 2 +- test/test_chordpro/28.sgc | 2 +- test/test_chordpro/29.sgc | 2 +- test/test_chordpro/author_names.sgc | 2 +- test/test_chordpro/chords.sgc | 2 +- test/test_chordpro/customchords.sgc | 2 +- test/test_chordpro/greensleeves.sgc | 2 +- test/test_chordpro/greensleeves.source | 2 +- test/test_chordpro/invalid_chord.sgc | 2 +- test/test_chordpro/invalid_customchord.sgc | 2 +- test/test_chordpro/metadata.sgc | 2 +- test/test_chordpro/metadata.source | 4 +-- test/test_chordpro/newline.sgc | 2 +- test/test_chordpro/nolyrics.sgc | 2 +- test/test_chordpro/ukulelechords.sgc | 2 +- 46 files changed, 85 insertions(+), 51 deletions(-) diff --git a/examples/example-all.sb b/examples/example-all.sb index 7b94d020..8f5d80a6 100644 --- a/examples/example-all.sb +++ b/examples/example-all.sb @@ -8,7 +8,7 @@ "booktype" : "chorded", "datadir": ["datadir2"], "template" : "patacrep.tex", -"lang" : "french", +"lang" : "fr", "encoding": "utf8", "authwords" : { "sep" : ["and", "et"] diff --git a/examples/songs/greensleeves.sgc b/examples/songs/greensleeves.sgc index e7a6d7cb..c1295968 100644 --- a/examples/songs/greensleeves.sgc +++ b/examples/songs/greensleeves.sgc @@ -1,4 +1,4 @@ -{language : english} +{lang : en} {columns : 2} { title : Greensleeves} {subtitle: Test of the chordpro format} diff --git a/examples/songs/tests/chords.sgc b/examples/songs/tests/chords.sgc index 8779a140..1539387b 100644 --- a/examples/songs/tests/chords.sgc +++ b/examples/songs/tests/chords.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {columns: 1} {title: Chords testing} {subtitle: Test of the chords specification and LaTeX translation} diff --git a/examples/songs/tests/errors.sgc b/examples/songs/tests/errors.sgc index 56cb61b7..3b682f4c 100644 --- a/examples/songs/tests/errors.sgc +++ b/examples/songs/tests/errors.sgc @@ -1,4 +1,4 @@ -{language : english} +{lang : en} {columns : 2} { title : Error} {subtitle: A chordpro file with many errors} diff --git a/patacrep/build.py b/patacrep/build.py index 492f30fc..44023e65 100644 --- a/patacrep/build.py +++ b/patacrep/build.py @@ -29,7 +29,7 @@ GENERATED_EXTENSIONS = [ ] DEFAULT_CONFIG = { 'template': "default.tex", - 'lang': 'english', + 'lang': 'en', 'content': [], 'titleprefixwords': [], 'encoding': None, diff --git a/patacrep/data/templates/songs.tex b/patacrep/data/templates/songs.tex index bdfcf7bb..5f7c8af6 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": {"english": "english", "french": "french"} + "default": {"en": "english", "fr": "french"} }, "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 lang in _languages -*) - \PassOptionsToPackage{((lang))}{babel} +(* for language in _languages -*) + \PassOptionsToPackage{((language))}{babel} (* endfor *) -\usepackage[((lang))]{babel} -\lang{((lang))} +\usepackage[(( lang2language(lang) ))]{babel} +\lang{(( lang2language(lang) ))} \usepackage{graphicx} \graphicspath{ % diff --git a/patacrep/songs/__init__.py b/patacrep/songs/__init__.py index 5c6eb797..818de044 100644 --- a/patacrep/songs/__init__.py +++ b/patacrep/songs/__init__.py @@ -7,12 +7,27 @@ 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'), +)) + +def lang2language(lang): + try: + return BABEL_LANGUAGES[lang] + except KeyError: + # TODO: raise a nice error + print('Unknown lang:' + lang) + 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)) @@ -246,6 +261,21 @@ 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 52b926a8..4b95b7b0 100644 --- a/patacrep/songs/chordpro/__init__.py +++ b/patacrep/songs/chordpro/__init__.py @@ -35,7 +35,9 @@ class ChordproSong(Song): song = parse_song(song.read(), self.fullpath) self.authors = song.authors self.titles = song.titles - self.language = song.get_data_argument('language', self.config['lang']) + #language and lang are synomyms in the directives + language = song.get_data_argument('language', self.config['lang']) + self.lang = song.get_data_argument('lang', language) self.data = song.meta self.cached = { 'song': song, @@ -47,6 +49,7 @@ class ChordproSong(Song): context = { 'language': self.language, + 'lang': self.lang, "titles": self.titles, "authors": self.authors, "metadata": self.data, diff --git a/patacrep/songs/chordpro/ast.py b/patacrep/songs/chordpro/ast.py index 526f7d93..09a3b226 100644 --- a/patacrep/songs/chordpro/ast.py +++ b/patacrep/songs/chordpro/ast.py @@ -181,7 +181,7 @@ class Song(AST): - content: the song content, as a list of objects `foo` such that `foo.inline` is True. - titles: The list of titles - - language: The language (if set), None otherwise + - lang: The language code (if set), None otherwise - authors: The list of authors - meta: Every other metadata. """ diff --git a/patacrep/songs/chordpro/data/chordpro/song_header b/patacrep/songs/chordpro/data/chordpro/song_header index ac5d8329..2ffb1d30 100644 --- a/patacrep/songs/chordpro/data/chordpro/song_header +++ b/patacrep/songs/chordpro/data/chordpro/song_header @@ -1,5 +1,5 @@ -(* if language is defined -*) - {language: (( language ))} +(* if lang is defined -*) + {lang: (( lang ))} (* endif *) (* if metadata.columns is defined -*) {columns: (( metadata.columns ))} diff --git a/patacrep/templates.py b/patacrep/templates.py index 3673f878..204d0b7a 100644 --- a/patacrep/templates.py +++ b/patacrep/templates.py @@ -8,7 +8,7 @@ import os import re import json -from patacrep import errors, files +from patacrep import errors, files, songs import patacrep.encoding _LATEX_SUBS = ( @@ -84,6 +84,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.template = self.jinjaenv.get_template(template) diff --git a/test/test_chordpro/00.sgc b/test/test_chordpro/00.sgc index 3e2185b7..768ee9ee 100644 --- a/test/test_chordpro/00.sgc +++ b/test/test_chordpro/00.sgc @@ -1 +1 @@ -{language: english} +{lang: en} diff --git a/test/test_chordpro/01.sgc b/test/test_chordpro/01.sgc index 25d6a677..6cf1934e 100644 --- a/test/test_chordpro/01.sgc +++ b/test/test_chordpro/01.sgc @@ -1,3 +1,3 @@ -{language: english} +{lang: en} A verse line diff --git a/test/test_chordpro/02.sgc b/test/test_chordpro/02.sgc index 2a8629e0..365a1b9b 100644 --- a/test/test_chordpro/02.sgc +++ b/test/test_chordpro/02.sgc @@ -1,3 +1,3 @@ -{language: english} +{lang: en} {title: A directive} diff --git a/test/test_chordpro/03.sgc b/test/test_chordpro/03.sgc index 341c9bd4..6468f5e9 100644 --- a/test/test_chordpro/03.sgc +++ b/test/test_chordpro/03.sgc @@ -1,2 +1,2 @@ -{language: english} +{lang: en} diff --git a/test/test_chordpro/04.sgc b/test/test_chordpro/04.sgc index e9c2a952..d25f38a8 100644 --- a/test/test_chordpro/04.sgc +++ b/test/test_chordpro/04.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {start_of_chorus} A one line chorus diff --git a/test/test_chordpro/05.sgc b/test/test_chordpro/05.sgc index 9bc7f016..cf6a654b 100644 --- a/test/test_chordpro/05.sgc +++ b/test/test_chordpro/05.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {start_of_bridge} A one line bridge diff --git a/test/test_chordpro/06.sgc b/test/test_chordpro/06.sgc index 341c9bd4..6468f5e9 100644 --- a/test/test_chordpro/06.sgc +++ b/test/test_chordpro/06.sgc @@ -1,2 +1,2 @@ -{language: english} +{lang: en} diff --git a/test/test_chordpro/07.sgc b/test/test_chordpro/07.sgc index b05a536f..1362cdeb 100644 --- a/test/test_chordpro/07.sgc +++ b/test/test_chordpro/07.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {start_of_tab} A tab diff --git a/test/test_chordpro/08.sgc b/test/test_chordpro/08.sgc index 9a9f0566..bfcb44dd 100644 --- a/test/test_chordpro/08.sgc +++ b/test/test_chordpro/08.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} A lot of new lines diff --git a/test/test_chordpro/09.sgc b/test/test_chordpro/09.sgc index 193db1b0..b252de69 100644 --- a/test/test_chordpro/09.sgc +++ b/test/test_chordpro/09.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {title: and a directive} diff --git a/test/test_chordpro/10.sgc b/test/test_chordpro/10.sgc index e7537b3b..431ddd11 100644 --- a/test/test_chordpro/10.sgc +++ b/test/test_chordpro/10.sgc @@ -1,3 +1,3 @@ -{language: english} +{lang: en} A line[A] with a chord diff --git a/test/test_chordpro/11.sgc b/test/test_chordpro/11.sgc index 3610f67c..350f195d 100644 --- a/test/test_chordpro/11.sgc +++ b/test/test_chordpro/11.sgc @@ -1,3 +1,3 @@ -{language: english} +{lang: en} A line ending with a chord[A] diff --git a/test/test_chordpro/12.sgc b/test/test_chordpro/12.sgc index 029ccad4..946d7add 100644 --- a/test/test_chordpro/12.sgc +++ b/test/test_chordpro/12.sgc @@ -1,3 +1,3 @@ -{language: english} +{lang: en} [A]A line starting with a chord diff --git a/test/test_chordpro/13.sgc b/test/test_chordpro/13.sgc index aa6f6603..1000c0db 100644 --- a/test/test_chordpro/13.sgc +++ b/test/test_chordpro/13.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {start_of_tab} A table diff --git a/test/test_chordpro/21.sgc b/test/test_chordpro/21.sgc index 25d6a677..6cf1934e 100644 --- a/test/test_chordpro/21.sgc +++ b/test/test_chordpro/21.sgc @@ -1,3 +1,3 @@ -{language: english} +{lang: en} A verse line diff --git a/test/test_chordpro/22.sgc b/test/test_chordpro/22.sgc index 2a8629e0..365a1b9b 100644 --- a/test/test_chordpro/22.sgc +++ b/test/test_chordpro/22.sgc @@ -1,3 +1,3 @@ -{language: english} +{lang: en} {title: A directive} diff --git a/test/test_chordpro/23.sgc b/test/test_chordpro/23.sgc index 3e2185b7..768ee9ee 100644 --- a/test/test_chordpro/23.sgc +++ b/test/test_chordpro/23.sgc @@ -1 +1 @@ -{language: english} +{lang: en} diff --git a/test/test_chordpro/24.sgc b/test/test_chordpro/24.sgc index e9c2a952..d25f38a8 100644 --- a/test/test_chordpro/24.sgc +++ b/test/test_chordpro/24.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {start_of_chorus} A one line chorus diff --git a/test/test_chordpro/25.sgc b/test/test_chordpro/25.sgc index 9bc7f016..cf6a654b 100644 --- a/test/test_chordpro/25.sgc +++ b/test/test_chordpro/25.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {start_of_bridge} A one line bridge diff --git a/test/test_chordpro/26.sgc b/test/test_chordpro/26.sgc index 341c9bd4..6468f5e9 100644 --- a/test/test_chordpro/26.sgc +++ b/test/test_chordpro/26.sgc @@ -1,2 +1,2 @@ -{language: english} +{lang: en} diff --git a/test/test_chordpro/27.sgc b/test/test_chordpro/27.sgc index b05a536f..1362cdeb 100644 --- a/test/test_chordpro/27.sgc +++ b/test/test_chordpro/27.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {start_of_tab} A tab diff --git a/test/test_chordpro/28.sgc b/test/test_chordpro/28.sgc index 9a9f0566..bfcb44dd 100644 --- a/test/test_chordpro/28.sgc +++ b/test/test_chordpro/28.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} A lot of new lines diff --git a/test/test_chordpro/29.sgc b/test/test_chordpro/29.sgc index 193db1b0..b252de69 100644 --- a/test/test_chordpro/29.sgc +++ b/test/test_chordpro/29.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {title: and a directive} diff --git a/test/test_chordpro/author_names.sgc b/test/test_chordpro/author_names.sgc index 36a39230..b1756cc3 100644 --- a/test/test_chordpro/author_names.sgc +++ b/test/test_chordpro/author_names.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {title: Title} {artist: The Beatles} {artist: Oasis} diff --git a/test/test_chordpro/chords.sgc b/test/test_chordpro/chords.sgc index 040dcf07..983e03ee 100644 --- a/test/test_chordpro/chords.sgc +++ b/test/test_chordpro/chords.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} [A]Simple [Bb]BĂ©mol diff --git a/test/test_chordpro/customchords.sgc b/test/test_chordpro/customchords.sgc index 3357bb96..dd42e025 100644 --- a/test/test_chordpro/customchords.sgc +++ b/test/test_chordpro/customchords.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {define: E4 base-fret 7 frets 0 1 3 3 x x} {define: E5 base-fret 7 frets 0 1 3 3 x x fingers - 1 2 3 - -} {define: E5/A* base-fret 7 frets 0 1 3 3 x x fingers - 1 2 3 - -} diff --git a/test/test_chordpro/greensleeves.sgc b/test/test_chordpro/greensleeves.sgc index 389f14d1..7ff25a58 100644 --- a/test/test_chordpro/greensleeves.sgc +++ b/test/test_chordpro/greensleeves.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {columns: 2} {title: Greensleeves} {title: Un autre sous-titre} diff --git a/test/test_chordpro/greensleeves.source b/test/test_chordpro/greensleeves.source index 1dee93ff..3801418d 100644 --- a/test/test_chordpro/greensleeves.source +++ b/test/test_chordpro/greensleeves.source @@ -1,4 +1,4 @@ -{language : english} +{lang : en} {columns : 2} {subtitle : Un sous titre} { title : Greensleeves} diff --git a/test/test_chordpro/invalid_chord.sgc b/test/test_chordpro/invalid_chord.sgc index acb5444f..3d6319be 100644 --- a/test/test_chordpro/invalid_chord.sgc +++ b/test/test_chordpro/invalid_chord.sgc @@ -1,3 +1,3 @@ -{language: english} +{lang: en} This is invalid. diff --git a/test/test_chordpro/invalid_customchord.sgc b/test/test_chordpro/invalid_customchord.sgc index 341c9bd4..6468f5e9 100644 --- a/test/test_chordpro/invalid_customchord.sgc +++ b/test/test_chordpro/invalid_customchord.sgc @@ -1,2 +1,2 @@ -{language: english} +{lang: en} diff --git a/test/test_chordpro/metadata.sgc b/test/test_chordpro/metadata.sgc index 979c1388..d48ded77 100644 --- a/test/test_chordpro/metadata.sgc +++ b/test/test_chordpro/metadata.sgc @@ -1,4 +1,4 @@ -{language: french} +{lang: fr} {capo: Capo} {title: Title} {title: Subtitle1} diff --git a/test/test_chordpro/metadata.source b/test/test_chordpro/metadata.source index 514ac2c0..02167238 100644 --- a/test/test_chordpro/metadata.source +++ b/test/test_chordpro/metadata.source @@ -4,8 +4,8 @@ {subtitle: Subtitle4} {t: Subtitle2} {st: Subtitle5} -{language: english} -{language: french} +{lang: en} +{lang: fr} {by: Author1} {artist: Author2} {album: Album} diff --git a/test/test_chordpro/newline.sgc b/test/test_chordpro/newline.sgc index 72bdc1a2..e9bf0c62 100644 --- a/test/test_chordpro/newline.sgc +++ b/test/test_chordpro/newline.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} This is a verse With a new line diff --git a/test/test_chordpro/nolyrics.sgc b/test/test_chordpro/nolyrics.sgc index fd59c884..2ad06b03 100644 --- a/test/test_chordpro/nolyrics.sgc +++ b/test/test_chordpro/nolyrics.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} A chorus [A]with lyrics [Emaj3]maj et nombre diff --git a/test/test_chordpro/ukulelechords.sgc b/test/test_chordpro/ukulelechords.sgc index 3b37123d..bbf4aa81 100644 --- a/test/test_chordpro/ukulelechords.sgc +++ b/test/test_chordpro/ukulelechords.sgc @@ -1,4 +1,4 @@ -{language: english} +{lang: en} {define: G frets 0 2 3 2} {define: D7 frets 2 2 2 3 fingers 1 1 1 2} {define: G frets 3 2 0 0 0 3}