# \[ANN\] ClothRed 0.3.0 released

**URL:** <https://rubytalk.org/t/ann-clothred-0-3-0-released/36888>\
**Category:** ruby-talk\
**Created:** [12 April 2007 15:53 UTC](https://rubytalk.org/t/ann-clothred-0-3-0-released/36888 "2007-04-12T15:53:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [12 April 2007 15:53 UTC](https://rubytalk.org/t/ann-clothred-0-3-0-released/36888/1 "2007-04-12T15:53:37Z")

</div>

Or: TTD is fun.

= ClothRed HTML 2 Textile converter

== What it is

A library to convert HTML into Textile markup for use, for example, with  
RedCloth.

== Requirements

All you need is Ruby.

== Get it

Available as a gem on RubyForge:  
gem install ClothRed

Or download from RubyForge:  
[http://rubyforge.org/frs/?group\_id=3427](http://rubyforge.org/frs/?group_id=3427)

Or get the source:  
svn checkout svn://viewvc.rubyforge.mmmultiworks.com/var/svn/clothred

== New in this release:  
&nbsp;&nbsp;&nbsp;\* Support for some HTML entities  
&nbsp;&nbsp;&nbsp;\* Support for tables

== Features

This is alpha software, and only a few Textile rules have been  
implemented yet:  
&nbsp;&nbsp;\* font markup and weight (\<b\>, \<strong\>, ...)  
&nbsp;&nbsp;\* text formatting (\<sub\>, \<sup\>, \<ins\>,\<del\>)  
&nbsp;&nbsp;\* Support for headings  
&nbsp;&nbsp;\* Support for paragraphs and \<blockquote\>  
&nbsp;&nbsp;\* Support for Textile entities

== Usage

require 'clothred'

text = ClothRed.new("\<b\>Bold\</b\> \<em\>HTML\</em\>!")  
text.to\_textile

== Get Help  
Feel free to contact me, or peruse the homepage.

&nbsp;&nbsp;\* [http://clothred.rubyforge.org/](http://clothred.rubyforge.org/)  
&nbsp;&nbsp;\* [http://rubyforge.org/projects/clothred/](http://rubyforge.org/projects/clothred/)

> **···**
>
> --  
> Phillip "CynicalRyan" Gawlowski  
> [http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)  
> [http://clothred.rubyforge.org](http://clothred.rubyforge.org)
> 
> Rule of Open-Source Programming #11:
> 
> When a developer says he will work on something, he or she means  
> "maybe".

---

<div class="post-metadata">

**Author:** ![Adriano\_Ferreira](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Adriano\_Ferreira](https://rubytalk.org/u/Adriano_Ferreira)\
**Post date:** [13 April 2007 13:42 UTC](https://rubytalk.org/t/ann-clothred-0-3-0-released/36888/2 "2007-04-13T13:42:07Z")

</div>

I think you will be bitten pretty fast with the current approach of  
doing only text substitutions while translating HTML into Textile.

For example, HTML is pretty insensitive to some spaces in between:

&nbsp;&nbsp;&nbsp;&nbsp;\<h1\>main title\</h1\> \<h2\>subtitle\</h2\>

is just as good as

&nbsp;&nbsp;&nbsp;&nbsp;\<h1\>main title\</h1\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<h2\>subtitle\</h2\>

But in Textile,

&nbsp;&nbsp;&nbsp;&nbsp;h1. main title

&nbsp;&nbsp;&nbsp;&nbsp;h2. subtitle

is not the same as

&nbsp;&nbsp;&nbsp;&nbsp;h1. main title h2. subtitle

or even

&nbsp;&nbsp;&nbsp;&nbsp;h1. main title  
&nbsp;&nbsp;&nbsp;&nbsp;h2. subtitle

In the current implementation, ClothRed behaves like that:

$ irb  
irb(main):001:0\> require 'rubygems'  
=\> true  
irb(main):002:0\> require 'clothred'  
=\> true  
irb(main):003:0\> t = ClothRed.new("\<h1\>Foo\</h1\> \<h2\>Bar\</h2\>")  
=\> "\<h1\>Foo\<h1\> \<h2\>Bar\</h2\>"  
irb(main):004:0\> t.to\_textile  
=\> "h1. Foo h2. Bar"

but that's pretty easy to work around with the simple patch attached.  
I just replaced "" as a substitution to HTML \</h1\>, \</h2\>, ... by  
"\n\n" producing the necessary paragraph breaks for Textile.  
"test/test\_headings.rb" had to be fixed and I also wrote  
"test/test\_misc.rb" as a test script for HTML with more than one tag  
in it.

The substitution approach will not work quite right for HTML where  
closing tags are missing. The algorithm will never understand when the  
tags were closed. So this is somewhat limited currently to XHTML which  
demands closing tags.

I think the suggestion of using a HTML parser (like Hpricot) to do  
this conversion will impose itself pretty soon.

Thanks for the inspiring work.

Adriano Ferreira.

[clothred.diff](https://rubytalk.org/uploads/short-url/rQA2P1OgrAHL3QfRaiWxV9OADTn.diff) (2.36 KB)

> **···**
>
> On 4/12/07, Phillip Gawlowski \<cmdjackryan@googlemail.com\> wrote:
> 
> > Or: TTD is fun.
> > 
> > = ClothRed HTML 2 Textile converter
> > 
> > == What it is
> > 
> > A library to convert HTML into Textile markup for use, for example, with  
> > RedCloth.
> > 
> > == Requirements
> > 
> > All you need is Ruby.
> > 
> > == Get it
> > 
> > Available as a gem on RubyForge:  
> > gem install ClothRed
> > 
> > Or download from RubyForge:  
> > [http://rubyforge.org/frs/?group\_id=3427](http://rubyforge.org/frs/?group_id=3427)
> > 
> > Or get the source:  
> > svn checkout svn://viewvc.rubyforge.mmmultiworks.com/var/svn/clothred
> > 
> > == New in this release:  
> > &nbsp;&nbsp;&nbsp;\* Support for some HTML entities  
> > &nbsp;&nbsp;&nbsp;\* Support for tables
> > 
> > == Features
> > 
> > This is alpha software, and only a few Textile rules have been  
> > implemented yet:  
> > &nbsp;&nbsp;\* font markup and weight (\<b\>, \<strong\>, ...)  
> > &nbsp;&nbsp;\* text formatting (\<sub\>, \<sup\>, \<ins\>,\<del\>)  
> > &nbsp;&nbsp;\* Support for headings  
> > &nbsp;&nbsp;\* Support for paragraphs and \<blockquote\>  
> > &nbsp;&nbsp;\* Support for Textile entities
> > 
> > == Usage
> > 
> > require 'clothred'
> > 
> > text = ClothRed.new("\<b\>Bold\</b\> \<em\>HTML\</em\>!")  
> > text.to\_textile
> > 
> > == Get Help  
> > Feel free to contact me, or peruse the homepage.
> > 
> > &nbsp;&nbsp;\* [http://clothred.rubyforge.org/](http://clothred.rubyforge.org/)  
> > &nbsp;&nbsp;\* [http://rubyforge.org/projects/clothred/](http://rubyforge.org/projects/clothred/)
> > 
> > --  
> > Phillip "CynicalRyan" Gawlowski  
> > [http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)  
> > [http://clothred.rubyforge.org](http://clothred.rubyforge.org)
> > 
> > Rule of Open-Source Programming #11:
> > 
> > When a developer says he will work on something, he or she means  
> > "maybe".

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [13 April 2007 16:24 UTC](https://rubytalk.org/t/ann-clothred-0-3-0-released/36888/3 "2007-04-13T16:24:02Z")

</div>

Adriano Ferreira wrote:

> but that's pretty easy to work around with the simple patch attached.  
> I just replaced "" as a substitution to HTML \</h1\>, \</h2\>, ... by  
> "\n\n" producing the necessary paragraph breaks for Textile.  
> "test/test\_headings.rb" had to be fixed and I also wrote  
> "test/test\_misc.rb" as a test script for HTML with more than one tag  
> in it.

Thanks. I'll add your patch ASAP, and make a new release.

> The substitution approach will not work quite right for HTML where  
> closing tags are missing. The algorithm will never understand when the  
> tags were closed. So this is somewhat limited currently to XHTML which  
> demands closing tags.

And it's buggy, I've noticed, as it ignores self-closing tags like \<br /\>. I'll fix that together with your patch.

> I think the suggestion of using a HTML parser (like Hpricot) to do  
> this conversion will impose itself pretty soon.

Probably, but I'll have to play with Hpricot first, to see if it can do what is needed (after a short skim, it allows to convert HTML4 into XHTML1.0, which will make it easier). Trouble is, I want ClothRed as dependency free as I can. So maybe I'll redistribute Hpricot in non-gem distributions (if that's possible, haven't checked Hpricot's license yet).

> Thanks for the inspiring work.

No problem. I'll need that library for my own ideas. 😉

> **···**
>
> --  
> Phillip "CynicalRyan" Gawlowski  
> [http://cynicalryan.110mb.com/](http://cynicalryan.110mb.com/)  
> [http://clothred.rubyforge.org](http://clothred.rubyforge.org)
> 
> Rule of Open-Source Programming #48:
> 
> The number of items on a project's to-do list always grows or remains  
> constant.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [13 April 2007 19:05 UTC](https://rubytalk.org/t/ann-clothred-0-3-0-released/36888/4 "2007-04-13T19:05:34Z")

</div>

I think what you're going to find is, parsing tag-soup HTML is harder than  
you think - especially if your goal with ClothRed is to parse \*arbitrary\*  
tag-soup HTML from arbitrary sources.

> **···**
>
> On Sat, Apr 14, 2007 at 01:24:02AM +0900, Phillip Gawlowski wrote:
> 
> > \>I think the suggestion of using a HTML parser (like Hpricot) to do  
> > \>this conversion will impose itself pretty soon.
> > 
> > Probably, but I'll have to play with Hpricot first, to see if it can do  
> > what is needed (after a short skim, it allows to convert HTML4 into  
> > XHTML1.0, which will make it easier). Trouble is, I want ClothRed as  
> > dependency free as I can.
