I’m a mostly-newbie when it comes to web development (though not to programming in general) so pardon any incorrect terminology.
I want to build a script that, when added to an HTML page, detects each Hebrew word in the page and transforms that word into an HTML element, e.g. into a hyperlink with title.
So, the following:
<p>ראש הלשכה</p>
Is transformed into:
<p><a title="word 1" href="#">הלשכה</a> <a title="word 2" href="#">ראש</a></p>
Make sense?
So, I suppose the first order of business is detecting Hebrew words in a page. How would I go about doing this? I don’t know where to start, outside of poking around jQuery documentation.
Searching for a Hebrew word in a string is fairly simple. Use a regexp that matches a contiguous sequence of Hebrew code points:
Since JS supports functional programming, we can easily write our own functions to walk the document tree, calling a function on each text node. First, a bit of scaffolding.
Next, we define a method to split strings into an array, including separators in the output.
Next, some node predicates.
Now the functions to walk the DOM.
The last group of functions replace text in a text node that matches a pattern with a new node of your choosing. This group (well, the function returned by
wrapText) hasn’t been completely tested for cross-browser compatibility, including whether it handles text direction properly.The last thing you have to do is start the process in (e.g.) a load handler or a script at the bottom of the page.
If you want to change the prefix for the title, pass the result of
createWordCounter(...)to thecreateAnchorWrap.