From 3121c28c8f5727e67900c5ce0fb5bd43f79430ef Mon Sep 17 00:00:00 2001 From: Louis Date: Sat, 15 Feb 2014 12:04:17 +0100 Subject: [PATCH] =?UTF-8?q?Traitement=20correct=20des=20espaces=20ins?= =?UTF-8?q?=C3=A9cables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- songbook/plastex-songs.py | 6 ++++-- songbook/plastex.py | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) 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"""