Transforming Plain Text to HTML

Problem

You want to add simple markup to plaintext and turn it into HTML.

Solution

Use RedCloth, written by "why the lucky stiff" and available as the RedCloth gem. It extends Rubys string class to support Textile markup: its to_html method converts Textile markup to HTML.

Heres a simple document:

	require 
ubygems
	require 
edcloth

	text = RedCloth.new %{Who would ever write "HTML":http://www.w3.org/MarkUp/
	markup directly?

	I mean, _who has the time_? Nobody, thats who:

	|_. Person |_. Has the time? |
	| Jake | No |
	| Alice | No |
	| Rodney | Not since the accident |
	}

	puts text.to_html
	# 

Who would ever write # <a href="http://www.w3.org/MarkUp/">HTML</a> # markup directly?

# #

I mean, who has the time? Nobody, that’s who:

# # # # # # # …

The Textile version is more readable and easier to edit.

Discussion

The Textile markup language lets you produce HTML without having to write any HTML. You just add punctuation to plain text, to convey what markup youd like. Paragraph breaks are represented by blank lines, italics by underscores, tables by ASCII-art drawings of tables.

A text-based markup that converts to HTML is very useful in weblog and wiki software, where the markup will be edited many times. Its also useful for hiding the complexity of HTML from new computer users. We wrote this entire book using a Textile-like markup, though it was converted to Docbook instead of HTML.

See Also

  • The RedCloth homepage (http://www.whytheluckystiff.net/ruby/redcloth/)
  • A comprehensive Textile reference (http://hobix.com/textile/)and a quick reference (http://hobix.com/textile/quick.html)
  • You can experiment with Textile markup at the languages homepage (http://www.textism.com/tools/textile/)
  • Markdown (http://daringfireball.net/projects/markdown/)is another popular simple markup language for plain text; you can turn Markdown text to XHTML with the BlueCloth gem (project page: http://www.deveiate.org/projects/BlueCloth); because BlueCloth and RedCloth both define String#to_html, its not easy to use them both in the same program
Person Has the time?


Strings

Numbers

Date and Time

Arrays

Hashes

Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming

XML and HTML

Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration



Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net