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!
#2

[eluser]xwero[/eluser]
If you find textile too big maybe you could try php markdown (full version is 32 kb) or a bbcode class. I think you should have a least header, underline, link and quote markup to have a basic markup parser.

Is there a reason why you let the $text variable linger in the memory?
#3

[eluser]Lone[/eluser]
Thanks for the suggestion on PHP Markdown - but what I am trying to achieve with this is just a simple output of text rather then a full markup parser. The best thing is that this is 1kb of extra code as opposed to a full parser that is being used for just some of its simple functionality on a commonly loaded page.

The real reason for using this as opposed to others is that I ideally use this for a 'description' field. So in the site we are working on at the moment the user can enter a description for the item their selling and is displayed on the site after being parsed by Mini Textile.

Instead of replacing every double linebreak ('/n/n') with a &lt;br /&gt; we use a paragraph - Ive just added the strong and emphasis tags as a little add on.

Not sure what you mean by the $text variable - I thought it would be destroyed automatically after the function is finished?
#4

[eluser]xwero[/eluser]
I can understand why you want a text parser with a more correct html syntax than the nl2br php function but when you talk about a mini textile helper you enter markup parser terrain. The way i would do it is create a function that only takes care of the paragraph part and one function that takes care of the markup.
Code:
function paragraph($text)
{
    $paragraphs = explode("\n\r\n", $text);
    $text = '';
    foreach($paragraphs as $paragraph) {
       // Make line breaks <br />
       $paragraph = preg_replace('/(.+)\n(.+)/m','$1<br />$2', $paragraph);
       // Save output
       $text .= "\n<p>".$paragraph."</p>\n";
    }
        
    return $text;
}

function textile($text)
{
   // Make STRONG text
    $text = preg_replace('/\*([^\*]+)\*/','<strong>$1</strong>',$text);
    // Make EM text
    $text = preg_replace('/\_([^\*]+)\_/','<em>$1</em>',$text);
    return $text;
}
The paragraph parsing is universal and it's easier to switch from one markup parsing to another (BBcode, wiki).

But i find it strange that you don't object to the size of php markdown. The textile class itself is about that size and it hasn't any depenencies.

I think it's good practice to commit the changes to the variables that already are defined instead of creating a new and do nothing with existing one.
#5

[eluser]Lone[/eluser]
Sorry, with PHP Markdown I did mean to make mention that it was around the same size/overheads as well. Was interesting to see another concept for markup languages like textile etc. Smile

I think that is a fair call on that naming it mini textile makes it like it is entering a full parser as such - but the main reason why was becuase it uses the textile 'style' of markup.

Not a bad suggestion there on the split of the two functions as well - ideally I would have done this as a class/library but just felt it was a bit small to justify it, I would like to wait and see any other suggestions people might have. Would be nice to form this into a HTML output library that is loosely based on textile and able to ouput fully parsed html or just single features such as the paragraph function.

With the usuage of the $text var my main reason for leaving it is that I have got into the habit of not modifying the original inputs variables in a function 'just in case' you need to re-use them again later in the function. But I can see it from your point of view as well whereby the input goes in and them comes out modified but the same name.
#6

[eluser]Craig A Rodway[/eluser]
I agree Textile can be too heavy for some sites, but also see the benefits of having full use of the Textile class. For smaller sites I think BBCode is ideal for the basic line breaks, paragraphs, links and text emphasis.
#7

[eluser]Lone[/eluser]
Now updated to be a library.

The key update as well was that paragraphs were not being formed properly, the problem comes from the problem of each OS have different ASCII chars for linebreak:

windows = \r\n
unix = \n
mac = \r
#8

[eluser]xwero[/eluser]
Looks nice and extendable
Code:
class Minibbcode extends Minitextile
{
    // Generates <em> tags
    function em($text) {
        return preg_replace('/\[i\]([^\*]+)\[\/i\]/','<em>$1</em>',$text);
    }

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

[eluser]Lone[/eluser]
Jsut a quick update - added the strip function to strip all the textile elements from the content body.

Had to use it for a short description.
#10

[eluser]Unknown[/eluser]
I noticed what might be a bug just now.

When the script tries to interpret _(hello)_ there, all the text goes italic instead of just the text in the paranthesis.

Any solutions?




Theme © iAndrew 2016 - Forum software by © MyBB