Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Creating a <span> over a range while preserving nesting

Reply
Thread Tools

Creating a <span> over a range while preserving nesting

 
 
Burke Libbey
Guest
Posts: n/a
 
      08-10-2007
While this is a rails-related question, it has more to do with ruby in
general.

The user selects a range by highlighting it. This is detected with
javascript and fed to ruby. I want to take this range and highlight it.

I'm doing that by adding <span class="anno"></span> around the region.
So that works ok, except that this often causes tag nesting problems.
What I want to do is intelligently add more tag pairs to preserve
nesting.

For example, the range:

this is a</span> test <strong>that <span>uses pretty</span>
bizarre</strong> markup.

Should become:

<span class="anno">this is a</span></span><span class="anno"> test
<strong>that <span>uses pretty</span> bizarre</strong> markup.</span>

I'm not looking for someone to write the code for me (although if you
did, I wouldn't complain); I just need someone to point me in the right
direction. Should I start from scratch with RegExes, or is there a
suitable library?

Thanks.

/burke
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Konrad Meyer
Guest
Posts: n/a
 
      08-10-2007
--nextPart2155894.2EC8YW2PYK
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On Friday 10 August 2007 08:28:38 am Burke Libbey wrote:
> While this is a rails-related question, it has more to do with ruby in
> general.
>=20
> The user selects a range by highlighting it. This is detected with
> javascript and fed to ruby. I want to take this range and highlight it.
>=20
> I'm doing that by adding <span class=3D"anno"></span> around the region.
> So that works ok, except that this often causes tag nesting problems.
> What I want to do is intelligently add more tag pairs to preserve
> nesting.
>=20
> For example, the range:
>=20
> this is a</span> test <strong>that <span>uses pretty</span>
> bizarre</strong> markup.
>=20
> Should become:
>=20
> <span class=3D"anno">this is a</span></span><span class=3D"anno"> test
> <strong>that <span>uses pretty</span> bizarre</strong> markup.</span>
>=20
> I'm not looking for someone to write the code for me (although if you
> did, I wouldn't complain); I just need someone to point me in the right
> direction. Should I start from scratch with RegExes, or is there a
> suitable library?
>=20
> Thanks.
>=20
> /burke


You might look into Hpricot by _why or stdlib REXML.

=2D-=20
Konrad Meyer <> http://konrad.sobertillnoon.com/

--nextPart2155894.2EC8YW2PYK
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBGvO9vCHB0oCiR2cwRAjRvAKC0XGz7fLow6ng8Ko61fa v4LRee8wCcDqed
jR02QbJe5zIziT+4DgTD2MU=
=mA1c
-----END PGP SIGNATURE-----

--nextPart2155894.2EC8YW2PYK--

 
Reply With Quote
 
 
 
 
Kenneth McDonald
Guest
Posts: n/a
 
      08-11-2007
The easy way to do this is to annotate _all text runs_ in the selected
area; don't try to figure
out if other tags are partly included or not.

In this example, I'll use <A: to mean <span class="anno"> and :A> to
mean a </span> that
closes an "anno" span. <B and B>, <C and C>, etc, stand for arbitrary
other tags. All text
is in lower case.

So, your example is

this is a:B> test <C:that <D:uses pretty>
bizarre:C> markup.

your example of what you wanted then can be written as:

<A:this is a:A>:B><A: test <C:that <D:uses pretty>
bizarre:C> markup.:A>

This is difficult because it requires some amount of parsing to figure out
what markup is 'complete' (both start and end tags present) and what isn't
complete, then various processing to take that info into account. It's a pain
Instead, if you can accept markup like this:

<A:this is a:A>:B><A: test :A><C:<A:that :A><D:<A:uses pretty:A>><A:bizarre:A>:C><A: markup.:A>

Then the algorithm for marking up the selection is simple; just find all
pieces of "real" text in selection
(i.e. not start or end tags), and wrap those pieces in <A: and :A>. This
is very easy to do with regular
expressions.

A potential problem is that you may end up with 'gaps' in your
highlighting. On the other hand,
you're less likely to run into weird CSS problems.

Cheers,
Ken

Burke Libbey wrote:
> While this is a rails-related question, it has more to do with ruby in
> general.
>
> The user selects a range by highlighting it. This is detected with
> javascript and fed to ruby. I want to take this range and highlight it.
>
> I'm doing that by adding <span class="anno"></span> around the region.
> So that works ok, except that this often causes tag nesting problems.
> What I want to do is intelligently add more tag pairs to preserve
> nesting.
>
> For example, the range:
>
> this is a</span> test <strong>that <span>uses pretty</span>
> bizarre</strong> markup.
>
> Should become:
>
> <span class="anno">this is a</span></span><span class="anno"> test
> <strong>that <span>uses pretty</span> bizarre</strong> markup.</span>
>
> I'm not looking for someone to write the code for me (although if you
> did, I wouldn't complain); I just need someone to point me in the right
> direction. Should I start from scratch with RegExes, or is there a
> suitable library?
>
> Thanks.
>
> /burke
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Sign preserving Vs value preserving sophia.agnes@gmail.com C Programming 4 12-07-2007 03:14 PM
Module.nesting -> Kernel#nesting Trans Ruby 10 09-16-2005 12:21 AM
integral promotion, arithmetic conversion, value preserving, unsigned preserving??? TTroy C Programming 16 01-31-2005 10:20 PM
How to backup a directory while preserving the modified date? JD Computer Support 1 09-16-2004 08:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57