2011-03-08 91 views
1

我正在为项目使用双向文本(混合英语和希伯来语)。文本以HTML格式显示,因此有时需要使用LTR或RTL标记(‎‏)才能使“弱字符”正确显示为标点符号。由于技术限制,这些标记在源文本中不存在,所以我们需要添加它们以使最终显示的文本显示正确。自动插入LTR标记

例如,以下文本:(example: מדגם) sample呈现为从右到左模式的sample (מדגם :example)。正确的字符串看起来像‎(example:‎ מדגם) sample,并将呈现为sample (מדגם (example:

我们希望快速插入这些标记,而不是重新编写所有文本。起初,这似乎很简单:只需将‎附加到标点符号的每个实例。但是,一些需要动态修改的文本包含HTML和CSS。造成这种情况的原因是不幸的,也是不可避免的。

解析HTML/CSS的不足之处,是否有一种已知算法用于即时插入Unicode定向标记(伪强字符)?

回答

1

我不知道如何在不解析它​​的情况下安全地将方向标记插入到HTML字符串中的算法。将HTML解析为DOM并操作文本节点是确保您不会不小心向<script><style>标记中的文本添加方向标记的最安全方法。

这是一个简短的Python脚本,它可以帮助您自动转换文件。如果需要,逻辑应该很容易翻译成其他语言。我不是你想的编码规则RTL不够熟悉,但你可以调整正则表达式'(\W([^\W]+)(\W)'和substituion模式ur"\u200e\1\2\3\u200e",让您预期的结果:

import re 
import lxml.html 

_RE_REPLACE = re.compile('(\W)([^\W]+)(\W)', re.M) 

def _replace(text): 
    if not text: 
     return text 
    return _RE_REPLACE.sub(ur'\u200e\1\2\3\u200e', text) 

text = u''' 
<html><body> 
    <div>sample (\u05de\u05d3\u05d2\u05dd :example)</div> 
    <script type="text/javascript">var foo = "ignore this";</script> 
    <style type="text/css">div { font-size: 18px; }</style> 
</body></html> 
''' 

# convert the text into an html dom 
tree = lxml.html.fromstring(text) 
body = tree.find('body') 
# iterate over all children of <body> tag 
for node in body.iterdescendants(): 
    # transform text with trails after the current html tag 
    node.tail = _replace(node.tail) 
    # ignore text inside script and style tags 
    if node.tag in ('script','style'): 
     continue 
    # transform text inside the current html tag 
    node.text = _replace(node.text) 

# render the modified tree back to html 
print lxml.html.tostring(tree) 

输出:

python convert.py 

<html><body> 
    <div>sample (&#1502;&#1491;&#1490;&#1501; &#8206;:example)&#8206;</div> 
    <script type="text/javascript">var foo = "ignore this";</script> 
    <style type="text/css">div { font-size: 18px; }</style> 
</body></html> 
+0

一让这变得更加困难的事情是破坏了HTML,但一个宽容的解析器可以帮助解决这个问题。对于这个应用程序,我们实际上使用HTML片段,因此解析是粗略的。真正的解决方案是在流程的早期推动变革。 – 2011-03-11 15:49:15