*** simple-toc.php	2012-01-15 18:55:25.000000000 +0900
--- simple-toc.php.new	2012-05-21 00:14:49.654932970 +0900
***************
*** 165,174 ****
  				$toc = '<div class="toc"><div class="toc-head">'.apply_filters('Simple_TOC_Heading',$he,$post).'</div><div class="toc_list">';
  				$content = str_replace($h,'',$content);
  			}else{
! 				$toc = '<div class="toc"><div class="toc_list">';
  			}
  			$toc .= apply_filters('Simple_TOC_before_items','',$post).'<ul>';
  			$toc .= apply_filters('Simple_TOC_before_first_item','',$post);
  			//tag is?
  			$start = $end = '';
  			$start = strpos($content, "[---TAG:");
--- 165,175 ----
  				$toc = '<div class="toc"><div class="toc-head">'.apply_filters('Simple_TOC_Heading',$he,$post).'</div><div class="toc_list">';
  				$content = str_replace($h,'',$content);
  			}else{
! 				$toc = '<div class="toc"><span>目次</span><div class="toc_list">';
  			}
  			$toc .= apply_filters('Simple_TOC_before_items','',$post).'<ul>';
  			$toc .= apply_filters('Simple_TOC_before_first_item','',$post);
+ 
  			//tag is?
  			$start = $end = '';
  			$start = strpos($content, "[---TAG:");
***************
*** 177,201 ****
  			$tag = str_replace('[---TAG:','',$tag_c);
  			$tag = str_replace('---]','',$tag);
  			$content = str_replace($tag_c,'',$content);
  			
! 			
! 			
  			$doc = new DOMDocument();
  			@$doc->loadHTML('<?xml encoding="'.get_bloginfo('charset').'">'.$content);
! 			$headings = $doc->getElementsByTagName($tag);
! 			foreach ($headings as $heading){
! 				$h = $heading_slug = $anchor = '';
! 				$h = $heading->nodeValue;
! 				$heading_slug = str_replace (" ",'-',$h);
! 				$heading_slug = apply_filters('Simple_TOC_Heading_Slugs',sanitize_title($heading_slug),$post);
! 				$anchor = '<a name="toc-'.$heading_slug.'" style="text-decoration: none;">&nbsp;&nbsp;</a>'; 
! 				$toc .= '<li class="toc_item"><a href="#toc-'.$heading_slug.'">'.$h.'</a></li>';
! 				$content = str_replace($h, $h.$anchor ,$content);
  			}
  			$toc .= apply_filters('Simple_TOC_After_Last_item','',$post);
  			$toc .= '</ul></div></div>';
  			
  			$content = str_replace("[---ATOC---]", $toc,$content);
  			return $content;
  		}
  		
--- 178,286 ----
  			$tag = str_replace('[---TAG:','',$tag_c);
  			$tag = str_replace('---]','',$tag);
  			$content = str_replace($tag_c,'',$content);
+ 			$tags = preg_split('/,/', $tag);
+ 
+                         $heading_numbering = false;
+                         if ( strpos($content, '[---AUTO_SECTION_NUMBER---]') != false )
+                         {
+                           $heading_numbering = true;
+                           $content = str_replace('[---AUTO_SECTION_NUMBER---]', '', $content);
+                         }//if
+ 
  			
! 			$heading_tags = array('h1' => 0, 'h2' => 1, 'h3' => 2, 'h4' => 3, 'h5' => 4, 'h6' => 5);
! 
  			$doc = new DOMDocument();
  			@$doc->loadHTML('<?xml encoding="'.get_bloginfo('charset').'">'.$content);
! 
!                         $hole_headings = array();
!                         $excludes = array();
!                         $min_level = 99999;
! 			foreach($tags as $hoge)
!                         {
! 			  $headings = $doc->getElementsByTagName($hoge);
!                           foreach($headings as $heading)
!                           {
!                             $hole_headings[$heading->getLineNo()] = $heading;
!                             $min_level = min($heading_tags[$heading->nodeName], $min_level);
! 
!                             if ( preg_match("/\[---EXCLUDE---\]/u", $heading->nodeValue) )
!                             { 
!                               $excludes[spl_object_hash($heading)] = true;
!                             } //if
!                           }
!                         }//foreach
!                         
!                         ksort($hole_headings);
! 
!                         if ( $heading_numbering )
!                         {
!                         $indices = array('h1' => 0, 'h2' => 0, 'h3' => 0, 'h4' => 0, 'h5' => 0, 'h6' => 0);
!                         $heading_order = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
!                         $exclude_level = -1;
! 			foreach ($hole_headings as $heading) // numbering
!                         {
!                           if ( $exclude_level == -1 )
!                           {
!                             if ( array_key_exists(spl_object_hash($heading), $excludes) )
!                             {
!                               $exclude_level = $heading_tags[$heading->nodeName];
!                               continue;
!                             }
!                           }//if
!                           else
!                           {
!                             if ( $heading_tags[$heading->nodeName] > $exclude_level )
!                             {
!                               continue;
!                             }
!                             else
!                             {
!                               $exclude_level = -1;
!                             }
!                           }//else
! 
! 
!                           ++$indices[$heading->nodeName];          
!                           foreach($heading_order as $h)
!                           {
!                             if ( $heading_tags[$h] > $heading_tags[$heading->nodeName] )
!                               $indices[$h] = 0;
!                           }//foreach
! 
!                           $label = '';
!                           foreach($heading_order as $h)
!                           {
!                             if ( $indices[$h] != 0 )
!                               $label .= $indices[$h] . '.';
!                           }//foreach
!                           
!                           $content = str_replace('<'.$heading->nodeName.'>'.$heading->nodeValue, 
!                                                  '<'.$heading->nodeName.'>'.$label.' '.$heading->nodeValue,$content);   
!                           $heading->nodeValue = $label . ' ' . $heading->nodeValue;
!                         }//foreach
! 
!                         }//if ( $heading_numbering )
! 
! 		        foreach ($hole_headings as $heading){	
! 			  $additonal_style = '';
! 			  if ( array_key_exists($heading->nodeName, $heading_tags) )
! 				$additional_style = 'style="margin-left:'.(($heading_tags[$heading->nodeName]-$min_level)*1.5).'em"';
! 			
! 		  	  $h = $heading_slug = $anchor = '';
! 		  	  $h = $heading->nodeValue;
! 		  	  $heading_slug = str_replace (" ",'-',$h);
! 		  	  $heading_slug = apply_filters('Simple_TOC_Heading_Slugs',sanitize_title($heading_slug),$post);
! 		  	  $anchor = '<a name="toc-'.$heading_slug.'" style="text-decoration: none;">&nbsp;&nbsp;</a>'; 
! 		  	  $toc .= '<li class="toc_item" '.$additional_style.'><a href="#toc-'.$heading_slug.'">'.$h.'</a></li>';
! 		  	  $content = str_replace($h, $h.$anchor ,$content);
  			}
+ 
  			$toc .= apply_filters('Simple_TOC_After_Last_item','',$post);
  			$toc .= '</ul></div></div>';
  			
  			$content = str_replace("[---ATOC---]", $toc,$content);
+ 			$content = str_replace('[---EXCLUDE---]', '',$content);
  			return $content;
  		}
  		
