-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f2a68f
commit a1c00d7
Showing
6 changed files
with
136 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,100 @@ | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
""" | ||
Functions for song file (.sg) rendering. | ||
Functions for song file (.sgc) rendering. | ||
""" | ||
|
||
|
||
from patanet.settings import SONGS_LIBRARY_DIR | ||
|
||
|
||
from patacrep.songs.chordpro import ChordproSong | ||
from patacrep.build import DEFAULT_CONFIG | ||
from patacrep.songs.chordpro import ChordproSong | ||
|
||
from pathlib import PurePosixPath | ||
|
||
import os | ||
|
||
from django.conf import settings | ||
|
||
|
||
def parse_song(filename): | ||
"""Parse song 'filename', and return the corresponding HTML code.""" | ||
# TODO | ||
with open(filename) as fd: | ||
return fd.read() | ||
# TODO: Clean this file | ||
|
||
# Hack to read the .sgc file | ||
filename += "c" | ||
|
||
relpath = os.path.join('songs', filename) | ||
|
||
datadir = settings.SONGS_LIBRARY_DIR | ||
config = DEFAULT_CONFIG.copy() | ||
song_model = ChordproSong(datadir, relpath, config) | ||
song_model.parse(config) | ||
failed = song_model.titles == [] | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
oliverpool
Author
Contributor
|
||
|
||
|
||
metadata = song_model.data | ||
|
||
basedir = os.path.dirname(song_model.fullpath) | ||
|
||
song = { | ||
'album': get_data(metadata, 'album'), | ||
'cover_url': get_cover_url(metadata, basedir, datadir), | ||
'url': get_data(metadata, 'url'), | ||
'chords': get_chords(metadata), | ||
'fullpath': song_model.fullpath, | ||
'capo': None, #Not implemented by patacrep yet | ||
'languages': song_model.languages, | ||
} | ||
|
||
|
||
if failed: | ||
song['failed'] = True | ||
|
||
with open(song['fullpath']) as fd: | ||
song['full_content'] = fd.read() | ||
return song | ||
|
||
def get_data(metadata, key, default=None): | ||
if key in metadata: | ||
return metadata[key] | ||
return default | ||
|
||
def get_chords(metadata): | ||
raw_chords = get_data(metadata, 'define', []) | ||
chords = [] | ||
|
||
def string_pos(strings): | ||
if not strings: | ||
return '' | ||
# Need a fix on the JS lib to join with a space | ||
return ''.join([str(pos) if pos else 'x' for pos in strings]) | ||
|
||
for raw_chord in raw_chords: | ||
chord = { | ||
'key': raw_chord.key.chord, | ||
'basefret': raw_chord.basefret if raw_chord.basefret else 0, | ||
'frets': string_pos(raw_chord.frets), | ||
'fingers': string_pos(raw_chord.fingers), | ||
} | ||
chords.append(chord) | ||
return chords | ||
|
||
def get_cover_path(file_without_ext): | ||
exts = ['', '.jpg', '.png'] | ||
for ext in exts: | ||
print(file_without_ext + ext) | ||
if os.path.isfile(file_without_ext + ext): | ||
return file_without_ext + ext | ||
raise FileNotFoundError() | ||
|
||
def get_cover_url(metadata, basedir, datadir): | ||
cov = get_data(metadata, 'cov') | ||
if not cov: | ||
return None | ||
cover_without_ext = os.path.join(basedir, str(cov)) | ||
coverfile = get_cover_path(cover_without_ext) | ||
relfile = str(PurePosixPath(coverfile).relative_to(datadir)) | ||
return relfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
<div | ||
class="chord-diagram" | ||
data-shift="{{ chord.shift }}" | ||
data-frets="{{ chord.frets_str }}" | ||
{% if chord.fingers_str %} | ||
data-fingers="{{ chord.fingers_str }}" | ||
data-shift="{{ chord.basefret }}" | ||
data-frets="{{ chord.frets }}" | ||
{% if chord.fingers %} | ||
data-fingers="{{ chord.fingers }}" | ||
{% endif %} | ||
data-name="{{ chord.name }}" | ||
data-name="{{ chord.key }}" | ||
> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Je reconnais que c'est bizare, mais rien n'interdit dans les non-spécifications de Chordpro d'avoir une chanson sans titre, non ?