Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

<pre> vs. <code> tags

by almut (Canon)
on Apr 25, 2008 at 22:56 UTC ( #682950=monkdiscuss: print w/ replies, xml ) Need Help??

Yes, I've read Writeup Formatting Tips and Perl Monks Approved HTML tags and I'm well aware that <pre> tags are discouraged here, in general — for a number of good reasons which I do appreciate and normally abide by.

Still, occasionally, I use <pre> tags in place of <code> tags, because the former allow you to embed HTML markup, such as <b> or <i>, which will be honored when the <pre>-formatted section is rendered. IMHO, this does have its uses in a few selected cases, e.g. to highlight specific fragments in some program output, in order to make things easier to read.  As far as I know, this is not possible within <code> sections (at least, I wouldn't know how).

With this reasoning in the back of my mind, I did make use of <pre> tags and <b>/bold highlighting in a recent node to have some debug output be rendered something like this:

...
Just used Second in Data
Methods are exported from First
First::import(): @EXPORT_OK = ...
After using First in Second
...

And that's what it looked like initially when I had posted the node. After having reloaded it about half an hour later, however, it looked like this

... Just used Second in Data Methods are exported from First <b>First::import()</b>: @EXPORT_OK = ... After using First in Second ...

apparently because someone had replaced the <pre> tags with <c> tags  (or do we in the meantime have an automatic mechanism which periodically goes through all nodes to replace any evil <pre> tags? — if so, I missed it being announced on What's new.)

Okay, not a big issue. But what particularly surprised me was the relatively short time frame within which this happened. Otherwise, it often takes a whole day or longer until considered nodes are finally being fixed, even if they display as some illegible blob of text and code all mixed up in one paragraph.

So I'm wondering what sin I have committed that made immediate action necessary :) — while the parent node, for example - which has been considered for not using code tags - is still waiting for some benevolent janitor's intervention at the time of this writing...

It can hardly have been the line length, because the longest line in the snippet in question is just 69 characters. Also, I can't imagine why anyone would want to one-click-download some dull debug output, or have the snippet be decorated with the line numbering that <code> tags optionally offer.

So, to recap, is there anything unconditionally wrong with using <pre> tags? If so, why not disable them entirely in the first place?

Comment on <pre> vs. <code> tags
Select or Download Code
Re: <pre> vs. <code> tags
by CountZero (Bishop) on Apr 25, 2008 at 23:17 UTC
    <pre>-tags do not automatically break long lines.

    Hence they tend to mess up your display.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I'm aware of that, but there were no long lines in that particular case. Of course, it's a matter of definition what is "long"... but as a rule of thumb, I'd consider anything less than 72 characters as acceptable (as traditionally being recommended for mail or news messages, for example).

        No, you can't assume that 72 characters isn't "too wide". My rule of thumb for that is more like 24 characters. Even 24 characters can be "too wide" in cases that I run into, but that is usually only on smaller-than-typical devices so I don't sweat it too much1. With nodelets and thread indenting and other things, it is quite easy for 60 characters to be "too wide" (no, my browser window is rarely maximized).

        1 I do prefer that PerlMonks renders reasonably on smaller-than-typical devices and am happy to find that PerlMonks actually does a better job of this than most web sites I run into. I don't mind when the rare <pre> section makes the site a bit too wide for a small-display client. But a 50-character line in a <pre> section can make a thread nearly impossible to read on a smaller device's browser.

        It wouldn't be particularly hard to parse <pre> blocks and re-render them as things that use fixed-width fonts and preserve whitespace but also are willing to wrap. That way we'd have something a lot like <code> but that allows HTML inside of it. And we wouldn't have to beat people up to avoid using <pre> tags. Not that this will be implemented this month.

        - tye        

Re: <pre> vs. <code> tags
by Arunbear (Parson) on Apr 26, 2008 at 09:10 UTC
    If you've enabled the Approval Nodelet, you can click on the node history link to see what janitorial edits were made. For the node you mention, there were no janitorial edits, which suggests that one of the gods made the edit.
      which suggests that one of the gods made the edit

      That would suck. Even if a god makes an edit, they should make it via the janitorial process to provide proper accountability. I'll mention this to them directly. Thanks.

      - tye        

        That was me, and it was an accident.

Re: <pre> vs. <code> tags
by chromatic (Archbishop) on Apr 26, 2008 at 16:16 UTC

    <pre> tags are four points smaller than <code> tags, so they're too hard to read. Please don't use them.

      <pre> tags are four points smaller than <code> tags

      Sounds like a (personal?) CSS problem. For reference, the following are the same size for me in this browser:

      Pre tags are THIS big
      
      Code tags are THIS big

      - tye        

        I haven't changed my site display CSS (except for blocking a couple trolls) and the pre is much larger than the code in Firefox and Safari on OS X. Gotta love HTML. The font size below is the culprit, I think-

        <pre>Pre tags are THIS big </pre> <pre class="code"> <div class="codeblock"> <tt class="codetext"> <font size="-1">Code tags are THIS big </font> </tt> </div>

        Update/amendment: they do display the same threaded beneath the OP but not when viewed singly.

Re: <pre> vs. <code> tags
by naikonta (Curate) on Apr 29, 2008 at 03:32 UTC
    There is some reason to keep the <pre> tags around and I think you just bring it up the good reasons for it. One feature distincts both tags is download link. With <code> tags, the part surrounded is downloadable.

    With <code>

    #!/usr/bin/perl use strict; use warnings; use DBI; # code following __END__

    With <pre>

    #!/usr/bin/perl
    
    use strict;    # don't ever write
    use warnings;  # without strictures
    use DBI;
    
    # code following
    __END__
    

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: <pre> vs. <code> tags
by John M. Dlugosz (Monsignor) on May 02, 2008 at 04:05 UTC
    I just had a note on a similar issue. I posted a code sample with a π character, and the preview did not work right because the code was double-escaped. I changed to pre. I hope someone doesn't break it after I posted it!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: monkdiscuss [id://682950]
Approved by grep
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-09-20 20:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (161 votes), past polls