WordWrapPreformattedText

Summary: Prevent long lines of text stretching beyond the window edge
Version:
Prerequisites:
Status:
Maintainer:
Categories: Administration

Question

How can I prevent long lines of text stretching beyond the window edge?

Answer

Lines of text which do not wrap automatically at the window edge have most likely empty spaces at the beginning of the paragraph (by design or accident), which makes pmwiki treat them as preformatted text. Preformatted text does not get wrapped by default. There are a number of approaches you can use to wrap preformatted text.

Markup

You can force word-wrapping of preformatted text with the following markup. Include it in config.php. No markup needs to be inserted in the page, and the original text does not get altered.

	# wrap preformatted text at max 75 characters:
	Markup('^ws', 'block',
		 '/^(\\s+)(.*)$/e',
		 "'<:pre,1>$1'.wordwrap(PSS('$2'), 75, '\n$1')");

If a different maximum width is required just change the number in the markup.

Overflow

Another approach uses stylesheets to add scrollbars to text that exceeds container edge. Simply add the following to your skin's stylesheet:

        pre
        {
          overflow: scroll;
        }

Browser wrapping

The forthcoming CSS3 specifies white-space behavior; however, this isn't widely supported yet. To wrap text without scrollbars, we can use a number of browser-specific styles. This method isn't as portable as other methods, but it does yield good results when it works. Add the following to your skin's stylesheet:

       pre
       {
          white-space: pre-wrap;       /* CSS-3                  */
          white-space: -moz-pre-wrap;  /* Mozilla, since 1999    */
          white-space: -pre-wrap;      /* Opera 4-6              */
          white-space: -o-pre-wrap;    /* Opera 7                */
          word-wrap: break-word;       /* Internet Explorer 5.5+ */
       }

No formatting

Finally, if no preformatted text is wanted/needed on the site, it can be turned off all together, by setting in config.php:

       # turn off preformatting
       $EnableWSPre = 0 ;          # PmWiki v2.2, see $EnableWSPre.
       DisableMarkup('^ws');     # PmWiki v2.1  
       DisableMarkup('^ ');      # PmWiki v2.0   

Notes and Comments

  • Now also works on PmWiki Version 2.2
  • This recipe was last tested on PmWiki version: 2 beta 31, v2.1.11
  • This recipe requires at least PmWiki version: 2

See Also

Contributors


JLuk 2008-Jan-15

Does not appear to work with the current stable release pmwiki-2.1.27.

Gary Levin March 02, 2006, at 01:19 PM This recipe is not working on "pmwiki-2.1.beta20". DisableMarkup does not prevent the formatting. And the pattern in the Markup does not match. It is easy to see by making the replacement string not a legal expression, which will result in an error if the replacement is attempted and no error shows up. It appears that the start of line anchor (^) is the problem.

Ah yes, I suspect that the problem is that this recipe interferes with the "leading whitespace rule" that was introduced in 2.1.beta1. I'll have to see about updating the recipe.

--Pm

Subhash: November 11, 2006, at 7:30 PM
Wrappig works for me with pmwiki-2.2.0-beta15. Is there a suggestion how to turn off pre formatting by leading whitespace now? "Naive Users" are preformatting and don't know what to do against it. I would like to keep everything very very simple. Thanks! – Subhash (office [at] subhash [dot] at)

Ian MacGregor May 23, 2007
There are other values for this:

pre
{
overflow: auto;
}

If the content is clipped, the browser should display a scroll bar to see the rest of the content - scrollbar shouldn't be displayed if content isn't clipped. Using overflow: scroll; displays the horizonal and vertical scrollbars in all pre-formatted text regardless of whether it's clipped or not. Using overflow: auto; will display a scroll bar only if the content is clipped. Here are other possibilities:

Value

Description

visible

Default. The content is not clipped, it renders outside the element.

hidden

The content is clipped, but the browser does not display a scroll-bar to see the rest of the content.

scroll

The content is clipped, but the browser displays a scroll-bar to see the rest of the content.

auto

If the content is clipped, the browser should display a scroll-bar to see the rest of the content. Scrollbar isn't displayed for content which isn't clipped.