GNU FriBidi is an implementation of the Unicode Bidirectional Algorithm (bidi). There is a Python binding PyFribidi, but it is not complete. What I need is not a visual presentation of a string, but information where direction is changing. This function is not provided by the binding, therefore I’ve made an alternative using ctypes.
License: public domain.
It was a chance for me to experiment with ctypes. With google and gdb, the success is archived. Don’t use my code as a source for inspiration. Most likely, some things can be done better.
s = u"......"
# Get the visual representation
s2 = ctfribidi.log2vis(s, 1)
# Mark direction changes
levels = ctfribidi.log2levels(s, 1)
s2 = ctfribidi.insert_markers(s, levels, 1)
The function log2levels (from fribidi) returns an array. Each member corresponds to a character of the source string and is the nesting direction level.
The Python code insert_markers uses the array to insert markers into the string. The markers are the characters:
So far I see, the nesting is correct. Each LRE and RLE is closed by PDF. But nothing promises this desired behaviour.
Inside the function one finds commented-out code to insert ZWNJ:
It was an attempt to solve a comma-position problem: http://tex.stackexchange.com/questions/214773/. For my needs, the problem was solved by other means, but maybe you still want the original attempt.