Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-04-19 02:17:38

jtlapp
Member
From: Austin, TX
Registered: 2006-03-19
Posts: 59
Website

How to paste arbitrary code

Below is a simple program for converting code into a form that can be posted to this forum.

I’ve wasted hours trying to figure out how to paste code to the forum without Textile mucking it up. These approaches do not always work. As far as I can tell, Textile does not have a way to blindly escape everything between two markers.

Then KurtRaschke pointed me to pastebin, which worked nicely except that a week after making my pastebin post the pastebin post disappeared. So much for pastebin.

So I created a simple web page that allows you to submit the code you want to post and be returned a page whose source is pastable directly to the forum. The submit page has a checkbox for optionally converting tabs to spaces, since leaving tabs in the source seems to make the code unreadable on my browser (as it appears in a post).

There is one serious drawback, though: you have to submit your post immediately after pasting the code without first previewing. This is because the preview page (or the browser) converts all the escaped characters back into their unescaped forms and leaves Textile free to muck with everything. Better to preview your post before inserting the code, or if you must, insert the code, preview the post, and then re-insert the code before submitting.

It’s one PHP file that I call sanitize.php:

<pre><code>
&#60;&#63;php

if&#40;&#33;isset&#40;&#36;&#95;POST&#91;&#39;submit&#39;&#93;&#41;&#41;
&#123;
&#63;&#62;

&#60;html style&#61;&#34;text&#45;align&#58; center&#34;&#62;
&#60;form method&#61;&#34;post&#34; action&#61;&#34;&#60;&#63;php echo &#36;&#95;SERVER&#91;&#39;PHP&#95;SELF&#39;&#93; &#63;&#62;&#34;&#62;
&#60;h2&#62;Sanitize code for inclusion in Textile&#60;&#47;h2&#62;

&#60;p&#62;Paste the code into the box and press &#60;b&#62;Sanitize&#60;&#47;b&#62;&#58;&#60;&#47;p&#62;
&#60;p&#62;&#60;textarea name&#61;&#34;code&#34; style&#61;&#34;width&#58;80&#37;&#59; border&#58; 2px solid &#35;000&#59;&#34; rows&#61;24&#62;&#60;&#47;textarea&#62;&#60;&#47;p&#62;

&#60;p&#62;
&#60;input type&#61;&#34;checkbox&#34; name&#61;&#34;notabs&#34; value&#61;&#34;1&#34; &#47;&#62;Convert tabs to spaces
&#38;nbsp&#59;&#38;nbsp&#59;&#38;nbsp&#59;&#60;input type&#61;&#34;submit&#34; name&#61;&#34;submit&#34; value&#61;&#34;Sanitize&#34; &#47;&#62;&#60;&#47;p&#62;

&#60;&#47;form&#62;
&#60;&#47;html&#62;

&#60;&#63;php

&#125;
else
&#123; &#36;code &#61; &#36;&#95;POST&#91;&#39;code&#39;&#93;&#59; if&#40;get&#95;magic&#95;quotes&#95;gpc&#40;&#41;&#41;

&#36;code &#61; stripslashes&#40;&#36;code&#41;&#59; &#36;notabs &#61; isset&#40;&#36;&#95;POST&#91;&#39;notabs&#39;&#93;&#41;&#59; echo &#34;&#60;html&#62;&#92;n&#60;h2 style&#61;&#39;text&#45;align&#58; center&#39;&#62;PLEASE VIEW SOURCE&#60;&#47;h2&#62;&#92;n&#92;n&#34;&#59; echo &#34;&#60;&#33;&#45;&#45; Copy everything from &#60;pre&#62; through &#60;&#47;pre&#62; &#45;&#45;&#62;&#92;n&#92;n&#34;&#59; echo &#34;&#60;&#33;&#45;&#45;&#92;n&#34;&#59; echo &#34;WARNING&#58; If you paste this code to the forum and then Preview&#44;&#92;n&#34;&#59; echo &#34;you will lose the character escaping that allows the code to&#92;n&#34;&#59; echo &#34;get by Textile and you&#39;ll need to repaste the code&#46; To keep&#92;n&#34;&#59; echo &#34;the code intact&#44; Submit the post immediately after pasting&#46;&#92;n&#34;&#59; echo &#34;&#45;&#45;&#62;&#92;n&#92;n&#34;&#59; echo &#34;&#60;pre&#62;&#60;code&#62;&#92;n&#34;&#59; for&#40;&#36;i &#61; 0&#59; &#36;i &#60; strlen&#40;&#36;code&#41;&#59; &#43;&#43;&#36;i&#41; &#123; &#36;ch &#61; &#36;code&#123;&#36;i&#125;&#59; if&#40;&#36;notabs &#38;&#38; &#36;ch &#61;&#61; &#34;&#92;t&#34;&#41; echo &#39; &#39;&#59; else if&#40;ctype&#95;space&#40;&#36;ch&#41; &#124;&#124; ctype&#95;alnum&#40;&#36;ch&#41;&#41; echo &#36;ch&#59; else echo &#39;&#38;&#35;&#39;&#46;ord&#40;&#36;ch&#41;&#46;&#39;&#59;&#39;&#59; &#125; echo &#34;&#92;n&#60;&#47;code&#62;&#60;&#47;pre&#62;&#92;n&#92;n&#60;&#47;html&#62;&#34;&#59; &#125;

&#63;&#62;
</code></pre>

Offline

#2 2006-04-27 17:37:33

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: How to paste arbitrary code

many thanks for this, jtlapp.

Maybe this can be “officially” added as a tool for the TXP forum, in the shape of a pop-up window…


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB