IncludeSection
Description
Include a section from the first available among a list of pages.
The PmWiki directive (:include Page1 Page2 Page3 #from#to:)
does not work as advertized. If Page1 exists, but does not contain a section #from#to, it returns nothing without checking the other pages.
I have a list of pages that "may" contain a specific section. I need to include this section if it exists in the first page, if not: from the second page and so on. This recipe adds a new markup that does exactly this:
(:includesection #from#to Page1 Page2 Page3:) (:includesection #section Page1 Page2 Page3:) |
Note that the section anchors are the first parameter, unlike with the (:include:) markup.
Installation
Place the following code in local/config.php :
Markup('includesection', '>if', '/\\(:includesection\\s+(\\S.*?):\\)/i', "IncludeSection"); $SaveAttrPatterns['/\\(:includesection\\s.*?:\\)/i'] = ' '; function IncludeSection($m) { extract($GLOBALS["MarkupToHTML"]); $args = ParseArgs($m[1]); $anc = array_shift($args['']); if($anc>'' && $anc[0]!="#") return ''; foreach($args[''] as $v) { $x = IncludeText($pagename, "$v$anc"); if($x>'') return PRR($x); } }
To set additional parameters such as lines=
or basepage=
, use such a markup (note the quotes):
(:includesection "#from#to lines=3 self=1" Page1 Page2 Page3:) |
To specify different section names with each page, use this (note the quotes) :
(:includesection "" Page1#sec1 Page2#sec2 Page3#sec3:) |
Notes
Release Notes
- 20220516 : update for PHP 7.4-8.0
- 20170618 : update for PHP 7.2
- 20160506 : update for PHP 5.5
- 20090228 : added $SaveAttrPatterns skip definition (suggested by Tontyna)
- 20090112a : added way to specify different section names with each page
- 20090112 : first release
See Also
Contributors
Comments
See discussion at IncludeSection-Talk
User notes +1: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.