Browse Source

Traitement correct des espaces insécables

pull/3/head
Louis 11 years ago
parent
commit
3121c28c8f
  1. 6
      songbook/plastex-songs.py
  2. 15
      songbook/plastex.py

6
songbook/plastex-songs.py

@ -3,6 +3,8 @@
import plasTeX import plasTeX
from songbook.plastex import processUnbreakableSpace
def split_linebreak(texlist): def split_linebreak(texlist):
return_list = [] return_list = []
current = [] current = []
@ -11,7 +13,7 @@ def split_linebreak(texlist):
return_list.append(current) return_list.append(current)
current = [] current = []
else: else:
current.append(token.textContent.encode('utf-8')) current.append(processUnbreakableSpace(token).textContent.encode('utf-8'))
if current: if current:
return_list.append(current) return_list.append(current)
return return_list return return_list
@ -31,7 +33,7 @@ class beginsong(plasTeX.Command):
args = {} args = {}
for (key, val) in self.attributes['args'].iteritems(): for (key, val) in self.attributes['args'].iteritems():
if isinstance(val, plasTeX.DOM.Element): 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): elif isinstance(val, unicode):
args[key] = val.encode('utf-8') args[key] = val.encode('utf-8')
elif isinstance(val, str): elif isinstance(val, str):

15
songbook/plastex.py

@ -2,12 +2,25 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from plasTeX.TeX import TeX from plasTeX.TeX import TeX
from plasTeX.Base.LaTeX import Sentences
import codecs import codecs
import copy import copy
import locale import locale
import os import os
import sys 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): def simpleparse(text):
"""Parse a simple LaTeX string. """Parse a simple LaTeX string.
@ -15,7 +28,7 @@ def simpleparse(text):
tex = TeX() tex = TeX()
tex.input(text.decode('utf8')) tex.input(text.decode('utf8'))
doc = tex.parse() doc = tex.parse()
return doc.textContent return processUnbreakableSpace(doc.textContent)
class SongParser: class SongParser:
"""Analyseur syntaxique de fichiers .sg""" """Analyseur syntaxique de fichiers .sg"""

Loading…
Cancel
Save