[pmwiki-users] Forcing wordwrap within [@ @] tags

Waylan Limberg waylan at gmail.com
Mon Aug 29 10:55:44 CDT 2005


I have some interesting formatting needs and decided the easiest way
is to have the users place these sections of text between [@ @] tags.
Now, in most cases, this is best, as the mono spaced text actually
makes things work better for this specific situation. However, I was
running into one small problem Generally speaking, each line is short
and the editor would hit enter/return at the end of the line.
Therefore, what appeared in the edit form would be identical to what
appeared on viewing the page. The problem though, was that on a few
rare occasions a longer paragraph is found, and as anything wrapped
between <pre> or <code> tags does not get wrapped, it was messing up
the formatting of the page.

I needed a way to force text to wrap within [@ @] tags. Sure, I could
make everyone manually enter a newline at the end of each line, but
that seemed silly. Then I considered using php's built in wordwrap()
function. After trying to understand the markup rules in stdmarkup.php
with little success because of my difficulty with regex, I simply
copied the necessary lines to my config file and made the following
changes there. As I understand it, this simply overrides the default
behavior.

## PreserveWrapText replaces Preservetext in stdmarkup.php
## Intended to work with the custom markup for [@ @] blocks
## Wraps all text between <pre> tags at 75 characters
function PreserveWrapText($sigil, $text, $lead) {
  if ($sigil=='=') return $lead.Keep($text);
  if (strpos($text, "\n")===false) 
    return "$lead<code>".Keep($text)."</code>";
  $text = preg_replace("/^[^\\S\n]*\n/", "\n", substr($lead,1).$text);
  $text = preg_replace("/\n[^\\S\n]*$/", "\n", $text);
  return "<pre>".Keep(wordwrap($text, 75, "\n"))."</pre>";
}

## Overriding markup of same name in stdmarkup.php
## Calls PreserveWrapText (above) instead of PreserveText
Markup('[=','_begin',"/(\n[^\\S\n]*)?\\[([=@])(.*?)\\2\\]/se",
    "PreserveWrapText('$2', PSS('$3'), '$1')");

And now for the questions:
First, is this really the best way to do this?
Could someone help me write a markup rule that uses some different
custom markup (perhaps (:prewrap:)) so the default behavior of [@ @]
could still be used? (note: currently the above is only applied to the
one group where it is needed  via local/Groupname.php - the rest of
the site gets regular behavior)

What I really like about this solution is that as long as the edit
form is set to the same width as the wordwrap (not forgetting the
width of the scrollbar), what appears in the edit form is exactly the
same as what appears on the page.

I will probably alter the last line of PreserveWrapText() to wrap the
text in <div class="somecustomclass"></div> rather than <pre> tags and
then set .somecustomclass{whitespace:pre; ...} which allows me to also
alter other formatting like background, border etc, without messing up
any other use of <pre> tags on the site.

Any other input would be greatly appreciated.
-- 
----
Waylan Limberg
waylan at gmail.com




More information about the pmwiki-users mailing list