--- /home/alexis/tools/drupal/tableofcontents/tableofcontents.module	2008-12-13 19:08:02.000000000 -0800
+++ tableofcontents/tableofcontents.module	2008-12-24 23:51:14.000000000 -0800
@@ -30,8 +30,6 @@
  */
 
 function tableofcontents_init() {
-  drupal_add_js(drupal_get_path('module', 'tableofcontents') . '/tableofcontents.js');
-  drupal_add_css(drupal_get_path('module', 'tableofcontents') . '/tableofcontents.css');
 }
 
 /**
@@ -92,7 +90,13 @@
     case 'prepare':
 
       // get all toc markers and options
-      preg_match_all('!<\!-- ?tableofcontents(.*)-->!', $text, $options_str, PREG_PATTERN_ORDER);
+      $tag = '!<\!-- ?tableofcontents([^>]*)-->!';
+      preg_match_all($tag, $text, $options_str, PREG_PATTERN_ORDER);
+
+      if (empty($options_str[0])) {
+        $tag = '!\[tableofcontents([^]]*)\]!';
+        preg_match_all($tag, $text, $options_str, PREG_PATTERN_ORDER);
+      }
 
       // to optimize performance enclose preparation in conditional that tests for presence of ToC marker
       if (!empty($options_str[0]) && $options_str[0][0] != "") {
@@ -111,7 +115,11 @@
         if (variable_get('tableofcontents_allow_override_' . $format, TRUE) && count($options_str,1) > 2) {
 
           // parse separate options
-          preg_match_all('/([A-z]+): ?([A-z0-9 ]+);/', $options_str[1][0], $options, PREG_PATTERN_ORDER);
+          $optstr = trim($options_str[1][0]);
+          if ($optstr[strlen($optstr) - 1] != ';') {
+            $optstr .= ';';
+          }
+          preg_match_all('/([A-z]+): *([A-z0-9 ]+);/', $optstr, $options, PREG_PATTERN_ORDER);
 
           // make sure arguments contain valid option identifiers
           $allowed_options = array_keys($toc_options);
@@ -131,14 +139,15 @@
             $toc_options["list"]="ol";
           }
           if (!($toc_options["minlevel"] >= 1 && $toc_options["minlevel"] < 6)) {
-            $error_message[] = $toc_options["minlevel"]." is an invalid minimum level option. You must use a number between 1 and 4. Using default value of 1.\n";
+            $error_message[] = $toc_options["minlevel"]." is an invalid minimum level option. You must use a number between 1 and 5. Using default value of 1.\n";
             $error_status = true;
             $toc_options["minlevel"] = 1;
           }
           if (!($toc_options["maxlevel"] >= $toc_options["minlevel"] && $toc_options["maxlevel"] <= 6)) {
-            $error_message[] = $toc_options["maxlevel"]." is an invalid maximum depth option. You must use a number between ".$toc_options["minlevel"]." and 5. Using default value of 3.";
+            $newmax = $toc_options["minlevel"] > 3 ? $toc_options["minlevel"] : 3;
+            $error_message[] = $toc_options["maxlevel"]." is an invalid maximum depth option. You must use a number between ".$toc_options["minlevel"]." and 6. Using ".$newmax.".";
             $error_status = true;
-            $toc_options["maxlevel"] = 3;
+            $toc_options["maxlevel"] = $newmax;
           }
           if (!($toc_options["attachments"] == 1 || $toc_options["attachments"] == 0)) {
             $error_message[] = t("!attachments is an invalid attachments option. Attachments option must be set to '1' to display or '0' to hide.", array('!attachments' => $toc_options["attachments"]));
@@ -156,6 +165,9 @@
         
         // Translate title.
         $toc_options["title"] = t($toc_options["title"]);
+
+        // save the tag, but do it now so users cannot overwrite it
+        $toc_options["tag"] = $tag;
       }
 
       return $text;
@@ -163,7 +175,8 @@
     case 'process':
       // to optimize performance enclose processing in conditional that tests for the presence of the toc marker options
       if (isset($toc_options)) {
-
+  drupal_add_js(drupal_get_path('module', 'tableofcontents') . '/tableofcontents.js');
+  drupal_add_css(drupal_get_path('module', 'tableofcontents') . '/tableofcontents.css');
         //build the toc
         // toc(array('level' => 1, 'heading' => $text))
         $toc = array();
@@ -185,7 +198,7 @@
         $toc_options["minlevel"] . '-' .
         $toc_options["maxlevel"] . '])>/is';
         
-        preg_match('!<\!-- ?tableofcontents(.*)-->!', $text, $matches, PREG_OFFSET_CAPTURE);
+        preg_match($toc_options["tag"], $text, $matches, PREG_OFFSET_CAPTURE);
         $heading_search_text = substr($text, $matches[0][1]);
         
         $matches = array();
@@ -221,7 +234,7 @@
         $toc_html = theme('tableofcontents_toc', $toc, $toc_options, $files);
 
         // replace all tableofcontents markers with generated ToC html
-        return preg_replace('!<\!-- ?tableofcontents(.*)-->!', $toc_html, $text);
+        return preg_replace($toc_options["tag"], $toc_html, $text);
       }
       else {
         return $text;
@@ -335,23 +348,29 @@
       if (variable_get("tableofcontents_remove_teaser_$node->format", TRUE)) {
         $filters = filter_list_format($node->format);
         if (isset($filters['tableofcontents/0'])) {
-          if ($node->teaser && ($node->teaser != $new_teaser = preg_replace('!<\!-- ?tableofcontents(.*)-->!', '', $node->teaser))) {
-            if (strpos($node->body, '<!--break-->') > 0) {
-              // We've specfied the split, but the summary is shown in the full
-              // view. So, we now have to convert it so that the summary is
-              // hidden.
-              $node->body = preg_replace('/(.*)<!--break-->/s', '<!--break-->$1', $node->body);
-              drupal_set_message(t("Your summary was split from the body as site settings don't allow Table of Contents in summaries."));
-            }
-            else {
-              // This is the case where no specific break or summary was
-              // specified, so now it becomes a split teaser.
-              $node->body = '<!--break-->' . $node->body;
-              drupal_set_message(t("A split summary was automatically created as site settings don't allow Table of Contents in summaries."));
+          if ($node->teaser) {
+	    $new_teaser = preg_replace('!<\!-- ?tableofcontents([^>]*)-->!', '', $node->teaser);
+            if ($node->teaser == $new_teaser) {
+	      $new_teaser = preg_replace('!\[tableofcontents([^]]*)]!', '', $node->teaser);
+	    }
+            if ($node->teaser != $new_teaser) {
+              if (strpos($node->body, '<!--break-->') > 0) {
+                // The user specified the split, but the summary is shown in the full
+                // view. So, we now have to convert it so that the summary is
+                // hidden.
+                $node->body = preg_replace('/(.*)<!--break-->/s', '<!--break-->$1', $node->body);
+                drupal_set_message(t("Your summary was split from the body as site settings don't allow Table of Contents in summaries."));
+              }
+              else {
+                // This is the case where no specific break or summary was
+                // specified, so now it becomes a split teaser.
+                $node->body = '<!--break-->' . $node->body;
+                drupal_set_message(t("A split summary was automatically created as site settings don't allow Table of Contents in summaries."));
+              }
+              
+              // Remove toc from teasers.
+              $node->teaser = $new_teaser;
             }
-            
-            // Remove toc from teasers.
-            $node->teaser = $new_teaser;
           }
         }
       }
