minimal support for yaml files in document merge: multi-line replacements get prefixed with as many space as the placeholder

This commit is contained in:
Ralf Becker 2015-06-17 19:37:40 +00:00
parent aa651be625
commit 46c8c6e318

View File

@ -174,6 +174,8 @@ abstract class bo_merge
return true; // alias for text/xml, eg. ms office 2003 word format
case 'message/rfc822':
return true; // ToDo: check if you are theoretical able to send mail
case 'application/x-yaml':
return true; // yaml file, plain text with marginal syntax support for multiline replacements
default:
if (substr($mimetype,0,5) == 'text/')
{
@ -687,7 +689,7 @@ abstract class bo_merge
{
$content = preg_replace('/\\\{\\\{([^\\}]+)\\\}\\\}/i','$$\1$$',$content);
}
// make currently processed mimetype available to class methods;
$this->mimetype = $mimetype;
@ -1085,6 +1087,19 @@ abstract class bo_merge
// Check for encoded >< getting double-encoded
$replacements = str_replace(array('&',"\r","\n",'&amp;lt;','&amp;gt;'),array('&amp;','',$break,'&lt;','&gt;'),$replacements);
}
if ($mimetype == 'application/x-yaml')
{
$content = preg_replace_callback('/^( +)(\$\$[^$]+\$\$)/m', function($matches) use ($replacements)
{
$replacement = $replacements[$matches[2]];
// replacement with multiple lines --> add same number of space as before placeholder
if (isset($replacement) && strpos($replacement, "\n") !== false)
{
return $matches[1].implode("\n".$matches[1], preg_split("/\r?\n/", $replacement));
}
return $matches[0]; // regular replacement below
}, $content);
}
return str_replace(array_keys($replacements),array_values($replacements),$content);
}