Thread: Adding HTML tags

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007

    Adding HTML tags


    Small question. Has anyone come across an elegant way of adding HTML to text based on ranges?

    My problem is simple, I have a string of text and several ranges (which can overlap), when the ranges overlap different styling is to be applied, otherwise the ranges are simply styled (i.e. <b> or <u> tags). But the problem is, if I insert tags at the start and the end of the ranges, then the other ranges will no longer be correct.

    Currently my approach is to build a list of the "points of interest", i.e. where ranges start or end. And work through it backwards, checking if the given point is in each of the ranges. If it is, then I apply the appropriate style. But this is cumbersome and ugly, as I have to check several different lists of ranges for each given point.

    Any ideas?

  2. #2
    Registered User
    Join Date
    Aug 2010
    I didn't get deeply into your problem.

    Assuming each letter can have a different styling, what about an array of style for each letter:

    0 = no style
    1 = only bold
    2 = only underline

    [ 0, 0, 0, 0, 0, 0, 0, 0 ]

    Now, sort all the ranges ascending (range 1,4 means applying style to indices 1, 2, 3)
    You compare only the starting index, and now:
    Given sorted ranges:
    0,4 - only bold
    2,7 - only underline

    You just start filling.
    Each next filling applied will overwrite the previous one.

    So after applying the first styler:
    [ 1, 1, 1, 1, 0, 0, 0, 0 ]
    After applying the second one:
    [ 1, 1, 2, 2, 2, 2, 0, 0 ]

    (create more arrays with complex styles, use masks etc)

    Then you iterate through the array and check where the values change. If they change, you append the appropriate HTML tags, otherwise, push back the letter (all to the separate string).

    Is this what you mean? Hope this helps.


    But the problem is, if I insert tags at the start and the end of the ranges, then the other ranges will no longer be correct.
    They will be correct, because they will overwrite the global style, this is why you need to sort the ranges.
    Last edited by kmdv; 08-25-2010 at 07:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HTML tags validator
    By Terrorist in forum C Programming
    Replies: 24
    Last Post: 05-09-2008, 02:14 AM
  2. Library which extract html tags content
    By Bargi in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2007, 10:17 PM
  3. Help reading file and adding html tags
    By enhancedmode in forum C Programming
    Replies: 3
    Last Post: 05-30-2005, 03:02 PM
  4. Stacks, classes, HTML tags, and parsing.
    By Shinobi-wan in forum C++ Programming
    Replies: 5
    Last Post: 10-01-2003, 05:50 PM
  5. HTML tags
    By netboy in forum A Brief History of
    Replies: 4
    Last Post: 03-27-2002, 07:52 AM