Please wait, Page is Loading...
 

Summary: Hightlight PHP Code
Version: pre-alpha
Prerequisites: none
Status: DEPRECATED
Maintainer: Dfolio
Categories: PHP | PmWiki | DEPRECATED

Description

Hightlight PHP Code.

DEPRECATED: I prefere to use Cookbook:Source

Notes

I propose here a new markup allowing to highlight PHP syntax: ie. use of the highlight_string() PHP function.
To this aim I proposed to add the following code to your local/config.php:

Markup('[php','_begin',"/(\n[^\\S\n]*)?\\[php(.*?)php\\]/se","ShowPhpCode(PSS('$2'), '$1')");
function ShowPhpCode($text, $lead) {
  global $pagename, $HiglightColors;
  SDVA($HiglightColors, array(
    "string"  => "#dd0000",
    "comment" => "#ff8000",
    "keyword" => "#007700",
    "bg"      => "inherit",
    "default" => "#0000bb",
    "html"    => "black",
    ));
  $hTpl = "<span style=\"color: %s\">";

  $text=str_replace("<:vspace>","\n",$text);

  if (strpos($text, "\n")===false) {
    $hc=highlight_string("<?php$text?>",true);## Highlighted contents
    $hc=str_replace(array("&lt;?php<br />","?&gt;","<br /><br />",'&amp;'),
                    array("",'',"<br />",'&'),$hc);
    foreach($HiglightColors as $key => $color) {
      if(!empty($color)){
        $hc = str_replace(sprintf($hTpl, ini_get("highlight.{$key}")), sprintf($hTpl, $color), $hc);
      }
    }
    $hc=str_replace("<code>","<code class='escaped'>",$hc);
    return "$lead".Keep($hc);
  }

  $text =preg_replace("/\n[^\\S\n]+$/", "\n", $text);

  $hc=highlight_string("<?php$text?>",true);## Highlighted contents
  $hc=str_replace(array("&lt;?php<br />","?&gt;","<br /><br />",'&amp;'),
                  array("",'',"<br />",'&'),$hc);
  foreach($HiglightColors as $key => $color) {
    if(!empty($color)){
      $hc = str_replace(sprintf($hTpl, ini_get("highlight.{$key}")), sprintf($hTpl, $color), $hc);
    }
  }
  if ($lead == "" || $lead == "\n"){
    $hc=str_replace("<code>","<pre class='escaped'>",$hc);
    $hc=str_replace("</code>","</pre>",$hc);
    return "$lead".Keep($hc);
  }
  $hc=str_replace(array("<code>","</code>"),"",$hc);
  return "$lead<:pre,1>".Keep($hc);
}
php]


Then you can simply use: @@[ php ... php ]@@ (with no space between php and '[' and']') markup as @@[= [@ =]...[= @] =]@@. This provide highlighted coded as above. Example:

[php
echo "Hello worl";
php]

[php echo “Hello worl”; php]



User can customize the output color by defining the @@[=$HiglightColors=]@@ variable:
[php
$HiglightColors=array(
    "string"  => "#dd0000",
    "comment" => "#ff8000",
    "keyword" => "#007700",
    "bg"      => "inherit",
    "default" => "#0000bb",
    "html"    => "black",
    );

!! Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it’s often easier for people to work with “release dates” instead of “version numbers”.

See Also

Contributors



Commentaire

Remark, suggestion post a comment:

Signer en tant qu'auteur 
Website 

Veuillez indiquer un nom d'auteur, et entrer la valeur du code (limite le spam...). Merci.


  • Add This
  • Add
  • Get
  • Google

default