WordPress.org

Forums

WP-Syntax
[PATCH] Add "include" parameter (2 posts)

  1. Constantin Bosneaga
    Member
    Posted 2 years ago #

    I use this plugin on my blog and I like it. Recently I improved it by adding new parameter "include" to the

    <pre> tag. It allows you to include code from filesystem and highlight it as well. It is very handy when you have working example on the site and want to show the source code on the blog.

    patch is below

    $ diff wp-syntax.php.orig wp-syntax.php
    62c62
    <     $ext = 'pre[id|name|class|style|lang|line|escaped|hightlight|src]';
    ---
    >     $ext = 'pre[id|name|class|style|lang|line|escaped|hightlight|src|include]';
    169c169,178
    <     $code = wp_syntax_code_trim($match[6]);
    ---
    >     $include_file = wp_syntax_code_trim($match[6]);
    >     $code = wp_syntax_code_trim($match[7]);
    >
    >     if (!empty($include_file)) {
    >       if (substr($include_file,0,4) == 'http')
    >               $code = file_get_contents($include_file);
    >       elseif (file_exists(ABSPATH.$include_file))
    >               $code = file_get_contents(ABSPATH.$include_file);
    >     }
    >
    217c226
    <         "/\s*<pre(?:lang=[\"']([\w-]+)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|highlight=[\"']((?:\d+[,-])*\d+)[\"']|src=[\"']([^\"']+)[\"']|\s)+>(.*)<\/pre>\s*/siU",
    ---
    >         "/\s*<pre(?:lang=[\"']([\w-]+)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|highlight=[\"']((?:\d+[,-])*\d+)[\"']|src=[\"']([^\"']+)[\"']|include=[\"']([^\"']+)[\"']|\s)+>(.*)<\/pre>\s*/siU",

    http://wordpress.org/extend/plugins/wp-syntax/

  2. parism5
    Member
    Posted 2 years ago #

    Constantin Bosneaga!!

    Your patch work just great!! Thanks a lot. I add a warning message if file not found:

    wp-syntax.patch

    --- wp-syntax.php.orig	2013-01-17 13:16:59.739527313 -0600
    +++ wp-syntax.php	2013-01-18 11:00:33.664527445 -0600
    @@ -59,7 +59,7 @@
     if (!defined("WP_PLUGIN_URL"))  define("WP_PLUGIN_URL",  WP_CONTENT_URL        . "/plugins");
    
     function wp_syntax_change_mce_options($init) {
    -    $ext = 'pre[id|name|class|style|lang|line|escaped|hightlight|src]';
    +    $ext = 'pre[id|name|class|style|lang|line|escaped|hightlight|src|include]';
    
         if ( isset($init["extended_valid_elements"]) )
     	{
    @@ -166,7 +166,17 @@
         $line = trim($match[2]);
         $escaped = trim($match[3]);
         $caption = wp_syntax_caption($match[5]);
    -    $code = wp_syntax_code_trim($match[6]);
    +    $include_file = wp_syntax_code_trim($match[6]);
    +    $code = wp_syntax_code_trim($match[7]);
    +
    +    if (!empty($include_file)) {
    +       if (substr($include_file,0,4) == 'http')
    +               $code = file_get_contents($include_file);
    +       elseif (file_exists(ABSPATH.$include_file))
    +               $code = file_get_contents(ABSPATH.$include_file);
    +       if(!$code)
    +	  $code="WARNING! file :".$include_file."\nNot found.";
    +    }
    
         if ($escaped == 'true') $code = htmlspecialchars_decode($code);
    
    @@ -214,7 +224,7 @@
     function wp_syntax_before_filter($content)
     {
         return preg_replace_callback(
    -        "/\s*<pre(?:lang=[\"']([\w-]+)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|highlight=[\"']((?:\d+[,-])*\d+)[\"']|src=[\"']([^\"']+)[\"']|\s)+>(.*)<\/pre>\s*/siU",
    +        "/\s*<pre(?:lang=[\"']([\w-]+)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|highlight=[\"']((?:\d+[,-])*\d+)[\"']|src=[\"']([^\"']+)[\"']|include=[\"']([^\"']+)[\"']|\s)+>(.*)<\/pre>\s*/siU",
             "wp_syntax_substitute",
             $content
         );

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WP-Syntax
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.