Welcome Guest, Not a member yet? Register   Sign In
Mini Textile Library - Updated
#1

[eluser]Lone[/eluser]
If you are not familiar with what textile is please click here. I love using Textile for the output of textarea boxes on any websites that we do as it is a simple markup language that still makes some sense unformatted.


TextilePHP and CI
I actually started off usi_ng the TextilePHP class with instructions for implementation in CI from the CI Wiki. But one offsetting thing for me was the sheer size of the class and the fact that it add another 1.2mb to memory usuage in CI.


Let's minimify!
I have come to the realisation that there are four main features of Textile that I only use so decided to make a small helper file that just carried on these main features that I used it for.


So what can it do?
You send the helper function a string that has been written using the textile style and it will return the HTML for output. Currently it will interpret only the following textile formats:

- Paragraphs (<p&gtWink
- Line breaks (<br /&gtWink
- Strong/bold (<strong&gtWink
- Emphasis (<em&gtWink


So how do I use it?
First of all save the following code snippet to your application/libraries dir as 'Minitextile.php'

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

class Minitextile {

    // Generates <em> tags
    function em($text) {
        return preg_replace('/\_([^\*]+)\_/','<em>$1</em>',$text);
    }

    // Generates <strong> tags
    function strong($text) {
        return preg_replace('/\*([^\*]+)\*/','<strong>$1</strong>',$text);
    }
    
    // Generates <br /> tags
    function br($text) {
        return preg_replace('/(.+)\n(.+)/m','$1<br />$2', $text);
    }
    
    // Generates paragraphs - note: needs to have linebreaks fixed first
    function paragraph($text) {
        $text = $this->fix_linebreaks($text);
        $paragraphs = explode("\n\n", $text);
        $output = '';
        foreach($paragraphs as $paragraph) {
            $output .= "\n<p>".$paragraph."</p>\n";
        }
        return $output;
    }
    
    /**
     * Fixes linebreaks that can be entered into a textarea from different systems - used for paragraph
     * windows = \r\n
     * unix = \n
     * mac = \r
     */    
    function fix_linebreaks($text) {
        return str_replace(array("\r\n", "\r", "\n"), "\n", $text);
    }
    
    function process($text) {
        $text = $this->em($text);
        $text = $this->strong($text);
        $text = $this->paragraph($text);
        $text = $this->br($text);
        return $text;
    }
    
    function strip($text) {
        $text = $this->process($text);
        $text = strip_tags($text);
        return $text;
    }

}
?&gt;

Now you need to load the library, you can either autoload it to your autoload.php (recommended) or just use:
Code:
$this->load->library('Minitextile');

To use the function it is as easy as:
Code:
echo $this->Minitextile->process($string);


I plan to extend this down the track but it serves its purpose for now and Im certain it will be handy for some people on here.

Also, if you find any bugs please let me know in here!


Messages In This Thread
Mini Textile Library - Updated - by El Forum - 01-23-2008, 12:59 AM
Mini Textile Library - Updated - by El Forum - 01-23-2008, 01:40 AM
Mini Textile Library - Updated - by El Forum - 01-23-2008, 02:05 AM
Mini Textile Library - Updated - by El Forum - 01-23-2008, 02:38 AM
Mini Textile Library - Updated - by El Forum - 01-23-2008, 02:58 AM
Mini Textile Library - Updated - by El Forum - 01-23-2008, 04:54 AM
Mini Textile Library - Updated - by El Forum - 01-23-2008, 07:36 AM
Mini Textile Library - Updated - by El Forum - 01-23-2008, 07:42 AM
Mini Textile Library - Updated - by El Forum - 01-28-2008, 03:45 PM
Mini Textile Library - Updated - by El Forum - 05-16-2008, 06:15 AM
Mini Textile Library - Updated - by El Forum - 05-18-2008, 04:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB