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
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):

15
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"""

Loading…
Cancel
Save