Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to output HTML code as text w/o rendering as HTML?

I want to see the code on the screen, not the interpreted code

         

MichaelBluejay

5:05 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd like my script to read an HTML file and then show the code in the browser. The problem is the browser interprets the code and displays it as a page. For example, I want to see:

<B><I>Boo boo puppy</I></B>

NOT:

Boo boo puppy

I tried changing the content-type to "text/plain" instead of "text/html" but that didn't help.

I tried printing out the Javascript code to print it out with a document.write but that fails when Javacript encounters a quote mark in the data which ends the string.

Surely there's a way to do this? Thanks for your help.

willybfriendly

5:19 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



&lt;tag&gt;

WBF

MichaelBluejay

6:56 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's the only way, to search & replace the <> signs? Surely there's got to be a way to tell the browser to just take the input and print it as text?

kaled

9:24 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this intended for personal use?
If you simply wish to view web pages as text there may be a simpler solution.

Kaled.

MichaelBluejay

10:10 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Um...does it matter whether it's for personal use or not?

This came about because I was writing an HTML optimizer to reduce my filesize after I uploaded pages, and I wanted to test it by looking at the code. But I found I couldn't actually look at the code, because when I output it to the page it got rendered as a web page. At that point I could choose View Source, but that seemed so...inelegant.

I know the &lt; works too, but I'm interested in that simple solution that I suspect must be out there! :)

adni18

1:19 pm on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you could try a <textarea> tag. They don't pick up tags.

saoi_jp

1:26 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



View source in the browser?

Or output the page with extension .txt instead of .html

rocknbil

3:09 pm on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's the only way, to search & replace the <> signs?

It's actually pretty easy. Stow all your output in a single variable, it doesn't hog that much memory:

$lt='&lt;';
$gt='&gt;';
#opening tag with or without a /, followed by anything
#not a >, followed by a >, global

$output =~ s/(<\/*)([^>])+(>)/$lt$2$gt/g;
$output = qq¦<PRE>$output</PRE>¦; #for line breaks
print "content-type:text/html\n\n";
print $out;

Probably can be done in a better way, but you get the idea. Exchange the double-line pipe in the qq for the qq separator of your choice.

MichaelBluejay

4:17 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



View source in the browser?

Yeah, but I mentioned that I'd rather not do this....

Or output the page with extension .txt instead of .html

Uh, when you're writing to the screen there is no extension. It's not a file.

you could try a <textarea> tag. They don't pick up tags.

Ah, this is exactly the kind of solution I was looking for. Thanks!

It's actually pretty easy. Stow all your output in a single variable, it doesn't hog that much memory:

Thanks, that's good to know. I'll add that to my database of tricks.

Thanks again everybody for your help.

Sanenet

4:20 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try the <PRE></PRE> tags

DrDoc

6:18 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<pre> won't work...


<pre><p>test</p>test</pre>

...would be rendered as...

test 
test

...instead of ...

<p>test</p>test


<textarea> is probably the best way of doing it, or converting the tags... I'd go for the latter, with <pre> wrapped around the whole thing.

moltar

11:16 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a big misconception that <textarea> does not require converting to HTML entities.

For example, if you want to include a <textarea> tag into your <textarea>. It will break the page.


[b]<textarea>[/b]

Test </textarea> Untest

[b]</textarea>[/b]

MichaelBluejay

1:43 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For example, if you want to include a <textarea> tag into your <textarea>. It will break the page.

I thought of that as soon as I saw the suggestion. Most of the code I'm outputting won't have a <TEXTAREA>, and when there is one I guess I'll search and replace it with something else.

Yeah, as long as I'm searching & replacing I guess I could just search & replace the <> tag markers and output the actual code instead of using <TEXTAREA>, but for some reason I'm drawn to the <TEXTAREA> solution.

DrDoc

1:48 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...or just output a text/plain document-type header...

bedlam

2:17 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Um, I know we're in the Perl forum, but no-one has suggested PHP's "htmlSpecialChars();"...

This:

$markup = "<html><head><title>Tester</title></head><body><p>Test!</p></body></html>";
echo htmlSpecialChars($markup);

...should output this:

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Tester&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;Test!&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

...if I'm not mistaken (minus the linebreaks which I put in to keep from breaking the page)...

-B

MichaelBluejay

9:36 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...or just output a text/plain document-type header...

Except I mentioned in my very first post that that didn't work.

rocknbil

6:12 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



as long as I'm searching & replacing I guess I could just search & replace the <> tag markers and output the actual code instead of using <TEXTAREA>, but for some reason I'm drawn to the <TEXTAREA> solution....

Michael re-check my post, it does both what DrDoc says (surrounds your output in <pre> tags) and does the s & r without error (I use it all the time.)

As for entities in a text area - many entities, in the format &label; or &#number;, will convert to their visual equivalent in a text area box. Some do and some don't. For example, &#091; resolves as a [ in a text area box. I know this because I'm dealing with this issue at the moment. :D

The point is if you output to a text area box, you may not be actually SEEING what you hope to see unless you view source. Using the pre tags will output literally.

MichaelBluejay

6:48 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah, I didn't notice that the code you supplied was also adding the <PRE></PRE> in addition to replacing the <> characters. Sorry.

I'm still drawn to the <TEXTAREA> solution, since it'll work on most of the pages I'm working with, and since I can pull it out of my head (unlike the search & replace solution, which would require that I go look it up, since I'd never remember the regular expressions).

DrDoc

12:33 am on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...or just output a text/plain document-type header...

Except I mentioned in my very first post that that didn't work.

Then there's something wrong with the perl script (or the browser)...

MichaelBluejay

4:55 am on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Then there's something wrong with the perl script (or the browser)...

Aha! Let me say that again: Aha!

I was starting to suspect that it might be a browser issue. I'd been testing in Safari. I just tried in Explorer and I get the HTML code just fine. I also found a workaround for Safari: If I remove the <HTML> tag from the very beginning, then Safari plays nice, too and prints just the code rather than interpreting it.

[blue]#!/usr/bin/perl

print "Content-type: text/plain\n\n";

print '<HTML><B><I>Boo boo puppy!</I></B></HTML>';
[/blue]

timster

8:48 pm on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you change the content type to "text/plain" and view the page in Firefox, that works correctly.

Edit: Oops just a little late.