This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Argy
Argy

Let me preface this request by saying I have no idea what I am doing. I'm brand new to October, and have managed to get a blog up and running. Many of the posts I'll be creating have syntax in them so I installed the Syntaxed plugin (which uses Prism) and this is also working fine. However, I'm trying to get around the problem of markup not being displayed by Prism (this is a Prism issue, not October).

To do this, I want to run a function on every blog page that converts various markup tags (eg < to &lt) so the markup will display properly in my pre tags. I've found a function that I think does this but I can't get it to work. In the Code section of the Blog Post page I have added the following code:


function onStart()
{
   $output = $this->page;
   $output = preg_replace_callback('#()(.*?)()#s','quote_meta', $output);
}

function quote_meta($a) 
{
  $lhs = array("", "[", "]", "!", "{", "}", "`", "*", "~");
  $rhs = array("<", ">", "[", "]", "!", "{", "}", "`", "*", "~");
  $b = str_replace("&", "\255", $a[2]);  //save "&"
  $lhs_preg = array('||',  '||');
  $rhs_preg = array('',  '');
  $b = preg_replace($lhs_preg, $rhs_preg, $b);
  $b = str_replace($lhs, $rhs, $b);
  return str_replace("\255", "&", $b);
}

(Note: Ignore the code in the quote_meta function as it is not displaying correctly. Ironic. But it does not affect the issue).

When I view a page, I get the following error: "preg_replace_callback(): Requires argument 2, 'quote_meta', to be a valid callback". This implies (to me) that the function quote_meta is not being seen by the page. There are probably many other things that are wrong with my approach, so I would appreciate anyone's help to sort this out.

Last updated

Vojta Svoboda
Vojta Svoboda

Try:

$output = preg_replace_callback('#()(.*?)()#s', 'self::quote_meta', $output);

OR

$output = preg_replace_callback('#()(.*?)()#s', function($a) { ... }, $output);

both works for me.

Source: http://php.net/manual/en/function.preg-replace-callback.php

Last updated

Argy
Argy

Thanks Vojta. I now have no errors, but the code below either is not being run, or it is not being run at the right time. Is onStart() the correct place to run it? And is $this->post the correct object to run it against? (I was using $this->page but it errored). As you can probably tell, I don't yet understand how all this hangs together.

function onStart()
{
   $output = $this->post;
   $output = preg_replace_callback('#()(.*?)()#s','self::quote_meta', $output);
}

function quote_meta($a) 
{
  $lhs = array("", "[", "]", "!", "{", "}", "`", "*", "~");
  $rhs = array("<", ">", "[", "]", "!", "{", "}", "`", "*", "~");
  $b = str_replace("&", "\255", $a[2]);  //save "&"
  $lhs_preg = array('||',  '||');
  $rhs_preg = array('',  '');
  $b = preg_replace($lhs_preg, $rhs_preg, $b);
  $b = str_replace($lhs, $rhs, $b);
  return str_replace("\255", "&", $b);
}

I wish I could get the code to display properly, as it makes it difficult to explain exactly what is going on, but hopefully the concept is clear.

Last updated

Vojta Svoboda
Vojta Svoboda

Try dump() method, to see if you Blog post is placed in $this->post variable:

dump($this->post);

But you definitely should assign output back to the variable:

$this->post = $output;

Last updated

Argy
Argy

Thanks again for your suggestions Vojta. Some minor progress...

The code needs to be in the onEnd() function. I've tried to simplify it so that it now looks like

function onEnd()
{
   $this->post = preg_replace_callback("#(<prismpre>)(.*?)(</prismpre>)#s", function ($a) 
{
  $lhs = array("<", ">", "[", "]", "!", "{", "}", "`", "*", "~");
  $rhs = array("&lt;", "&gt;", "&#091;", "&#093;", "&#033;", "&#123;", "&#125;", "&#96;", "&#42;", "&#126;");
  $b = str_replace("&", "\255", $a[2]);  //save "&"
  $lhs_preg = array('|<!(!*)prismpre>|',  '|<!(!*)/prismpre>|');
  $rhs_preg = array('<$1prismpre>',  '<$1/prismpre>');
  $b = preg_replace($lhs_preg, $rhs_preg, $b);
  $b = str_replace($lhs, $rhs, $b);
  return str_replace("\255", "&amp;", $b);
}, $this->post);
   //dump($this->post);

}

When I dump it the code appears as though the preg_replace_callback has not run. I'm at a loss.

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.