Easy Text Formatting with Textile

When I’m writing content for the web, I hate dealing with HTML coding. HTML is rather verbose as a markup language, and having to include closing tags is messy and error-prone.

We’re all pretty much stuck with delivering HTML code from our web sites, but that doesn’t mean we have to write in it. There are various markup approaches that are superior to HTML when it comes to content creation, and which can be used as source code from which to create HTML. Two that are widely used are Textile and Markdown.

Textile was originally developed by Dean Allen for the TextPattern content management system and is now widely used in wikis and blogs. Markdown, created by John Gruber and Aaron Swartz, is an alternative that is conceptually similar but with a different syntax. Many of the popular blog engines provide a choice of Textile or Markdown (or HTML) for posts and comments (this site offers Textile).

In this article, I’ll show the basics of Textile markup and how easy it is to use it in Rails applications.

What is Textile?

Textile strives to make text markup as clean and simple as possible. You don’t have to put <p> in front of each paragraph; bare text is automatically surrounded by <p> and </p> tags. To make a first-level heading, just start the line with @h1. @. For example:

h1. This is a heading and will be wrapped in <h1> and </h1> tags

This is a text paragraph and will be wrapped in <p> and </p> tags.

This is another paragraph.

And I bet you can guess how to make a second-level heading, or a third-level heading.

You never need to use close tags; a pair of carriage returns automatically triggers the appropriate end tag. Quotes are automatically converted to open and close quotes; hyphens are converted to n-dashes (if there is a space on either side); and double hyphens are converted to m-dashes.

To make text bold, surround it with asterisks; to make it italic, with underscores:

*This text will render in bold*
_This text will render in italic*
_*This text will render in bold italic*_

To add a link to a piece of text, surround the text in quotes, and follow it by a colon and the URL:

"text to be linked":http://thisistheurl.com

To include an image, type its URL surrounded by exclamation points:

!http://domain.com/path/to/image.jpg!

To create a bullet list, start each line with an asterisk and a space, like this:

* First item
* Second item
* Third item

Want a nested bullet list? Just indent the asterisk by three spaces.

There’s more, but this should give you a feel for it. You can also use any HTML you want for things that Textile doesn’t cover — HTML code is simply passed through. For more details, see the Textile Reference. You can try out Textile online at the Textile home page.

Implementing Textile

There are Textile implementations for various environments. In my case, using Ruby on Rails, Textile is implemented by whytheluckystiff ’s RedCloth gem. (The current implementation does have some flaws, and a much-enhanced version called Super RedCloth is in development.) To install, just enter the following in the console:

gem install RedCloth

With RedCloth installed, you create a new RedCloth object and pass some Textile-marked-up text to it:

textile_styled_text ="h1. This is an example of a heading in Textile"
textile_object = RedCloth.new(textile_styled_text)

Then, when you want to render this as HTML, you simply use:

html = textile_object.to_html

Making Textile Even Easier

That’s pretty easy, but you can make it even easier. There’s a Rails helper “textilize,” but thanks to the acts_as_textiled plug from Chris Wanstrath, it’s even easier than that. Install this plugin:

ruby script/plugin install
 svn://errtheblog.com/svn/plugins/acts_as_textiled

And then all you have to do is declare a model attribute as Textile-formatted in your model class:

acts_as_textiled  :field_name

That’s it — everything just works! (Remember, though, that you need to restart the server for the model change to take effect.) Textile marked up text is stored in the database. Form fields automatically show unrendered textile. But any other use of the model attribute automatically delivers the rendered HTML instead of the Textile source.

Markup without Coding

You can also simplify the entry of Textile markup by using the textile_editor_helper from Dave Olsen and Chris Scharf. This plug-in renders a set of icons for the common markup tasks in your form view, just above the text entry area:

When you click an icon, JavaScript code inserts the appropriate markup into your text.

To use this, first install the plugin:

ruby script/plugin install
 http://svn.webtest.wvu.edu/repos/rails/plugins/textile_editor_helper

rake textile_editor_helper:install

And then replace the text_area tag in the form in which the user enters the Textile-formatted text with textile_editor:

<%= textile_editor 'object', 'field' -%>

textile_editor takes all the same options as text_area.

and at the end of the form add:

<%= textile_editor_initialize -%>

Finally, if you don’t already include prototype.js, you’ll need to add that to your layout:

<%= javascript_include_tag 'prototype.js' %>

Presto! Now you have a row of icons above the text area so your users don’t have to remember any markup at all. They’ll still see the markup in the text area (this is not a wysiwyg editor), which will help them learn the markup code.


Add Your Comments

(not published)

User experience of Textile

From: Cedders, 09/08/10 03:04 AM

My experience of using Textile (mostly in Drupal) is infuriating. Wiki markup I can see some point in, since it allows easy [[links]] and {{templates}}, and CKEditor variants please people expecting wysiwyg, but Textile is a completely superfluous and confusing layer on top of HTML. Sure, adding automatic paragraph breaks makes things easier for users, and in theory something like BBCode (although also rather pointless) can be an alternative to writing code to filter HTML. But the Textile parsers can trigger unexpectedly on punctuation, and don't seem flexible enough to allow any way around it, so it's really not suitable for any type of technical documentation, and can trap even ordinary text, such as enumerating a list (a) (b) (c).

_This text will render in italic*

From: Herbert Kruitbosch, 05/01/10 05:47 AM

_This text will render in italic* Shouldn't this be: _This text will render in italic_ It would make more sense :)

Sorry

From: Barry Darcy, 04/21/10 07:41 AM

So sorry... the submit button wouldnt refresh, i didnt realise i clicked it so many times until refreshing the page. Sorry

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements.

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements.

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:39 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:38 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:38 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Paragragh format

From: Barry Darcy, 04/21/10 07:38 AM

h1.Testing heading @h1.Testing heading I am new to rails and i am falling in love with it more and more each day, i know that textile is available on other langs but implementation is so easy with rails as is with every other rails implements

Facing one bug in textile editor

From: Rajiv Kaushal, 02/13/09 12:35 AM

Hi, Editor is really good and and appears instantly. But I am facing one prblem in it. i.e 'Console is undefined'. Facing this error for both browser IE and Firefox. Would you please tell, how can I fix it. Regards, Rockey

even easier textile

From: Eric Draut, 01/20/09 04:58 AM

I recently submitted a new feature to RedCloth which is included in the latest release. Now you can use textile like this: <%=t "*bold*" %>. This allows flexibility in that you can mark up any arbitrary string, whether or not it's tied to a particular field.

textile_editor_helper in the In-place editing

From: Marius, 09/24/08 01:53 PM

Hi Michael, I wanted to use the textile_editor_helper in the In-place editing of the Lesson 14 of your screencast.Just to have some kind of wysiwyg editor in the In-place editing, but I can't figure out how to implement it. Could give some tips? Thanks in advance

console error's with textile_editor_helper

From: pk, 08/05/08 09:27 AM

Just an FYI in case you have problems with this. I ran into some "console" errors implementing the textile_editor_helper. The only way I was able to fix it was by going in and commenting out the lines referring to "console" in the textile-editor.js.

 

Sponsored By

New Relic Rails Performance Monitoring