Recent Topics

Displaying code in your posts

Started by on Aug 17, 2005 – Contents updated: Aug 17, 2005

Aug 17, 2005 18:29    

Maybe some other people will find this convenient? First you make a new button next to "upload image" then you make a new file that converts code to the encoded entity thing that will insert it into your post.

In admin/_edit_form.php add a new javascript:

Code

function launchupload()
  {
    window.open ("b2upload.php", "b2upload", "width=380,height=360,location=0,menubar=0,resizable=1,scrollbars=yes,status=1,toolbar=0");
  }
 
  function launchupcode()
  {
    window.open ("b2upcode.php", "b2upcode", "width=740,height=500,location=0,menubar=0,resizable=1,scrollbars=yes,status=1,toolbar=0");
  }

Next scroll down and add the button that will use this script:

Code

if( $current_User->check_perm( 'upload' ) )
  { // ------------------------------- UPLOAD ---------------------------------- ?>
    <input type="button" value="<?php echo T_('Upload a file/image') ?>"
    onclick="launchupload();" tabindex="12"  />
    <?php
  }
 
  if( $current_User->check_perm( 'upload' ) )
  { // ------------------------------- UPCODE ---------------------------------- ?>
    <input type="button" value="<?php echo T_('Insert Code') ?>"
    onclick="launchupcode();" tabindex="13"  />
    <?php
  }

Now save this as "b2upcode.php" in your admin folder:

Code

<?php
require_once (dirname(__FILE__).'/_header.php');
 
$current_User->check_perm( 'upload', 'any', true );
 
?><html xml:lang="<?php locale_lang() ?>" lang="<?php locale_lang() ?>">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php locale_charset() ?>" />
<title><?php echo T_('b2evo') ?> &gt; <?php echo T_('process code for posting') ?></title>
<link href="variation.css" rel="stylesheet" type="text/css" title="Variation" />
<link href="desert.css" rel="alternate stylesheet" type="text/css" title="Desert" />
<link href="legacy.css" rel="alternate stylesheet" type="text/css" title="Legacy" />
<?php if( is_file( dirname(__FILE__).'/custom.css' ) ) { ?>
<link href="custom.css" rel="alternate stylesheet" type="text/css" title="Custom" />
<?php } ?>
<script type="text/javascript" src="styleswitcher.js"></script>
<script type="text/javascript">
<!--
function targetopener(blah, closeme, closeonly) {
  if (! (window.focus && window.opener))return true;
  window.opener.focus();
  if (! closeonly)window.opener.document.post.content.value += blah;
  if (closeme)window.close();
  return false;
  }
 
function ProcCode() {
  var fixing = document.form1.rawcode.value+"";
  fixing = fixing.replace(/&/g, "&amp;");
  fixing = fixing.replace(/ \* /g, "&nbsp;*&nbsp;");
  fixing = fixing.replace(/ \= /g, "&nbsp;=&nbsp;");
  fixing = fixing.replace(/  /g, "  ");
  fixing = fixing.replace(/</g, "&lt;");
  fixing = fixing.replace(/>/g, "&gt;");
  fixing = fixing.replace(/; /g, ";&nbsp;");
  fixing = fixing.replace(/(\n\n)/g, "\n&nbsp;\n");
  fixing = fixing.replace(/(\r\r)/g, "\n&nbsp;\n");
  fixing = fixing.replace(/(\n\r\n\r)/g, "\n&nbsp;\n");
  var codetext = fixing+"";
  document.form1.finished.value = "<code>"+codetext+"</code>";
  }
//-->
</script>
</head>
<body>
<div class="panelblock">
<fieldset>
<legend><?php echo T_('Process code for posting') ?></legend>
 
<form action="#" method="post" name="form1">
<p><textarea id="rawcode" name="rawcode" rows="12" cols="80" onkeydown="setTimeout('ProcCode();',10);"></textarea></p>
<p><textarea id="finished" name="finished" rows="8" cols="80">
Enter code in the field above and it will show up down here for a preview, then click "Add Code to Post", then close this window.
</textarea></p>
</form>
 
<form action="b2upcode.php">
<p>
<input type="button" name="close" value="<?php echo T_('Add Code to Post') ?>" class="search" onclick="targetopener(document.form1.finished.value)" />
&nbsp;
<input type="button" name="close" value="<?php echo T_('Close this window') ?>" class="search" onclick="window.close()" />
</p>
</form>
 
</fieldset>
</div>
</body>
</html>

That will insert your converted code inside CODE tags, so if you want something different you change this line:

Code

document.getElementById("finished").value = "<code>"+codetext+"</code>";

Since I'm my only blogger and I let myself upload images I just copied that tool, including the permission aspect. If you have multiple bloggers you might want to change the permission check to look for user level or group.

Version 0.9.0.12, maybe backwards (but you should upgrade) and no clue about future versions.

Aug 24, 2005 11:13

Hello,

When I'm using your hack, the code is not updated in the field above and in the post i'm writing I got: "Enter code in the field above and it will show up down here for a preview, then click "Add Code to Post", then close this window." instead of the code.

I'm using Firefox. Do you have an idea to resolve this ?

Aug 24, 2005 11:43

Sometimes there is a delay before the code gets converted after pasting stuff in the top box. Or maybe it just gets buggy - I dunno for sure. Simplest fix: put the cursor anywhere in the top box, add a space then delete it back out.

Aug 24, 2005 14:15

I used it, and it works like a charm.

Aug 25, 2005 17:02

I added a preview button to avoid this problem B)

Code

<input type="button" name="preview" value="<?php echo T_('Preview') ?>" class="search" onclick="setTimeout('ProcCode();',10);" />
&nbsp;
<input type="button" name="close" value="<?php echo T_('Add Code to Post') ?>" class="search" onclick="targetopener(document.form1.finished.value)" />
&nbsp;
<input type="button" name="close" value="<?php echo T_('Close this window') ?>" class="search" onclick="window.close()" />

I will try to add PHP Syntax Highlighter.

Aug 26, 2005 09:01

slamp wrote:

I will try to add PHP Syntax Highlighter.

That would be awesome !

Aug 31, 2005 10:07

I saw it, in fact I wrote a renderer plugin only for php, then I saw the other solution so I stop working on it. Also I have the same problem as Mariono about html checkin.

Sep 11, 2005 01:14

Just a quick glance at your source code tells me you either did the hack incorrectly or have something *significantly* hosed in your blog somewhere. It's not a browser thing though. If it was the problem wouldn't show up in Firefox cuz that's the only browser I use - unless I'm testing skins.

your source code wrote:

<h2 class="topheading"><a href="http://blog.thedarksighed.com/travelblogue/2005/09/10/how_to_display_code_in_your_posts" title="Permanent link to full entry" class="permalink_left">How to display code in your posts</a></h2>
<div class="bText">

<!-- ************************************ -->
<!-- EDITED TO REMOVE 'MORE' ICON -->
<!-- <p>In the admin/_edit_form.php locate the first javascript function, and paste the second function immediately below it:</p>
<p><code><br />
function launchupload()<br />
{<br />
window.open ("b2upload.php", "b2upload", "width=380,height=360,location=0,menubar=0,resizable=1,scrollbars=yes,status=1,toolbar=0");<br />
}<br />
&nbsp;<br />
function launchupcode()<br />
{<br />
window.open ("b2upcode.php", "b2upcode", "width=740,height=500,location=0,menubar=0,resizable=1,scrollbars=yes,status=1,toolbar=0");<br />
}</code></p>
<p>Next, scroll down and locate the UPLOAD section. Immediately below it, paste the UPCODE section shown below:</p>
<p><code> if( $current_User-&gt;check_perm( 'upload' ) )<br />
{ // ------------------------------- UPLOAD ---------------------------------- ?&gt;<br />
&lt;input type="button" value="&lt;?php echo T_('Upload a file/image') ?&gt;"<br />
onclick="launchupload();" tabindex="12" /&gt;<br />
&lt;?php<br />
}<br />
&nbsp;<br />

and so on...

Sep 11, 2005 01:34

Actually I think it's just that particular block of code which is causing the problem, because I've tested it with other random blocks of code which I copied from other posts, and they work just fine...

Basically what I was trying to do is copy the entire code with directions onto my blog in a post visible only to me (sort of a collection of useful hacks in one place without having to search for them on the forums).

So, I tried removing the section that goes into the b2upcode.php file and, if you check the post again, it posted just fine... It seems to me it's either the javascript section of the b2upcode.php code, or the fact that it contains the whole <html> top portion...

Check the post again.... it does work, just something about that particular block of code that doesn't allow it work...

http://blog.thedarksighed.com/travelblogue/2005/09/10/how_to_display_code_in_your_posts

jj.

Sep 11, 2005 03:06

Pasting from a phpbb forum to a blog app is hit or miss at best. Keep in mind all dynamic applications do their own funky things to make their bells ring and whistles sing. Just consider what b2evolution does with the "more" tag and you'll understand.

Why not just bookmark the pages you like, or keep a protected post with links to stuff you want to reference? It is a LOT more reliable than trying to paste an entire phpbb post into a blog app!

Bottom line is this: neither the hack nor the browser is causing the problem you are seeing. What you are seeing is how different browsers are trying to interpret a dynamically generated page that was copied from a different dynamic page generator.

Sep 29, 2005 11:30

I implemented this hack into 0.9.1b so that I could add the following code:

Code

<location /blog/wp-comments-newpost.php>
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^.*dvorak.org/.*
RewriteRule ^.* http://www.ctyme.com/comment-spam.html
</location>

But even with the hack I got an invalid tag error for the location tag.

Sep 29, 2005 11:39

The code you quoted is not php source.

Sep 29, 2005 12:05

slamp wrote:

The code you quoted is not php source.

I know, it's Apache configuration directives. So I can only use

Code

<code></code>

for displaying PHP code?

Code

<pre></pre>
didn't work either for the unencoded tags at least, I'll try modifying the hack to use

Code

<pre>
then instead.

Thanks.

Sep 29, 2005 12:09

cornetchap wrote:

I'll try modifying the hack to use

Code

<pre>
then instead.

Which worked. To be honest I find that a little surprising as my understanding of using <pre> in this sort of context was that no interpretation was done on the text between the opening and closing tags.

Ho hum, at least it works :-/

Sep 29, 2005 18:16

The gushey thing is cooler in a lot of ways, but I had no problem putting

Code

<location /blog/wp-comments-newpost.php>
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^.*dvorak.org/.*
RewriteRule ^.* http://www.ctyme.com/comment-spam.html
</location>
in a post with this hack in my v12 installation.

Sep 29, 2005 18:28

EdB wrote:

The gushey thing is cooler in a lot of ways, but I had no problem putting

Code

<location /blog/wp-comments-newpost.php>
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^.*dvorak.org/.*
RewriteRule ^.* http://www.ctyme.com/comment-spam.html
</location>
in a post with this hack in my v12 installation.

Neither did I once I'd changed <code> for <pre> ;) But I'm still confused as to why <pre> interprets the text between the opening and closing tags. Maybe I'm just easily confused.

Sep 29, 2005 18:58

Version-specific issue most likely? I haven't looked at upping this one to live inside dawn (91b) because I got way too much hackage to upgrade myself. First I need to upgrade, and hopefully before the next release happens, and then I'll keep an eye out for this problem and see if I can't make it work.

Oh plus I noticed it also made the url a link. That's a drag because it shouldn't have been - especially when the code you want to display is a domain you want to ban via .htaccess!!!

Sep 29, 2005 21:09

EdB wrote:

Version-specific issue most likely? I haven't looked at upping this one to live inside dawn (91b) because I got way too much hackage to upgrade myself. First I need to upgrade, and hopefully before the next release happens, and then I'll keep an eye out for this problem and see if I can't make it work.

Maybe not, I took the trouble to lookup the meaning of <pre> and it seems it doesn't quite behave as I'd expected. It does seem to interpret embedded tags but it preserves whitespace and line breaks.

EdB wrote:

Oh plus I noticed it also made the url a link. That's a drag because it shouldn't have been - especially when the code you want to display is a domain you want to ban via .htaccess!!!

Almost, the URL is really a redirection for spam-bots that access the comment posting code directly rather than via the blog. So, if you substitute /blog/wp-comments-newpost.php with /blog/htsrv/comment_post.php you can also use these Apache directives to assist in preventing comment spam with b2evolution.

Nov 20, 2005 19:10

I have added the following format lines for tabs, double and single quotes. Turns out that the SQL cleaning syntax was converting these to left/right quotes of the double and single variety and it really messes up PHP.

Code

fixing = fixing.replace(/  /g, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
   fixing = fixing.replace(/"/g, "&quot;");
   fixing = fixing.replace(/'/g, "'");

Everything is working well except I cannot get the "Add Code to Post" button to work. I end up with the post page displayed and nothing in the box.

I end up having to copy the text from the one box to the other. In all not bad, but I would like the post to work.

Aug 09, 2006 13:09

Hi Edb: I´m trying to modify your script so as to remove unwanted line breaks in the middle of paragraphs and remove those e-mail reply marks (> symbols). Just like Text Cleanup or similar programs does.

I added this code to your function ProcCode() {

fixing = fixing.replace(/<br>\s*<br>/g, '<!-- aaa -->');
fixing = fixing.replace(/<br>/g, ' ');
fixing = fixing.replace(/<!-- aaa -->/g, '<br><br>');
fixing = fixing.replace(/>>/g, ' ');

It works with the e-mail reply marks, but it doesn´t with the unwanted line breaks. Could you help me?


Form is loading...

Photo albums software – This forum is powered by b2evolution CMS, a complete engine for your website.