472,129 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,129 software developers and data experts.

How to make html ignore code that is part of text?

I need to include code snippets in my html document, something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" "Password=password;Persist Security
Info=True;User ID=newuser;Initial Catalog=catalog;Data Source=localhost" />
</appSettings>
</configuration>

If I try to put this in a regular document, I get errors... I've tried
enclosing it in <pre> tags, but that didn't help. How do I get this code
sample into a document?

Thanks
Jul 23 '05 #1
18 48089
deko wrote:
I need to include code snippets in my html document, something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" "Password=password;Persist Security
Info=True;User ID=newuser;Initial Catalog=catalog;Data Source=localhost" />
</appSettings>
</configuration>

If I try to put this in a regular document, I get errors... I've tried
enclosing it in <pre> tags, but that didn't help. How do I get this code
sample into a document?


Change the less-than signs ("<") to "&lt;", the ampersands ("&") to
"&amp;", and the greater-than signs (">") to "&gt;".
Jul 23 '05 #2
deko wrote:
I need to include code snippets in my html document


You need to use &lt; and &gt; instead of < and >.

See http://tranchant.plus.com/web/html-tutorial/characters

--
Mark.
http://tranchant.plus.com/
Jul 23 '05 #3
> Change the less-than signs ("<") to "&lt;", the ampersands ("&") to
"&amp;", and the greater-than signs (">") to "&gt;".


Thanks!!
Jul 23 '05 #4
> See http://tranchant.plus.com/web/html-tutorial/characters

very helpful link... thanks!
Jul 23 '05 #5
"deko" <ww*******************************@nospam.com> wrote:
I need to include code snippets in my html document, something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" "Password=password;Persist Security
Info=True;User ID=newuser;Initial Catalog=catalog;Data Source=localhost" />
</appSettings>
</configuration>

If I try to put this in a regular document, I get errors... I've tried
enclosing it in <pre> tags, but that didn't help. How do I get this code
sample into a document?


Replace occurrences of < with &lt; , for consistency you might also
consider replacing > with &gt;

Then enclose it in:

<pre><code>
....
</code></pre>

markup.

--
Spartanicus
Jul 23 '05 #6
> Replace occurrences of < with &lt; , for consistency you might also
consider replacing > with &gt;

Then enclose it in:

<pre><code>
...
</code></pre>

markup.


That did the trick:

<pre>
<code>
&lt?xml version="1.0" encoding="utf-8" ?&gt;
&ltconfiguration&gt
&ltappSettings&gt
&ltadd key="ConnectionString" "Password=password;Persist Security
Info=True;User ID=newuser;Initial Catalog=database;Data Source=localhost"
/&gt
&lt/appSettings&gt
&lt/configuration&gt
</code>
</pre>

looks great... thanks
Jul 23 '05 #7
"deko" <ww*******************************@nospam.com> wrote:
Replace occurrences of < with &lt; , for consistency you might also
consider replacing > with &gt;

Then enclose it in:

<pre><code>
...
</code></pre>

markup.
That did the trick:


Consider that an illusion.
<pre>
<code>
&lt?xml version="1.0" encoding="utf-8" ?&gt;
&ltconfiguration&gt
&ltappSettings&gt
&ltadd key="ConnectionString" "Password=password;Persist Security
Info=True;User ID=newuser;Initial Catalog=database;Data
Source=localhost" /&gt
&lt/appSettings&gt
&lt/configuration&gt
</code>
</pre>


You are not using the semicolon as instructed. Terminating entity
references with semicolon has always been good practice in HTML, and in
XHTML it is obligatory - an XHTML conformant browser is _required_ to
choke on your usage. Besides, e.g. &ltadd is invalid in good old HTML
too, and is to be treated as an undefined entity reference.

As a minor detail, it is a bit safer to write

<pre><code>&lt;?xml version="1.0" ...
...
&lt;/configuration&gt;</code></pre>

i.e. to have no line breaks after <pre> and <code> tags or before </code>
or </pre> tags. The reason is that many browsers get the relevant parsing
rules wrong and may understand the situation so that there is an empty
line at the start of the <pre> element and at the end of it. You might
see this if you set a background color or a border for the <pre> element.

--
Yucca, http://www.cs.tut.fi/~jkorpela/
Pages about Web authoring: http://www.cs.tut.fi/~jkorpela/www.html

Jul 23 '05 #8
In our last episode,
<dA******************@newssvr29.news.prodigy.com >,
the lovely and talented deko
broadcast on comp.infosystems.www.authoring.html:
I need to include code snippets in my html document, something like this: <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" "Password=password;Persist Security
Info=True;User ID=newuser;Initial Catalog=catalog;Data Source=localhost" />
</appSettings>
</configuration> If I try to put this in a regular document, I get errors... I've tried
enclosing it in <pre> tags, but that didn't help. How do I get this code
sample into a document?


Change < to &lt; and > to &gt;


--
Lars Eighner -finger for geek code- ei*****@io.com http://www.io.com/~eighner/
If it wasn't for muscle spasms, I wouldn't get any exercise at all.
Jul 23 '05 #9
On Tue, 07 Sep 2004 07:38:49 +0000, deko wrote:
I need to include code snippets in my html document, something like this:

<?xml version="1.0" encoding="utf-8" ?> <configuration>
<appSettings>
<add key="ConnectionString" "Password=password;Persist Security
Info=True;User ID=newuser;Initial Catalog=catalog;Data Source=localhost"
/>
</appSettings>
</configuration>

If I try to put this in a regular document, I get errors...
What kind of errors? when? Do you mean it no longer validates as strict
HTML 4 or XHTML?
I've tried enclosing it in <pre> tags, but that didn't help. How do I
get this code sample into a document?


To display code snippets, encode the angle brackets and singe and double
quotes. Formatting can be improved by wrapping it with "<pre><code>"
(<pre> to preserve white space, <code> to use a monospace font).

HTH,
La'ie Techie

Jul 23 '05 #10
On Wed, 08 Sep 2004 01:10:24 -0600, Lāʻie Techie
<laie@win_remove_get_nospam_solutions.com> declared in
comp.infosystems.www.authoring.html:
(<pre> to preserve white space, <code> to use a monospace font).


That depends on the browser, the CSS and the user's settings (not
necessarily in that order).

pre { font-family: monospace; }
code { font-family: serif; }

--
Mark Parnell
http://www.clarkecomputers.com.au
Jul 23 '05 #11
L??ie Techie <laie@win_remove_get_nospam_solutions.com> wrote:
<code> to use a monospace font).


Use <code> to markup code, not for presentational purposes.

--
Spartanicus
Jul 23 '05 #12
On Tue, 07 Sep 2004 09:15:18 +0100, Spartanicus <me@privacy.net> wrote:
"deko" <ww*******************************@nospam.com> wrote:
I need to include code snippets in my html document...

[...]
Replace occurrences of < with &lt; , for consistency you might also
consider replacing > with &gt; Then enclose it in:

<pre><code>
...
</code></pre>


It seems more correct to me to use the following markup

<code>
<pre>...</pre>
</code>

where the '...' sequence represents the exact preformatted code element
content, with entity references included where required.

After all the OP asked for how to include a CODE section in a way that
it would be rendered as an example of markup.

The 'CODE' element has a semantic definition attached to it and should
thus be at the major level of markup in relation to 'PRE'.

A 'PRE' element has no specific semantics attached to it, and it is in
my eyes only presentational. It should as such only be involved at the
end of any branch in a parse tree.

--
Rex
Jul 23 '05 #13
Jan Roland Eriksson <jr****@newsguy.com> wrote:
On Tue, 07 Sep 2004 09:15:18 +0100, Spartanicus <me@privacy.net> wrote:
"deko" <ww*******************************@nospam.com> wrote:
I need to include code snippets in my html document...
<pre><code>
...
</code></pre>


It seems more correct to me to use the following markup

<code>
<pre>...</pre>
</code>


Shame it isn't valid. :-/
The 'CODE' element has a semantic definition attached to it and should
thus be at the major level of markup in relation to 'PRE'.
Unfortunately code is an inline element ...
A 'PRE' element has no specific semantics attached to it, and it is in
my eyes only presentational. It should as such only be involved at the
end of any branch in a parse tree.


.... and pre is a block element.

Steve

--
"My theories appal you, my heresies outrage you,
I never answer letters and you don't like my tie." - The Doctor

Steve Pugh <st***@pugh.net> <http://steve.pugh.net/>
Jul 23 '05 #14
On Wed, 08 Sep 2004 01:10:24 -0600, Lāʻie Techie
<laie@win_remove_get_nospam_solutions.com> wrote:
To display code snippets, encode the angle brackets and singe and double
quotes. Formatting can be improved by wrapping it with "<pre><code>"
(<pre> to preserve white space, <code> to use a monospace font).


pre = block level element which preserves white space. Many browsers'
default rendering is monospace.

code = inline element which semantically defines an example of code. Many
browsers' default rendering is monospace.

Neither should be chosen to force a monospace font. CSS (selector
{font-family: monospace;}) should be used for that purpose.
Jul 23 '05 #15
On Wed, 08 Sep 2004 16:29:01 -0400, Neal <ne*****@yahoo.com> wrote:
Neither (pre or code) should be chosen to force a monospace font. CSS
(selector {font-family: monospace;}) should be used for that purpose.


And of course, user CSS or a no-CSS UA can make that suggestion moot...
Jul 23 '05 #16
Neal <ne*****@yahoo.com> wrote:
force


Who's a naughty boy then? ;-)

--
Spartanicus
Jul 23 '05 #17
On Wed, 08 Sep 2004 21:22:15 +0100, Steve Pugh <st***@pugh.net> wrote:
Jan Roland Eriksson <jr****@newsguy.com> wrote:

[...]
It seems more correct to me to use the following markup
<code>
<pre>...</pre>
</code>


Shame it isn't valid. :-/


Yes I know that, sadly I stopped writing before I had indicated whatever
implications that was attached to my line of thought.

I still stand for my line of thinking as described though. PRE is
presentational and should in reality have been defined as an "inline
element.

--
Rex
Jul 23 '05 #18
On Thu, 09 Sep 2004 00:37:53 +0100, Spartanicus <me@privacy.net> wrote:
Neal <ne*****@yahoo.com> wrote:
force


Who's a naughty boy then? ;-)

Sorry, it just happened! I promise I won't do it again!

(No, my girlfriend didn't buy it either...)
Jul 23 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Martin Ernst | last post: by
10 posts views Thread by st4 | last post: by
2 posts views Thread by Tor Inge Rislaa | last post: by
2 posts views Thread by ZBINContact | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.