http://www.perlmonks.org?node_id=189399

While working on something else, I realized that I could make <code> tags automatically wrap code to (roughly) fit your window. So you can now go to user settings and check the "Auto Code Wrap" option and any stuff inside of <code> tags will be rendered such that your browser will wrap it to fit. So if the code is hard to read because it is wrapped too much, just widen your browser window.

If someone puts a really long string without spaces inside of <code> tags, then your browser won't wrap in the middle of this really long string. This is a pain, so if you have "Auto Code Wrap" checked and you also enter a number (say "20") in "Code Wrap Length" (leaving it blank doesn't default to "70" in this case), then we'll insert a red soft hyphen after each run of 20 non-space characters. These will be invisible (on a conforming browser) unless your browser decides to wrap the code at that point.

You can also now enter a "Code Prefix" which will be prepended to each line of code displayed. If you include a run of digits something matching /&\d+;/ in this prefix, then those digits will be incremented for each line of code. So you can use a prefix of, for example, "&001;:&nbsp;" to have all lines of code numbered. Note that &#8226; includes a bullet while &8226; would start numbering lines at 8226.

Note that "Code Wrapping Off" overrides all of these settings.

Also, if you have "Auto Code Wrap" checked (and no Code Prefix defined), you might be tempted to skip the "d/l code" link and just cut'n'paste the code directly. However, if you've entered a number for Code Wrap Length and that code has any strings of non-space characters that long or longer, then you'll get some soft hyphens included (even though they might be invisible) which will break the code.

I've tested things quite a bit but there were a lot of changes involved and some browsers may not like the red soft hyphens (if so, then either don't check Auto Code Wrap or set Code Wrap Length to be empty)...

If you'd like to test your new settings, try Re: Space or Soft Hyphens (see code wrap).

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
(kudra: why tie code prefix to code wrapping on?) Re: New code wrap options
by kudra (Vicar) on Aug 12, 2002 at 10:00 UTC
    Is there any particular reason to have Code Prefix tied to Code Wrapping on? It doesn't seem especially logical, so I assume it is a matter of implementation simplification. If not, I hope you'll consider changing this for people like myself who would like line numbering but don't like the line breaking.

    Thank you for continuing to add new features to the site.

      I just ran out of time. I hope to have Code Wrapping Off not disable Code Prefix when I find some time again.

              - tye (but my friends call me "Tye")
Re: New code wrap options
by blakem (Monsignor) on Aug 12, 2002 at 12:36 UTC
    Unfortunately this option breaks the CSS hack some of us have been using to highlight code blocks. It assumes code blocks are surrounded by <pre><tt> tags. I think a style="code" tag surrounding the block might solve this, but I'm not much of a CSS expert.

    See a random page from the perlmonks.thepen site for an example of how I have the CSS code highlighting set up.

    -Blake

      Now code is wrapped in <tt class="code"> (for all combinations of wrapping settings). Let me know if that works.

      Update: In-line <code> (like that -- stuff in <code> tags that doesn't include a newline) is not designated class="code". If someone wants a class designation for this, then come up with a nice, short, clear name for the class. (:

              - tye (but my friends call me "Tye")
        Thanks tye. Setting CSS styles for tt.code instead of pre tt works with both user setting options now.

        -Blake

        If someone wants a class designation for this, then come up with a nice, short, clear name for the class.

        class="fragment"

        However getting short and clear at the same times seems to me to be setting the bar too high. Maybe having one without the other is preferable?


        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi


Re: New code wrap options
by RMGir (Prior) on Aug 12, 2002 at 15:35 UTC
    Nice options!

    I had a problem this morning with a code block that was wrapping like mad, which I thought was related to this change. But it turns out I'd forgotten the </code> tag, which I guess can be expected to confuse things :)

    Here's what happens when you do that:

    <code> #!/usr/bin/perl -w use strict; #line 3 print "Hello, world!\n"; #line 5 # forgot /code tag here on line 6
    --
    Mike

Re: New code wrap options - FYI <code2html>
by barrachois (Pilgrim) on Aug 16, 2002 at 20:40 UTC
    I use a <code2html> tag on my wiki that displays source code as spiffy syntax-colored and formatted html. While it isn't a standard html tag and the cpu load to process the source can be a bit much, it's been quite useful on our site.

    The Code2HTML.pm package that does all the work is a single perl module that I adapted from Peter Palfrader's utility. You can see it described on my wiki here. And the Code2HTML.pm perl package is available here.

Re: New code wrap options
by blakem (Monsignor) on Aug 21, 2002 at 10:41 UTC
    Cutting and pasting code sections from Mozilla to emacs with the new Auto Code Wrap feature didn't work too well for me at first, so I hadn't been using it. Tonight I investigated further and found that the following line in my .emacs config file fixes it for me.
    ;; perlmonks soft-hyphen fix (noop for hidden char) (global-set-key "\xad" ())
    Update: Furthermore, putting this in my ~/.inputrc file fixes it for my shell.
    "\xAD":noop

    -Blake