<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>ext/toc.c</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -16,18 +16,9 @@ rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self)
     VALUE buf = rb_str_buf_new(4096);
     FILE *stream = rb_str_io_new(buf);
 
-    /* compile flags */
-    int flags = MKD_TABSTOP | MKD_NOHEADER;
+    int flags = rb_rdiscount__get_flags(self);
 
-    /* smart */
-    if ( rb_funcall(self, rb_intern(&quot;smart&quot;), 0) != Qtrue )
-        flags = flags | MKD_NOPANTS;
-
-    /* filter_html */
-    if ( rb_funcall(self, rb_intern(&quot;filter_html&quot;), 0) == Qtrue )
-        flags = flags | MKD_NOHTML;
-
-    MMIOT *doc = mkd_string(RSTRING(text)-&gt;ptr, RSTRING(text)-&gt;len, flags);
+    MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags);
     markdown(doc, stream, flags);
 
     fclose(stream);
@@ -35,10 +26,55 @@ rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self)
     return buf;
 }
 
+static VALUE
+rb_rdiscount_toc_content(int argc, VALUE *argv, VALUE self)
+{
+    int flags = rb_rdiscount__get_flags(self);
+  
+    /* grab char pointer to markdown input text */
+    VALUE text = rb_funcall(self, rb_intern(&quot;text&quot;), 0);
+    Check_Type(text, T_STRING);
+    
+    /* allocate a ruby string buffer and wrap it in a stream */
+    VALUE buf = rb_str_buf_new(4096);
+    FILE *stream = rb_str_io_new(buf);
+    
+    MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags);
+    mkd_compile(doc, flags);
+    mkd_generatetoc(doc, stream);
+    
+    fclose(stream);
+    
+    return buf;
+}
+
+int rb_rdiscount__get_flags(VALUE ruby_obj)
+{
+  /* compile flags */
+  int flags = MKD_TABSTOP | MKD_NOHEADER;
+
+  /* smart */
+  if ( rb_funcall(ruby_obj, rb_intern(&quot;smart&quot;), 0) != Qtrue )
+      flags = flags | MKD_NOPANTS;
+
+  /* filter_html */
+  if ( rb_funcall(ruby_obj, rb_intern(&quot;filter_html&quot;), 0) == Qtrue )
+      flags = flags | MKD_NOHTML;
+      
+  /* generate_toc */
+  if ( rb_funcall(ruby_obj, rb_intern(&quot;generate_toc&quot;), 0) == Qtrue)
+    flags = flags | MKD_TOC;
+    
+  return flags;
+  
+}
+
+
 void Init_rdiscount()
 {
     rb_cRDiscount = rb_define_class(&quot;RDiscount&quot;, rb_cObject);
     rb_define_method(rb_cRDiscount, &quot;to_html&quot;, rb_rdiscount_to_html, -1);
+    rb_define_method(rb_cRDiscount, &quot;toc_content&quot;, rb_rdiscount_toc_content, -1);
 }
 
 /* vim: set ts=4 sw=4: */</diff>
      <filename>ext/rdiscount.c</filename>
    </modified>
    <modified>
      <diff>@@ -41,6 +41,9 @@ class RDiscount
   # RedCloth compatible line folding -- not used for Markdown but
   # included for compatibility.
   attr_accessor :fold_lines
+  
+  # Enable Table Of Contents generation
+  attr_accessor :generate_toc
 
   # Create a RDiscount Markdown processor. The +text+ argument
   # should be a string containing Markdown text. Additional arguments may be</diff>
      <filename>lib/rdiscount.rb</filename>
    </modified>
    <modified>
      <diff>@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
     ext/rbstrio.h
     ext/rdiscount.c
     ext/resource.c
+    ext/toc.c
     lib/markdown.rb
     lib/rdiscount.rb
     rdiscount.gemspec</diff>
      <filename>rdiscount.gemspec</filename>
    </modified>
    <modified>
      <diff>@@ -7,9 +7,9 @@ MARKDOWN_TEST_DIR = &quot;#{File.dirname(__FILE__)}/MarkdownTest_1.0.3&quot;
 
 class MarkdownTest &lt; Test::Unit::TestCase
 
-  def test_that_extension_methods_are_present_on_markdown_class
-    assert Markdown.instance_methods.include?('to_html'),
-      &quot;Markdown class should respond to #to_html&quot;
+  def test_that_extension_methods_are_present_on_markdown_class  
+      assert Markdown.instance_methods.map{|m| m.to_s }.include?('to_html'),
+        &quot;Markdown class should respond to #to_html&quot;
   end
 
   def test_that_simple_one_liner_goes_to_html</diff>
      <filename>test/markdown_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -27,4 +27,16 @@ class RDiscountTest &lt; Test::Unit::TestCase
     rd = RDiscount.new(%(# Heading\n\n&quot;Quoted text&quot;), :smart)
     assert_equal %(&lt;h1&gt;Heading&lt;/h1&gt;\n\n&lt;p&gt;&amp;ldquo;Quoted text&amp;rdquo;&lt;/p&gt;\n), rd.to_html
   end
+  
+  def test_that_generate_toc_sets_toc_ids
+    rd = RDiscount.new(&quot;# Level 1\n\n## Level 2&quot;, :generate_toc)
+    assert rd.generate_toc
+    assert_equal %(&lt;h1 id=&quot;Level+1\&quot;&gt;Level 1&lt;/h1&gt;\n\n&lt;h2 id=&quot;Level+2\&quot;&gt;Level 2&lt;/h2&gt;\n), rd.to_html
+  end
+  
+  def test_should_get_the_generated_toc
+    rd = RDiscount.new(&quot;# Level 1\n\n## Level 2&quot;, :generate_toc)
+    exp = %(&lt;ul&gt;\n &lt;li&gt;&lt;a href=&quot;#Level+1&quot;&gt;Level 1&lt;/a&gt;\n  &lt;ul&gt;\n  &lt;li&gt;&lt;a href=&quot;#Level+2&quot;&gt;Level 2&lt;/a&gt;  &lt;/li&gt;\n  &lt;/ul&gt;\n &lt;/li&gt;\n &lt;/ul&gt;)
+    assert_equal exp, rd.toc_content.strip
+  end
 end</diff>
      <filename>test/rdiscount_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>6090614bb5936647d5b6c213b58ea0fc44f8d131</id>
    </parent>
    <parent>
      <id>82efdbb6f4b913c8be86659809d72c98549d0d9d</id>
    </parent>
  </parents>
  <author>
    <name>Ryan Tomayko</name>
    <email>rtomayko@gmail.com</email>
  </author>
  <url>http://github.com/rtomayko/rdiscount/commit/8ad8780b22561aae67634a74a04d15afb9144eae</url>
  <id>8ad8780b22561aae67634a74a04d15afb9144eae</id>
  <committed-date>2009-01-31T13:41:21-08:00</committed-date>
  <authored-date>2009-01-31T13:41:21-08:00</authored-date>
  <message>Merge js/master: TOC and Ruby 1.9 support</message>
  <tree>08a2676b58cfdf6121ced44b2958b86e0697a017</tree>
  <committer>
    <name>Ryan Tomayko</name>
    <email>rtomayko@gmail.com</email>
  </committer>
</commit>
