Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

programatically coloring code displayed on web pages... 3

Status
Not open for further replies.

BigTeeJay

Technical User
Jun 13, 2001
106
US
Does anyone know of a way of doing this? Has any one run
across any ASP / Javascript component/function/etc that
would allow you to process user supplied code (HTML,
Javascript, ASP, etc) and have the output be displayed as
colored code?...

ie:
<html>
<body>

Some text
<!-- some comments -->

I am trying to write an instructional page and thought
it would be nice if I could address this in a far easier
method than checking by hand (or some other incredibly
unreasonable method).

I do have ASP available, but do not have PHP/Javascript
(or much else really).

Your help is much appreciated,
Tj
 
Hi,
Have not come across one, but I think you should be able to build a simple one quickly:
============================================
'==================================================
'Function: colorCode( string ) string
' Return : a color coded HTML display ready string.
'==================================================
function colorCode( vsUserInput )
dim vasTagColor, vasTag, vsOutput

'----------------------------------------------
' Define all the tags you want to convert here.
' Note: The longest tag MUST be on top.
'----------------------------------------------
vasTagColor = array( _
array( &quot;<!--&quot;, &quot;<font color='green'>&quot; & server.HTMLEncode(&quot;<!--&quot;) ), _
array( &quot;-->&quot;, &quot;</font>&quot; & server.HTMLEncode(&quot;-->&quot;) ), _
array( &quot;<&quot;, &quot;<font color='blue'>&quot; & server.HTMLEncode(&quot;<&quot;) ), _
array( &quot;>&quot;, &quot;</font>&quot; & server.HTMLEncode(&quot;>&quot;) ) _
)

'---------------------------------------------
' Loop through all the tags to convert
' and color code it. Also convert the HTML
' codes into non-parsable characters
'---------------------------------------------
vsOutput = vsUserInput
for each vasTag in vasTagColor
vsOutput = replace( vsOutput, vaTag(0), vaTag(1) )
next
colorCode= vsOutput
end function


Response.Write( colorCode( &quot;<html><body>My stuff here <!-- some comment --></body></html>&quot; ) )

============================================

I wrote this on the fly, hope there is no bug here ... but once you get the logic, it should be easy to elaborate/repair on.

regards,
- Joseph ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Visit --> for (Replica) Watches, Pen, Handbags, Hats, Jerseys and more ... at prices that makes your code spin ...
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
 
I started writing a function not to long ago to do this. Unfortunatly as I did not have time to finish it to my satisfaction it hasn't been posted anywhere. Depending on how complicated you want to get, this can be a quick job or a slightly less than quick job.
Basically the above statement will get you on your way, but it will need to be extended a little.
I assume for the following that you have the html stored in a single string (like from a db) and that you will have mixed html and ASP code
1. Define a a startPosition and endPosition variable
2. startPosition = InStr(yourPage,&quot;<&quot;)
3. while startPosition > 0
4. endPosition = InStr(startPosition,&quot;>&quot;
5. if the character at start position + 1 is a % (<%) or a ! (<!--) move on
6. else replace by grabbing left to startPosition & font tag & mid from startPosition to endPosition - startPosition & end font tag & right from end position to len() - endPosition
7. Reset your counter for the next loop
8. Repeat loop logic for HTML comments
9. Loop for ASP comments (use REM instead of ')
your end position will be the next vbCRLF that you find (end of line)
10. Now replace all vbCrLf's with <br> tags so it will display correctly on the web page.

After doing the above you can get more complicated if you like and start coloring certain portion of the ASP. You will want to split this off into another function because you wouldn't want to accidentally color something in the HTML as ASP.
For each string you find that starts with &lt;% and ends with %&gt; send it to the function:
For each line with quotes on it, get a pair of start and end quotes, if the endquotes + 1 is also a quote (&quot;&quot;) get the next one after the pair (Instr(endquotes+2,&quot;&quot;&quot;&quot;)). Add pink to both ends, shove it back in the string, continue to next.
Now create an array of words that you want to be red in the ASP. For each word in the array do a replace that will replace all occurences of it with font color=red and the tag name and /font.
Now you have one long string of ASP code that is color coded, before you return it, concatenate on the front and the end of the the string another font tags to make all non-colored text appear a certain base color (I prefer brown for my ASP). Return it

Your main function just finished getting back the last ASP string in it's loop. All done.

Hope that helps you plan out the code a little better. I am sure there are other ways to do this, this was just mine :)

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Thanks for all your replies... I also was given a link that
pointed to...

...which seems like it (might) do what I want as well.

krisbrixon:
Thanks for the link... I checked it out, and since I didnt
originally plan on having my code saved as separate files
(I was just going to display it in the webpage that was
teaching the relating concept) that wouldn't originally
work (unless there is some way, other than a frame to
utilize this... I cant think of any way to to a src
attribute to display text within a page other than via a
frame). But it is nice to have an option in case the other
ideas dont work out (and I will just have to change my
layout a little to use the frames, and have the code shown
in the frame).

fhlee & Tarwn:
Thats what I was thinking I would have to do... and hoping
to be able to avoid it (considering all the tags I would
have to account for). Thanks for the quick example though
should I need to/choose to pursue that method.

Thanks & Regards to all... I appreciated your thoughts &
input.

Tj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top