How do I put </code> into a multi-line <CODE></CODE> block? Example:
#!/usr/bin/perl use strict; print "Put your code in <CODE></
CODE> tags!\n"; How do I stop it breaking? Hint: I get the </code> to display by putting </CODE><CODE> in between the </ and the CODE>.

/me is glad that he wrote his code wrapping script, but he might amend it to automatically fix </code> in the script rather than manually.

Replies are listed 'Best First'.
Re: Code that contains </code> (easy)
by tye (Sage) on Aug 05, 2003 at 21:23 UTC

    This is Perl. The number of ways to do it is huge:

    print "Use </", "code>"; print "Use <\/code>"; print "Use </c$,ode>"; print "Use </"."code>"; my $code="code"; print "Use </$code>"; ...

                    - tye

      Mr. Muskrat smacks his forehead with the palm of his hand

      It's so cruel! The obvious answers elude me today.

      I don't want to change the actual content of the code just so that it displays ok in a Perl Monks writeup! Lets try <pre> tags instead of <code>...
      
      #!/usr/bin/perl
      use strict;
      print "Put your code in <CODE></CODE> tags!\n";
      
      OK that works, but you have to do something to the tags (at first I used <i></i>, then changed to &lt; at aristotle's suggestion).
        That's not even necessary. If you're using <pre>, you can just change one of the angle brackets to its entity name (like &lt;) and PM will obligingly look past the <code></code> tags. But using <pre> means your code/output will not be autowrapped, nor will it be easily downloadable, and you have to manually escape any PM approved HTML tags as well as any of your ampersands that look like entities.

        Makeshifts last the longest.

        You don't want to change the code so you prefer to change the < (in the code) to &lt;?? And how is this not changing the code? A smaller change would be to add a \ before the / or the >.

        And you aren't bothered that people can't download the code simply any longer? If you end up with even slightly long lines in your code, then others certainly will be bothered by you using PRE tags instead of CODE tags.

                        - tye
Re: Code that contains </code>
by jeffa (Bishop) on Aug 06, 2003 at 14:28 UTC
    Most of the time, i can get away with using <pre> tags and escaping < like so:
    <pre> #!/usr/bin/perl use strict; print "Put your code in &lt;CODE>&lt;/CODE> tags!\n"; </pre>
    This yields:
    #!/usr/bin/perl
    use strict;
    print "Put your code in <CODE></CODE> tags!\n";
    
    It's not an ideal solution, but if your snippet is small enough, it'll get ya by. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
•Re: Code that contains </code>
by merlyn (Sage) on Aug 05, 2003 at 21:42 UTC
    Simple:
    print "</co</co
    de><code>de>"; update: Or at least, that should have worked, but a /code..code pair inserts extra blank lines for me. Stupid rendering engine. Where do I point at to fix that? The HTML looks like:
    <pre><tt class="code"><font size="-1"> print &quot;&lt;/co&lt;/co </font></tt></pre> <tt><font size="-1">de&gt;&lt;co</font></tt><pre><tt class="code"><fon +t size="-1">de&gt;de&gt;&quot;; </font></tt></pre>
    so it looks like the Everything2 engine is adding extra newlines inside the PRE element. Bad. I was trying to print:
    print "</co</code><code>de>";

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      so it looks like the Everything2 engine is adding extra newlines inside the PRE element.

      What you're seeing is the rather poorly documented behavior of <pre> when there's a newline somewhere between the tags.

      No, that's me on crack dust.

        Just to be clear, PRE and P are "block" tags so if I use

        <pre>line1\nli</pre><pre>ne2\nline3</pre>
        or
        <p>line1\nli</p><p>ne2\nline3</p>
        (where \n is a real newline), the middle close+open don't just disappear:
        line1
        li
        ne2
        line3
        I see two blocks with a blank line between them.

        CODE tags where there is a newline in the code become a block. CODE tags that contain no newlines do not become a block. So a close+open is invisible if there are no newlines in either chunk of code. A close+open cannot become invisible for a block unless we special-case it. That would surprise people who expect them to work like other block tags.

                        - tye
Re: Code that contains </code>
by Mr. Muskrat (Canon) on Aug 05, 2003 at 20:41 UTC

    Simplest answer: Don't use code tags in your code ;-)

    It's a little screwy looking but you simply need to break apart the closing code tag.
    <code>my $code = '<code></code</code><code>>';</code> results in:
    my $code = '<code></code>';
    exhibits break in multi-line code blocks :(