https://rt-wiki.bestpractical.com/index.php?title=PlainTextPre&feed=atom&action=historyPlainTextPre - Revision history2024-03-29T04:57:16ZRevision history for this page on the wikiMediaWiki 1.37.2https://rt-wiki.bestpractical.com/index.php?title=PlainTextPre&diff=2607&oldid=prevAdmin: 2 revisions imported2016-04-06T20:20:28Z<p>2 revisions imported</p>
<p><b>New page</b></p><div>This patch is at http://issues.bestpractical.com/Ticket/Display.html?id=8374. It was tested with 3.6.1 and I believe it will work as-is with 3.6.3.<br />
<br />
Many of the messages our RT instance handles contain space- and tab-based formatting in plain text. The way RT displays them (as HTML with &lt;br&gt; used to preserve line breaks) causes this formatting to be lost. Eg, this text:<br />
<br />
<nowiki>left side right side<br />
--------- ----------<br />
item one value one<br />
item two value two<br />
<br />
</nowiki><br />
<br />
won't look right on the web page with a standard RT setup.<br />
<br />
I couldn't figure out a clean way to change this via an overlay or callback. One problem is the &lt;br&gt;s get added after the callback returns. Another is that you want to use one &lt;pre&gt; for the whole message body (because it introduces vertical space the way a &lt;p&gt; does), but [[ShowMessageStanza]] is called for each blob of text Text::Quoted extracts. Additionally the standard [[ShowMessageStanza]] adds newlines around the &lt;div&gt;s, and these cause blank lines inside &lt;pre&gt;.<br />
<br />
There might be a way to do have space- and tab-based formatting work based on "white-space: pre;" style instead of &lt;pre&gt;, but this didn't line things up correctly when I tried it. I am no HTML expert.<br />
<br />
Here's the solution I came up with. It adds a new config variable, $RT::[[PlainTextPre]], which allows you to tell the system to use &lt;pre&gt; on plain text message bodies.<br />
<br />
<nowiki>--- etc/RT_Config.pm.in.~1~ 2006-10-25 23:18:11.000000000 -0400<br />
+++ etc/RT_Config.pm.in 2007-05-10 15:34:04.000000000 -0400<br />
@@ -463,6 +463,13 @@<br />
<br />
Set($ShowTransactionImages, 1);<br />
<br />
+# Normally plain text attachments are displayed as HTML with line<br />
+# breaks preserved. This causes space- and tab-based formatting not<br />
+# to be displayed correctly. By setting $PlainTextPre they'll be<br />
+# displayed using &lt;pre&gt; instead so such formatting works, but they'll<br />
+# use a monospaced font.<br />
+<br />
+Set($PlainTextPre, 0);<br />
<br />
# $HomepageComponents is an arrayref of allowed components on a user's<br />
# customized homepage ("RT at a glance").<br />
--- html/Ticket/Elements/ShowMessageStanza.~1~ 2006-06-19 18:44:04.000000000 -0400<br />
+++ html/Ticket/Elements/ShowMessageStanza 2007-05-10 14:52:13.000000000 -0400<br />
@@ -1,3 +1,4 @@<br />
+%# $Id: PlainTextPre,v 1.1 2007/05/10 20:30:13 www-data Exp www-data $<br />
%# BEGIN BPS TAGGED BLOCK {{{<br />
%#<br />
%# COPYRIGHT:<br />
@@ -43,35 +44,73 @@<br />
%# those contributions and any derivatives thereof.<br />
%#<br />
%# END BPS TAGGED BLOCK }}}<br />
-% if (ref($Message)) {<br />
-&lt;div class="message-stanza-depth-&lt;% $Depth %&gt;"&gt;<br />
&lt;%perl&gt;<br />
-foreach my $stanza (@$Message) {<br />
- if ( ref $stanza eq "ARRAY" ) {<br />
- $m-&gt;comp( 'ShowMessageStanza',<br />
- Depth =&gt; $Depth + 1,<br />
- Transaction =&gt; $Transaction,<br />
- Message =&gt; $stanza );<br />
- }<br />
- elsif ( ref $stanza eq "HASH" ) {<br />
- my $content = $stanza-&gt;{raw};<br />
- RT::Interface::Web::EscapeUTF8(\$content);<br />
- $m-&gt;comp('/Elements/Callback', content =&gt; \$content, %ARGS);<br />
- $content =~ s{$}{&lt;br /&gt;}mg<br />
- if defined $content;<br />
<br />
+# This component is called to display plain text attachments. At the<br />
+# top level it's passed the data structure returned by Text::Quoted,<br />
+# then it uses itself recursively to display it.<br />
+#<br />
+# By default the messages are displayed as simple escapped HTML. Line<br />
+# breaks are preserved by adding &lt;br&gt;.<br />
+#<br />
+# If you set $RT::PlainTextPre then the messages are displayed inside a<br />
+# &lt;pre&gt; section. This allows space- and tab-based formatting to work,<br />
+# at the cost of using a monospaced font.<br />
+#<br />
+# There might be a way to do have space- and tab-based formatting work<br />
+# based on "white-space: pre;" style instead of &lt;pre&gt;, but this didn't<br />
+# line things up correctly when I tried it. I am no HTML expert.<br />
+<br />
+# Because the output might be inside &lt;pre&gt; this code has to be careful<br />
+# not to introduce extra newlines.<br />
+<br />
+# An oddity of Text::Quoted is that it strips the trailing newline from<br />
+# each returned strings. The strings can be multi-line, they'll always<br />
+# be missing the newline at the end.<br />
+<br />
+if (ref($Message)) {<br />
+ my ($pon, $poff) = ($RT::PlainTextPre &amp;&amp; $Depth == 0)<br />
+ ? ('&lt;pre&gt;', '&lt;/pre&gt;')<br />
+ : ('', '');<br />
+ $m-&gt;out($pon);<br />
+ $m-&gt;out(qq(&lt;div class="message-stanza-depth-$Depth"&gt;));<br />
+ foreach my $stanza (@$Message) {<br />
+ if ( ref $stanza eq "ARRAY" ) {<br />
+ $m-&gt;comp( 'ShowMessageStanza',<br />
+ Depth =&gt; $Depth + 1,<br />
+ Transaction =&gt; $Transaction,<br />
+ Message =&gt; $stanza );<br />
+ }<br />
+ elsif ( ref $stanza eq "HASH" ) {<br />
+ my $content = $stanza-&gt;{raw};<br />
+ RT::Interface::Web::EscapeUTF8(\$content);<br />
+ $m-&gt;comp('/Elements/Callback', content =&gt; \$content, %ARGS);<br />
+ # I put the trailing \n back here, after the callback,<br />
+ # because the callback was historically called without it<br />
+ # attached.<br />
+ $content .= "\n";<br />
+ $content =~ s{\n}{&lt;br /&gt;\n}mg<br />
+ if !$RT::PlainTextPre;<br />
+ $m-&gt;out($content);<br />
+ }<br />
+ }<br />
+ $m-&gt;out('&lt;/div&gt;');<br />
+ $m-&gt;out($poff);<br />
+} else {<br />
+ my $content = $Message;<br />
+ RT::Interface::Web::EscapeUTF8(\$content);<br />
+ $m-&gt;comp('/Elements/Callback', content =&gt; \$content, %ARGS);<br />
+ $content .= "\n"<br />
+ if $content !~ /\n$/;<br />
+ if ($RT::PlainTextPre) {<br />
+ $content = "&lt;pre&gt;$content&lt;/pre&gt;";<br />
+ }<br />
+ else {<br />
+ $content =~ s{\n}{&lt;br /&gt;\n}mg;<br />
+ }<br />
+ $m-&gt;out($content);<br />
+}<br />
&lt;/%perl&gt;<br />
-&lt;%$content |n%&gt;<br />
-% }<br />
-% } # end foreach<br />
-&lt;/div&gt;<br />
-% } else {<br />
-% my $content = $Message;<br />
-% RT::Interface::Web::EscapeUTF8(\$content);<br />
-% $m-&gt;comp('/Elements/Callback', content =&gt; \$content, %ARGS);<br />
-% $content =~ s{$}{&lt;br /&gt;}mg;<br />
-&lt;%$content |n%&gt;<br />
-% }<br />
&lt;%INIT&gt;<br />
use URI::URL;<br />
&lt;/%INIT&gt;<br />
</nowiki></div>Admin