Welcome Guest, Not a member yet? Register   Sign In
TextiLite - Another lightweight version of Textile
#1

[eluser]evanwalsh[/eluser]
I decided to make a version of Textile that only supported a small set of HTML tags because I needed it for a forum app I'm making. Here's the code for the library:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class TextiLite{
    
    /*
        TextiLite:
            A lightweight version of Textile built with PHP by Evan Walsh
            Version 001

        Based on the work of:
            http://codeigniter.com/wiki/BBCode_Helper/
            http://ellislab.com/forums/viewthread/69615/
            
        Supports:
            <br/> by the way of newline
            *Text* => <strong>Text</strong>
            _Text_ => <em>Text</em>
            !http://image.url! => <img src="http://image.url"/>
            "Text":http://text.url => <a href="http://text.url" title="Text">Text</a>
    */

    function paragraph($text){
        $paragraphs = explode("\n\n", $text);
        $output = null;
        foreach($paragraphs as $paragraph) {
            $output .= "\n<p>".$paragraph."</p>\n";
        }
        return $output;
    }

    function textile($text = null){
        $regex = array(
            '/(.+)\n(.+)/',
            '/\*([^\*]+)\*/',
            '/\_([^\*]+)\_/',
            '/(!)((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))(!)/',
            '/(")(.*?)(").*?((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))/',
        );
        $replace = array(
            "$1<br/>$2",
            "<strong>$1</strong>",
            "<em>$1</em>",
            "<img src=\"$2\"/>",
            "<a href=\"$4\" title=\"$2\">$2</a>",
        );
        return preg_replace($regex,$replace,$text);
    }
    
    function process($text){
        $text = strip_tags($text);
        $text = $this->paragraph($text);
        $text = $this->textile($text);
        return $text;
    }

}

?&gt;

Installation:
Copy the above code into a file named Textilite.php in your app's libraries folder

Usage:
Code:
$this->load->library("textilite");
echo $this->textilite->process("*Test*\n\n_Test_");

This would render:
Code:
<p><strong>Test</strong></p>

<p><em>Test</em></p>

Tell me what you think. I have encountered some errors and I plan on fixing them very, very soon.
#2

[eluser]Lone[/eluser]
Thanks for the link to our minitextile and I just wanted to make a couple of comments.

First of all I am just curious as to why to make a new library rather then suggestions on our own - the reason I ask is not because we don't want another but just curious as to whether you found in many flaws in our own. Infact I ran a quick test between ours and yours and saw a definite speed increase (when looped 10000 times) in yours compared to ours. I also really like that array usuage for the preg_replace! Didnt know you could do that Tongue

However that does steep me into the next comment, the string I tested it on for yours actually intepreted it incorrectly.. below is the string:

Code:
$sample =
'This is some *sample* text to
test the time it does take

for this *to be processed* through *the* textile library

hurray!';

And here is the output (note the extra <br /> after the <p> tag

Code:
<p>This is some <strong>sample</strong> text to
<br/>test the time it does take</p>

<p><br/>for this <strong>to be processed</strong> through <strong>the</strong> textile library</p>

<p><br/>hurray!</p>

As ours was slower I wonder if it was because ours was able to interpret better and hence needed more overhead?
#3

[eluser]evanwalsh[/eluser]
I am aware that line breaks and such aren't handled correctly. Also, putting a link and an image in the same line can mess things up, too. My library combines yours and the BBCode helper, so I thought it was too different to suggest changes. However, I am willing to figure out a way to make these changes and incorporate them into your library. Your work did inspire me a great deal, so I suppose I owe it to you.

Thanks for letting me know what you think Big Grin

EDIT: I fixed the line breaks and the image issue, to my knowledge




Theme © iAndrew 2016 - Forum software by © MyBB