Added an extension, called EditSectionHiliteLink, that highlights the appropriate...
[lhc/web/wiklou.git] / includes / parser / Parser.php
index 0f57d57..fa0b57d 100644 (file)
@@ -3371,7 +3371,7 @@ class Parser
         * @private
         */
        function formatHeadings( $text, $isMain=true ) {
-               global $wgMaxTocLevel, $wgContLang, $wgEnforceHtmlIds;
+               global $wgMaxTocLevel, $wgContLang, $wgEnforceHtmlIds, $wgSectionContainers;
 
                $doNumberHeadings = $this->mOptions->getNumberHeadings();
                $showEditLink = $this->mOptions->getEditSection();
@@ -3658,6 +3658,10 @@ class Parser
                $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
                $i = 0;
 
+               if ( $wgSectionContainers ) {
+                       $openDivs = array();
+               }
+               
                foreach( $blocks as $block ) {
                        if( $showEditLink && $headlineCount > 0 && $i == 0 && $block !== "\n" ) {
                                # This is the [edit] link that appears for the top block of text when
@@ -3672,6 +3676,34 @@ class Parser
                                # Top anchor now in skin
                                $full = $full.$toc;
                        }
+                       
+                       # wrap each section in a div if $wgSectionContainers is set to true
+                       if ( $wgSectionContainers ) {
+                       if( !empty( $head[$i] ) ) { # if there's no next header, then don't try to close out any existing sections here 
+                               # get the level of the next header section
+                                       preg_match('/<H([0-6])/i', $head[$i], $hLevelMatches);
+                               
+                                       if ( count($hLevelMatches) > 0 ) {
+                                               $hLevel = $hLevelMatches[1];
+                                               if ( $i != 0 ) { # we don't have an open div for section 0, so don't try to close it
+                                                       # close any open divs for sections with headers that are <= to the next header level
+                                                       $this->closeSectionContainers( $hLevel, &$currentHLevel, &$full, &$openDivs);
+                                               }
+                                               $currentHLevel = $hLevel;
+                                       }
+                               }
+
+                               # open the div for the next header, if there is one
+                               if ( isset($currentHLevel) && !empty( $head[$i] ) ) {
+                                       $full .= '<div id="section_' . $i . '_container">';
+                                        array_push($openDivs, array($currentHLevel, $i));
+                               }
+
+                               # if we've outputed the last section of the article, close any open divs that are remaining
+                               if ( $i == ( count($blocks) - 1)  && isset($currentHLevel) ) {
+                                       $this->closeSectionContainers( $hLevel, &$currentHLevel, &$full, &$openDivs);
+                               }
+                       }
 
                        if( !empty( $head[$i] ) ) {
                                $full .= $head[$i];
@@ -3685,6 +3717,29 @@ class Parser
                }
        }
 
+       /**
+        * Analyze the header level of the current and next section being parsed to 
+        * determine if any already parsed sections need to be closed
+        *
+        * @param string $hLevel the level of the next header to be parsed
+        * @param string $currentHLevel the level of the last parsed header
+        * @param string $full a reference to the string that stores the output of the parser
+        * @param array $openDivs a reference to the array that stores a list of open section containers
+        * @return true
+        */
+       function closeSectionContainers( $hLevel, &$currentHLevel, &$full, &$openDivs) {
+               while ( $hLevel <= $currentHLevel ) {
+                       $full .= '</div>';
+                       $popped = array_pop($openDivs);
+                       if ( count($openDivs) ) {
+                               $currentHLevel = $openDivs[count($openDivs) - 1][0];
+                       } else {
+                               break;
+                       }
+               }                       
+               return true;
+       }
+       
        /**
         * Transform wiki markup when saving a page by doing \r\n -> \n
         * conversion, substitting signatures, {{subst:}} templates, etc.