Welcome Guest, Not a member yet? Register   Sign In
Is there a library that ....
#1

[eluser]ShannenName[/eluser]
I have this situation:
I want users to post code and for the system to automatically escape and format anything in a
Code:
[code]
tag.
EXACTLY like CI forums code box does.
Is there a library or plugin or something for this?

I would look this up for myself but I don't know what this process is called!
#2

[eluser]WanWizard[/eluser]
In the current version of our CMS we use GeSHI for code processing and syntax hightlighting.

Converting it into a CI library is still on the todo list (at the bottom), but since it's a class, making it available as a CI library should be a breeze.
#3

[eluser]ShannenName[/eluser]
How would I make it a class and am I allowed to do this?
#4

[eluser]cahva[/eluser]
There is an easy way to get GeSHi working right away.

First add autoloading(PHP5 feature) as described here. Basically you only need to add that __autoload function to your config.php.

Then download geshi from the site, and copy geshi.php and geshi folder to application/libraries. Rename geshi.php to GeSHi.php (thats the name of the class and autoload will find it also in *NIX systems which are case sensitive).

Then you're ready to go. Test it:
Code:
<?php
class Test extends Controller {

    function __construct()
    {
        parent::__construct();
    }
    
    function index()
    {
        $code = '
$foo = "foo";
echo "foo is {$foo}";

$testarray = array(
    "CI" => "rocks!",
    "PHP" => "rox!"
);

foreach ($testarray as $k=>$v)
{
    echo $k." ".$v."\n";
}
';
        $language = 'php';

        $geshi = new GeSHi($code, $language);

        echo $geshi->parse_code();
    }
}

For the bbcode, you can look into this basic function to parse bbcode tags:
http://www.php.net/manual/en/function.bb....php#93349

Create a helper of that function so you can use it anywhere you like.
#5

[eluser]ShannenName[/eluser]
I don't get what to put in the autoload function.
I get this far then get stuck:
Code:
function __autoload
As you can see not very far.
#6

[eluser]ShannenName[/eluser]
delete this
#7

[eluser]cahva[/eluser]
So you got it to work? Smile
#8

[eluser]ShannenName[/eluser]
Oh no just that post wasn't sent properply. It was a double post but my internet cut out and something happened to it.
Problem not solved =(
#9

[eluser]cahva[/eluser]
Ok Smile

So as in the blog post, just add this bit of code to file application/config/config.php:
Code:
/*
| -------------------------------------------------------------------
|  Native Auto-load
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
*/
function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'libraries/'. $class . EXT );
    }
}

What that autoload does,is that when you put code anywhere in controllers, models, helpers or libraries like this:
Code:
$foo = new Foo();
.. it will try to find file called Foo.php in your application/libraries folder. So you wont have to create a "proper" library for CI if you dont want. This is very neat for 3rd party classes.
#10

[eluser]pickupman[/eluser]
I downloaded the geshi source and converted it to a CI library. Download the attached zip file and place into your /application/libraries folder. Here's how you can use it:
Code:
$this->load->library('geshi');
$config['set_language'] = 'php'; //REQUIRED set language to parse
$config['set_source'] = 'foreach($test as $item){ echo "hello";}'; //REQUIRED set source code to parse
$this->geshi->initialize($config);

echo $this->geshi->output(); //output from geshi alias of parse_code() method

The file is too big to attach to the post, so I have posted it on my website. Dowload Here




Theme © iAndrew 2016 - Forum software by © MyBB