<?php if (!defined('PmWiki')) exit();

$RecipeInfo['SectionEdit']['Version'] = '2007-05-02';

## DESCRIPTION:  This recipe allows you to merge a datapage and a template to produce a nice output. For more info, visit the SectionEdit cookbook page at PmWiki.  Author: Dan Vis aka Caveman <editor àt fast döt st>, Copyright 2007.  

## LICENSE:  You can redistribute this software and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.


Markup('zapsections', '<{$var}', '/\(:sectionlist (.*?):\)/e', 'SectionList("$1")');
function SectionList($x) {
	$args = ParseArgs($x);
	$data = substr(stripslashes($args['data']), 1, -1);
	$template = substr(stripslashes($args['fmt']), 1, -1);
	if ((! CondAuth($data, 'read')) || (! CondAuth($template, 'read'))) return 'oops';
	$template = ReadPage($template);
	$template = $template['text'];
	$data = ReadPage($data);
	$sections = explode("[[#ID", $data['text']);
	unset($sections[0]);
	foreach ($sections as $i => $section) {
		$sectionarray = '';
		$lines = explode("\n", $section);
		$id = substr($line[0], 0, strpos($line[0], "]]"));
		$sectionarray['id'] = $id;
		foreach ($lines as $line) {
			if (strpos($line, ": ") !== false) {
				$f = substr($line, 0, strpos($line, ": "));
				$v = substr($line, strpos($line, ": ") + 2);
				$sectionarray[$f] = $v;
				}
			}
		$out[$i] = preg_replace('/\\{\\$\\$(\\w+)\\}/e', 'ZAPfieldreplace("$1", $sectionarray)', $template);
return $out[$i];
		}
	if (!is_array($out)) return '';
	return implode('', $out);
	return '';
	}