Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4043524
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:08:16+00:00 2026-05-20T13:08:16+00:00

I am working with bidirectional text (mixed English and Hebrew) for a project. The

  • 0

I am working with bidirectional text (mixed English and Hebrew) for a project. The text is displayed in HTML, so sometimes a LTR or RTL mark (‎ or ‏) is required to make ‘weak characters’ like punctuation display properly. These marks are not present in the source text due to technical limitations, so we need to add them in order for the final displayed text to appear correct.

For instance, the following text: (example: מדגם) sample renders as sample (מדגם :example) in right-to-left mode. The corrected string would look like ‎(example:‎ מדגם) sample and would render as sample (מדגם (example:.

We’d like to do on-the-fly insertion of these marks rather than re-authoring all the text. At first this seems simple: just append an ‎ to each instance of punctuation. However, some of the text that needs to get modified on-the-fly contains HTML and CSS. The reasons for this are unfortunate and unavoidable.

Short of parsing HTML/CSS, is there a known algorithm for on-the-fly insertion of Unicode directional marks (pseudo-strong characters)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:08:16+00:00Added an answer on May 20, 2026 at 1:08 pm

    I don’t know of an algorithm to insert directional marks into an HTML string safely without parsing it. Parsing the HTML into a DOM and manipulating the text nodes is the safest way of ensuring you don’t accidentally add directional marks to text inside <script> and <style> tags.

    Here is a short Python script which might help you transform your files automatically. The logic should be easy to translate into other languages if necessary. I’m not familiar enough with the RTL rules you’re trying to encode, but you can tweak the regexp '(\W([^\W]+)(\W)' and substituion pattern ur"\u200e\1\2\3\u200e" to get your expected result:

    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)
    

    Output:

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on a project at the moment and we have to implement soft deletion
Working on a project that parses a log of events, and then updates a
Working with python interactively, it's sometimes necessary to display a result which is some
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
Working on calling a C function from my asm project. I'm trying to push
Working on an old Kohana 2 project and I want to link two models.
I'm having some issues getting a bidirectional one-to-many association working with JoinTables. This is
Working on a maintenance project, I am struggling with lot of messy code. In
Working on dom html . I want to convert node value to string: $html
Working on a end of the year project for school and chose to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.