From c8d80134c50754e4725c071f604a081d98de3c9d Mon Sep 17 00:00:00 2001 From: Luthaf Date: Sun, 15 Jun 2014 12:21:52 +0100 Subject: [PATCH 01/18] [WIP] utilisation de plastex pour afficher les chants --- generator/songs.py | 78 +++++++++++++++------------------------- generator/views/songs.py | 4 +-- 2 files changed, 29 insertions(+), 53 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index 7b1308b..6fa57ab 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -21,7 +21,7 @@ from django.utils.encoding import smart_text from django.conf.global_settings import LANGUAGES -from songbook_core.plastex import parsetex +from songbook_core.plastex import parsetex, SongParser from generator.models import Song, Artist @@ -30,55 +30,33 @@ import sys import pprint -_BLOCKS_PATTERNS = [(r"\beginverse", '

'), - (r"\begin{verse}", '

'), - (r"\beginverse*", '

'), - (r"\begin{verse*}", '

'), - (r"\beginchorus", '

'), - (r"\begin{chorus}", '

'), - ] - -_BLOCKS_PATTERNS += [(r"\endverse", '

'), - (r"\end{verse}", '

'), - (r"\end{verse*}", '

'), - (r"\end{chorus}", '

'), - ] - - -def parse_chords(content): - content = re.sub('\\\\\\[(.*?)\]({[^\\\\\s\n]*}|[^\\\\\s\n]*)', - '\g<1>' - '\g<2>', - content) - content = content.replace('&', "♭") - content = content.replace('#', "♯ ") - return content - - -def parse_blocks(content): - for TeX, HTML in _BLOCKS_PATTERNS: - content = content.replace(TeX, HTML) - return content - - -def parse_unsuported(content): - # remove the beggining of the song - content = re.sub('^(.*?) Date: Mon, 16 Jun 2014 23:10:19 +0200 Subject: [PATCH 02/18] Improving chord rendering --- generator/songs.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index 6fa57ab..3cdb221 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -46,8 +46,7 @@ def parse_song(filename): res += '\n

\n' elif node.nodeName == "chord" and in_song: res += '' - for subnode in node.allChildNodes: - res += subnode + res += node.chord res += '' # TODO: get chord text res += '' From fa9d15c76d17a0d895f8c6b217770a5a4c8c432a Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 17 Jun 2014 14:17:05 +0200 Subject: [PATCH 03/18] Proposition en s'inspirant des renderer de plasTeX --- generator/songs.py | 94 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 24 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index 3cdb221..4005b3b 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -30,32 +30,78 @@ import sys import pprint +class Renderer: + def __init__(self, document): + self.document = document + self._render = { + '#text': self.renderText, + 'verse': self.renderVerse, + 'verse*': self.renderVerse, + 'chorus': self.renderVerse, + 'bridge': self.renderVerse, + 'par': self.renderPar, + 'chord': self.renderChord, + } + + def renderNodes(self, nodes): + return "".join([ + self._render.get(node.nodeName, self.renderDefault)(node) + for node in nodes + ]) + + def renderDefault(self, node): + if getattr(node, 'tagName', None) == 'active::\n': + return "
" + else: + print("TODO Default for", unicode(node)) + return "" + + def renderText(self, node): + return unicode(node) + + def renderVerse(self, node): + res = "" + res += "

\n".format(node.nodeName.replace('*', '_star')) + res += self.renderNodes(node.childNodes) + res += "

" + return res + + def renderPar(self, node): + # TODO + return "" + + def renderChord(self, node): + # Je ne comprend pas pourquoi la version suivante, bien plus élégante, + # m'introduit des saut de ligne partout... + # return u""" + # + # {} + # + # {} + # + # """.format( + # node.chord.replace('&', u"♭").replace('#', u"♯"), + # "", # TODO + # ) + return ( + u'' + u'' + u'{}' + u'' + u'{}' + u'' + u'' + ).format( + node.chord.replace('&', u"♭").replace('#', u"♯"), + "", # TODO + ) + + def parse_song(filename): + filename = '/home/louis/projets/songbook/core/songbook_core/data/examples/songs/vent_frais.sg' tex = SongParser.parse(filename) - in_song = False - res = "" - - for node in tex.allChildNodes: - if node.nodeName in ["chorus", "verse", "verse*", "bridge"]: - if not in_song: - in_song = True - part_name = node.nodeName - part_name.replace("*", "_star") - res += '

\n'.format(node=node.nodeName) - elif node.nodeName == "par" and in_song: - res += '\n

\n' - elif node.nodeName == "chord" and in_song: - res += '' - res += node.chord - res += '' - # TODO: get chord text - res += '' - # TODO: advance in the loop - elif node.nodeName == "#text" and in_song: - res += node - res = res.replace('&', "♭") - res = res.replace('#', "♯ ") - return res + return Renderer(tex).renderNodes(tex.childNodes) + # TODO: Write all the output to a log file def import_song(repo, filepath): From 6f1fac45099bba99a50a1ef5cff6b353f99005ed Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 17 Jun 2014 14:26:23 +0200 Subject: [PATCH 04/18] Oubli --- generator/songs.py | 1 - 1 file changed, 1 deletion(-) diff --git a/generator/songs.py b/generator/songs.py index 4005b3b..63628aa 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -98,7 +98,6 @@ def renderChord(self, node): def parse_song(filename): - filename = '/home/louis/projets/songbook/core/songbook_core/data/examples/songs/vent_frais.sg' tex = SongParser.parse(filename) return Renderer(tex).renderNodes(tex.childNodes) From 9079e2e1de5ca5cdd2c19fc207342a831ff3dddb Mon Sep 17 00:00:00 2001 From: Luthaf Date: Tue, 17 Jun 2014 21:10:38 +0100 Subject: [PATCH 05/18] Rendu sans linebreak --- generator/songs.py | 34 ++++++-------------- generator/templates/generator/show_song.html | 2 +- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index 63628aa..0296ab2 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -71,30 +71,16 @@ def renderPar(self, node): return "" def renderChord(self, node): - # Je ne comprend pas pourquoi la version suivante, bien plus élégante, - # m'introduit des saut de ligne partout... - # return u""" - # - # {} - # - # {} - # - # """.format( - # node.chord.replace('&', u"♭").replace('#', u"♯"), - # "", # TODO - # ) - return ( - u'' - u'' - u'{}' - u'' - u'{}' - u'' - u'' - ).format( - node.chord.replace('&', u"♭").replace('#', u"♯"), - "", # TODO - ) + return u""" + + {} + + {} + + """.format( + node.chord.replace('&', u"♭").replace('#', u"♯"), + "", # TODO + ) def parse_song(filename): diff --git a/generator/templates/generator/show_song.html b/generator/templates/generator/show_song.html index 0a23f10..11a56fd 100644 --- a/generator/templates/generator/show_song.html +++ b/generator/templates/generator/show_song.html @@ -34,7 +34,7 @@

{% endcomment %} -
{{ content|safe|linebreaks }}
+
{{ content|safe }}
{% endblock %} From f0249ff310ebfdc0e33a21f0506dea6f440e7149 Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 21 Jul 2014 17:06:43 +0200 Subject: [PATCH 06/18] =?UTF-8?q?Prise=20en=20compte=20des=20derni=C3=A8re?= =?UTF-8?q?s=20avanc=C3=A9es=20de=20patacrep=20pour=20l'analyse=20des=20ac?= =?UTF-8?q?cords?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/patacrep/patacrep/commit/af9c39507966dc028b0def9edfc28e13a1ee5ce7 --- generator/songs.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index cd49e9a..648a898 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -44,23 +44,35 @@ def __init__(self, document): 'bridge': self.renderVerse, 'par': self.renderPar, 'chord': self.renderChord, + 'active::\n': self.renderPlainText(u"
"), + 'dots': self.renderPlainText(u"…"), } + self._render_text = {} def renderNodes(self, nodes): return "".join([ - self._render.get(node.nodeName, self.renderDefault)(node) + self._render.get( + node.nodeName, + self.renderDefault + )(node) for node in nodes ]) + def renderDefault(self, node): - if getattr(node, 'tagName', None) == 'active::\n': - return "
" - else: - print("TODO Default for", unicode(node)) - return "" + print("TODO Default for", unicode(node), node.nodeName) + return u"" + + def renderPlainText(self, string): + def __renderPlainText(__): + return string + return __renderPlainText def renderText(self, node): - return unicode(node) + return self._render_text.get( + unicode(node), + self.renderPlainText(unicode(node)), + )(node) def renderVerse(self, node): res = "" @@ -74,14 +86,21 @@ def renderPar(self, node): return "" def renderChord(self, node): - return u""" + # TODO Remplacer par un with + self._render.update({ + 'active::&': self.renderPlainText(u"♭"), + }) + self._render_text.update({ + '#': self.renderPlainText(u"♯"), + }) + return u""" {} {} """.format( - node.chord.replace('&', u"♭").replace('#', u"♯"), + self.renderNodes(node.childNodes), "", # TODO ) From 0eb9ef5f20c88a74aa4269e8626635e721f5552d Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 21 Jul 2014 18:33:53 +0200 Subject: [PATCH 07/18] Remplacement d'une partie par un `with` --- generator/songs.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index 648a898..8975574 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -31,6 +31,8 @@ import pprint import logging +from contextlib import contextmanager + LOGGER = logging.getLogger(__name__) class Renderer: @@ -49,6 +51,13 @@ def __init__(self, document): } self._render_text = {} + @contextmanager + def push(self, attr, extension): + old = dict(getattr(self, attr)) # Copy of main dictionary + getattr(self, attr).update(extension) + yield + setattr(self, attr, old) + def renderNodes(self, nodes): return "".join([ self._render.get( @@ -86,23 +95,18 @@ def renderPar(self, node): return "" def renderChord(self, node): - # TODO Remplacer par un with - self._render.update({ - 'active::&': self.renderPlainText(u"♭"), - }) - self._render_text.update({ - '#': self.renderPlainText(u"♯"), - }) - return u""" - - {} - - {} - - """.format( - self.renderNodes(node.childNodes), - "", # TODO - ) + with self.push("_render", {'active::&': self.renderPlainText(u"♭")}): + with self.push("_render_text", {'#': self.renderPlainText(u"♯")}): + return u""" + + {} + + {} + + """.format( + self.renderNodes(node.childNodes), + "", # TODO + ) def parse_song(filename): From a2b3a08698f32ad956e8c0c80a9178b0644fa23f Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 21 Jul 2014 18:47:49 +0200 Subject: [PATCH 08/18] pylint compliant --- generator/songs.py | 93 ++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index 8975574..c6c5cc2 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -35,68 +35,92 @@ LOGGER = logging.getLogger(__name__) -class Renderer: +class Renderer(object): + """Render a PlasTeX-parsed song as HTML""" + def __init__(self, document): self.document = document self._render = { - '#text': self.renderText, - 'verse': self.renderVerse, - 'verse*': self.renderVerse, - 'chorus': self.renderVerse, - 'bridge': self.renderVerse, - 'par': self.renderPar, - 'chord': self.renderChord, - 'active::\n': self.renderPlainText(u"
"), - 'dots': self.renderPlainText(u"…"), + '#text': self.render_text, + 'verse': self.render_verse, + 'verse*': self.render_verse, + 'chorus': self.render_verse, + 'bridge': self.render_verse, + 'par': self.render_par, + 'chord': self.render_chord, + 'active::\n': self.render_plain_text(u"
"), + 'dots': self.render_plain_text(u"…"), } self._render_text = {} @contextmanager def push(self, attr, extension): - old = dict(getattr(self, attr)) # Copy of main dictionary + """With statement used to locally update a dictonary + + Arguments: + - attr: name (as a string) of the attribute to update. `self.attr` + must be a dictonary. + - extension: dictionary to use to update the dictonary. + """ + old = dict(getattr(self, attr)) getattr(self, attr).update(extension) yield setattr(self, attr, old) - def renderNodes(self, nodes): + @staticmethod + def render_plain_text(string): + """Return a `render_*`-like function that return a constant string.""" + def _render_plain_text(__node): + """Return constant string.""" + return string + return _render_plain_text + + def render_nodes(self, nodes): + """Render a list of nodes""" return "".join([ self._render.get( node.nodeName, - self.renderDefault + self.render_default )(node) for node in nodes ]) - def renderDefault(self, node): + @staticmethod + def render_default(node): + """Default rendering of a node""" print("TODO Default for", unicode(node), node.nodeName) return u"" - def renderPlainText(self, string): - def __renderPlainText(__): - return string - return __renderPlainText + def render_text(self, node): + """Render a text node. - def renderText(self, node): + If `unicode(node)` is a key of `self._render_text`, call the + corresponding method. Otherwise, return unicode(node). + """ return self._render_text.get( unicode(node), - self.renderPlainText(unicode(node)), + self.render_plain_text(unicode(node)), )(node) - def renderVerse(self, node): + def render_verse(self, node): + """Render a `verse`, `verse*` or `chorus` environment.""" res = "" res += "

\n".format(node.nodeName.replace('*', '_star')) - res += self.renderNodes(node.childNodes) + res += self.render_nodes(node.childNodes) res += "

" return res - def renderPar(self, node): + @staticmethod + def render_par(__node): + """Render a paragraph.""" # TODO return "" - def renderChord(self, node): - with self.push("_render", {'active::&': self.renderPlainText(u"♭")}): - with self.push("_render_text", {'#': self.renderPlainText(u"♯")}): + def render_chord(self, node): + r"""Render a chord command `\[`.""" + with self.push("_render", {'active::&': self.render_plain_text(u"♭")}): + with self.push("_render_text", {'#': self.render_plain_text(u"♯")}): return u""" {} @@ -104,19 +128,21 @@ def renderChord(self, node): {} """.format( - self.renderNodes(node.childNodes), + self.render_nodes(node.childNodes), "", # TODO ) def parse_song(filename): + """Parse song 'filename', and return the corresponding HTML code.""" + filename = filename.replace("../patacrep/data/examples/songs/", "") tex = SongParser.parse(filename) - return Renderer(tex).renderNodes(tex.childNodes) + return Renderer(tex).render_nodes(tex.childNodes) def import_song(repo, filepath): '''Import a song in the database''' data = parsetex(filepath) - LOGGER.info("Processing " + + LOGGER.info("Processing " + pprint.pformat(data['titles'][0])) artist_name = smart_text(data['args']['by'], 'utf-8') @@ -169,10 +195,11 @@ def import_song(repo, filepath): title=song_title, artist=artist_model, defaults={ - 'title': song_title, - 'language': language_code, - 'file_path': filepath_rel, - 'slug': ('%06x' % random.randrange(16**6)) }) + 'title': song_title, + 'language': language_code, + 'file_path': filepath_rel, + 'slug': ('%06x' % random.randrange(16**6)) + }) if created: if Song.objects.filter(slug=song_slug).exists(): song_slug += '-' + str(song_model.id) From 0e98a787476c27516f455f20b0725d51b92ef8ae Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 21 Jul 2014 19:03:52 +0200 Subject: [PATCH 09/18] More LaTeX commands rendered --- generator/songs.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/generator/songs.py b/generator/songs.py index c6c5cc2..127f4a2 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -50,6 +50,12 @@ def __init__(self, document): 'chord': self.render_chord, 'active::\n': self.render_plain_text(u"
"), 'dots': self.render_plain_text(u"…"), + 'selectlanguage': self.render_silent, + 'songcolumns': self.render_silent, + 'beginsong': self.render_silent, + 'endsong': self.render_silent, + 'cover': self.render_silent, + 'gtab': self.render_gtab, } self._render_text = {} @@ -85,13 +91,29 @@ def render_nodes(self, nodes): for node in nodes ]) - @staticmethod def render_default(node): """Default rendering of a node""" print("TODO Default for", unicode(node), node.nodeName) return u"" + @staticmethod + def render_silent(_): + """Return nothing + + To be used by LaTeX commands that does not produce any output. + """ + return u"" + + @staticmethod + def render_gtab(node): + r"""Render LaTeX `\gtab` command.""" + print "TODO GTAB" + return u"GTAB({chord}, {diagram})".format( + diagram=node.attributes["diagram"], + chord=node.attributes["chord"], + ) + def render_text(self, node): """Render a text node. From b66c01669499ccf25da1c4489e946e3a551e8a50 Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Mon, 21 Jul 2014 19:55:05 +0200 Subject: [PATCH 10/18] Do not add new_line \n in verse/chorus --- generator/songs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/songs.py b/generator/songs.py index 127f4a2..070ab77 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -128,7 +128,7 @@ def render_text(self, node): def render_verse(self, node): """Render a `verse`, `verse*` or `chorus` environment.""" res = "" - res += "

\n".format(node.nodeName.replace('*', '_star')) + res += "

".format(node.nodeName.replace('*', '_star')) res += self.render_nodes(node.childNodes) res += "

" return res From 48d78587a239f8b5bb261198cf853732cf2a3441 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 22 Jul 2014 14:01:05 +0200 Subject: [PATCH 11/18] =?UTF-8?q?Am=C3=A9lioration=20du=20rendu=20dans=20l?= =?UTF-8?q?es=20cas=20non=20g=C3=A9r=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generator/songs.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/generator/songs.py b/generator/songs.py index 127f4a2..d46659b 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -56,6 +56,7 @@ def __init__(self, document): 'endsong': self.render_silent, 'cover': self.render_silent, 'gtab': self.render_gtab, + 'bgroup': self.render_group, } self._render_text = {} @@ -95,7 +96,7 @@ def render_nodes(self, nodes): def render_default(node): """Default rendering of a node""" print("TODO Default for", unicode(node), node.nodeName) - return u"" + return node.textContent @staticmethod def render_silent(_): @@ -105,6 +106,9 @@ def render_silent(_): """ return u"" + def render_group(self, node): + return """\n{}\n""".format(self.render_nodes(node.childNodes)) + @staticmethod def render_gtab(node): r"""Render LaTeX `\gtab` command.""" From a3188b69710fddd5f05815f698f3e2e90e5b0384 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 22 Jul 2014 14:32:37 +0200 Subject: [PATCH 12/18] Diacritics ar new well rendered --- generator/songs.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index d46659b..1967eb8 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -49,7 +49,6 @@ def __init__(self, document): 'par': self.render_par, 'chord': self.render_chord, 'active::\n': self.render_plain_text(u"
"), - 'dots': self.render_plain_text(u"…"), 'selectlanguage': self.render_silent, 'songcolumns': self.render_silent, 'beginsong': self.render_silent, @@ -84,7 +83,7 @@ def _render_plain_text(__node): def render_nodes(self, nodes): """Render a list of nodes""" - return "".join([ + return u"".join([ self._render.get( node.nodeName, self.render_default @@ -94,9 +93,25 @@ def render_nodes(self, nodes): @staticmethod def render_default(node): - """Default rendering of a node""" - print("TODO Default for", unicode(node), node.nodeName) - return node.textContent + """Default rendering of a node + + - If `node` has an attribute `unicode`, return + `node.unicode`. + - Otherwise, return `node.textContent`. + """ + if hasattr(node, 'unicode'): + if isinstance(node.unicode, basestring): + return node.unicode + + LOGGER.warning(u"Cannot render node {} (type: {}; name: {})".format( + node, + type(node), + node.nodeName, + )) + if isinstance(node.textContent, basestring): + return node.textContent + else: + return u"" @staticmethod def render_silent(_): @@ -121,12 +136,13 @@ def render_gtab(node): def render_text(self, node): """Render a text node. - If `unicode(node)` is a key of `self._render_text`, call the - corresponding method. Otherwise, return unicode(node). + - If `unicode(node)` is a key of `self._render_text`, return + `self._render_text[unicode(node)](node)`. + - Otherwise, return `self.render_default(node)`. """ return self._render_text.get( unicode(node), - self.render_plain_text(unicode(node)), + self.render_default, )(node) def render_verse(self, node): From 621cce03dae5119367faf31c6656cd5cd72c427c Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 23 Jul 2014 10:23:22 +0200 Subject: [PATCH 13/18] Improving chord rendering --- generator/songs.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index 1967eb8..ede4fe1 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -122,7 +122,7 @@ def render_silent(_): return u"" def render_group(self, node): - return """\n{}\n""".format(self.render_nodes(node.childNodes)) + return self.render_nodes(node.childNodes) @staticmethod def render_gtab(node): @@ -163,16 +163,15 @@ def render_chord(self, node): r"""Render a chord command `\[`.""" with self.push("_render", {'active::&': self.render_plain_text(u"♭")}): with self.push("_render_text", {'#': self.render_plain_text(u"♯")}): - return u""" - - {} - - {} - - """.format( - self.render_nodes(node.childNodes), - "", # TODO - ) + name = self.render_nodes(node.attributes["name"]) + text = self.render_nodes(node.childNodes) + return u""" + + {name} + + {text} + + """.format(name=name, text=text) def parse_song(filename): From e072ca08b65400f2c3f0304aacde0b35a6967b71 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 23 Jul 2014 10:36:14 +0200 Subject: [PATCH 14/18] Improving rendering of \[ (chord) --- generator/songs.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index ede4fe1..4fe3cd6 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -21,7 +21,7 @@ from django.utils.encoding import smart_text from django.conf.global_settings import LANGUAGES -from patacrep.plastex import parsetex, SongParser +from patacrep.plastex import parsetex from generator.models import Song, Artist @@ -38,8 +38,9 @@ class Renderer(object): """Render a PlasTeX-parsed song as HTML""" - def __init__(self, document): - self.document = document + def __init__(self, song): + self.song = song + self.document = song['_doc'] self._render = { '#text': self.render_text, 'verse': self.render_verse, @@ -73,6 +74,10 @@ def push(self, attr, extension): yield setattr(self, attr, old) + def render(self): + """Return the HTML version of self.""" + return self.render_nodes(self.document.childNodes) + @staticmethod def render_plain_text(string): """Return a `render_*`-like function that return a constant string.""" @@ -177,8 +182,8 @@ def render_chord(self, node): def parse_song(filename): """Parse song 'filename', and return the corresponding HTML code.""" filename = filename.replace("../patacrep/data/examples/songs/", "") - tex = SongParser.parse(filename) - return Renderer(tex).render_nodes(tex.childNodes) + song = parsetex(filename) + return Renderer(song).render() def import_song(repo, filepath): '''Import a song in the database''' From 805c777c4d2e2bf4f97f34c7f9af52aa2dd8d5ae Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Thu, 24 Jul 2014 00:13:56 +0200 Subject: [PATCH 15/18] Missing " around className --- generator/songs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generator/songs.py b/generator/songs.py index ea4177e..c350a86 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -153,7 +153,7 @@ def render_text(self, node): def render_verse(self, node): """Render a `verse`, `verse*` or `chorus` environment.""" res = "" - res += "

".format(node.nodeName.replace('*', '_star')) + res += "

".format(node.nodeName.replace('*', '_star')) res += self.render_nodes(node.childNodes) res += "

" return res From 4f276f7370fc9580bfaf241cc6ab80a15ffd78ee Mon Sep 17 00:00:00 2001 From: Oliverpool Date: Thu, 24 Jul 2014 00:14:45 +0200 Subject: [PATCH 16/18] Remove generated spaces --- generator/songs.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index c350a86..1f0644a 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -170,13 +170,13 @@ def render_chord(self, node): with self.push("_render_text", {'#': self.render_plain_text(u"♯")}): name = self.render_nodes(node.attributes["name"]) text = self.render_nodes(node.childNodes) - return u""" - - {name} - - {text} - - """.format(name=name, text=text) + return ('' + '' + '{name}' + '' + '{text}' + '' + '').format(name=name, text=text) def parse_song(filename): From 35bae25ce9ea8b330402f82e58f7a09bb22d491a Mon Sep 17 00:00:00 2001 From: Luthaf Date: Mon, 1 Sep 2014 21:44:06 +0200 Subject: [PATCH 17/18] All strings are unicode in the HTML --- generator/songs.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index 1f0644a..ad29b6a 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -152,17 +152,17 @@ def render_text(self, node): def render_verse(self, node): """Render a `verse`, `verse*` or `chorus` environment.""" - res = "" - res += "

".format(node.nodeName.replace('*', '_star')) + res = u"" + res += u"

".format(node.nodeName.replace('*', '_star')) res += self.render_nodes(node.childNodes) - res += "

" + res += u"

" return res @staticmethod def render_par(__node): """Render a paragraph.""" # TODO - return "" + return u"" def render_chord(self, node): r"""Render a chord command `\[`.""" @@ -170,13 +170,13 @@ def render_chord(self, node): with self.push("_render_text", {'#': self.render_plain_text(u"♯")}): name = self.render_nodes(node.attributes["name"]) text = self.render_nodes(node.childNodes) - return ('' - '' - '{name}' - '' - '{text}' - '' - '').format(name=name, text=text) + return (u'' + u'' + u'{name}' + u'' + u'{text}' + u'' + u'').format(name=name, text=text) def parse_song(filename): From 9aeff9625d5c304ac55a1578998fbe255505d55e Mon Sep 17 00:00:00 2001 From: Luthaf Date: Wed, 24 Sep 2014 18:55:36 +0200 Subject: [PATCH 18/18] Silent the \gtab rendering --- generator/songs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/generator/songs.py b/generator/songs.py index ad29b6a..4b4980a 100644 --- a/generator/songs.py +++ b/generator/songs.py @@ -55,7 +55,8 @@ def __init__(self, song): 'beginsong': self.render_silent, 'endsong': self.render_silent, 'cover': self.render_silent, - 'gtab': self.render_gtab, + #'gtab': self.render_gtab, + 'gtab': self.render_silent, 'bgroup': self.render_group, } self._render_text = {} @@ -132,7 +133,6 @@ def render_group(self, node): @staticmethod def render_gtab(node): r"""Render LaTeX `\gtab` command.""" - print "TODO GTAB" return u"GTAB({chord}, {diagram})".format( diagram=node.attributes["diagram"], chord=node.attributes["chord"],