diff --git a/songbook/plastex-songs.py b/songbook/plastex-songs.py index 6d409d8d..cddec648 100644 --- a/songbook/plastex-songs.py +++ b/songbook/plastex-songs.py @@ -3,6 +3,8 @@ import plasTeX +from songbook.plastex import processUnbreakableSpace + def split_linebreak(texlist): return_list = [] current = [] @@ -11,7 +13,7 @@ def split_linebreak(texlist): return_list.append(current) current = [] else: - current.append(token.textContent.encode('utf-8')) + current.append(processUnbreakableSpace(token).textContent.encode('utf-8')) if current: return_list.append(current) return return_list @@ -31,7 +33,7 @@ class beginsong(plasTeX.Command): args = {} for (key, val) in self.attributes['args'].iteritems(): if isinstance(val, plasTeX.DOM.Element): - args[key] = val.textContent.encode('utf-8') + args[key] = processUnbreakableSpace(val).textContent.encode('utf-8') elif isinstance(val, unicode): args[key] = val.encode('utf-8') elif isinstance(val, str): diff --git a/songbook/plastex.py b/songbook/plastex.py index 1aab62fb..a130e81e 100755 --- a/songbook/plastex.py +++ b/songbook/plastex.py @@ -2,12 +2,25 @@ # -*- coding: utf-8 -*- from plasTeX.TeX import TeX +from plasTeX.Base.LaTeX import Sentences + import codecs import copy import locale import os import sys +def processUnbreakableSpace(node): + """Replace '~' and '\ ' in node by nodes that will be rendered as unbreakable space. + + Return node object for convenience. + """ + if type(node) == Sentences.InterWordSpace or (type(node) == Sentences.NoLineBreak and node.source == '~ '): + node.unicode = unichr(160) + for child in node.childNodes: + processUnbreakableSpace(child) + + return node def simpleparse(text): """Parse a simple LaTeX string. @@ -15,7 +28,7 @@ def simpleparse(text): tex = TeX() tex.input(text.decode('utf8')) doc = tex.parse() - return doc.textContent + return processUnbreakableSpace(doc.textContent) class SongParser: """Analyseur syntaxique de fichiers .sg"""