class YamlFormTidy in YAML Form 8
Provides YAML tidy function.
Hierarchy
- class \Drupal\yamlform\Utility\YamlFormTidy
Expanded class hierarchy of YamlFormTidy
11 files declare their use of YamlFormTidy
- DebugYamlFormHandler.php in tests/
modules/ yamlform_test/ src/ Plugin/ YamlFormHandler/ DebugYamlFormHandler.php - yamlform.drush.inc in drush/
yamlform.drush.inc - YAML Form module drush commands.
- yamlform.theme.inc in includes/
yamlform.theme.inc - Preprocessors and helper functions to make theming easier.
- yamlform.translation.inc in includes/
yamlform.translation.inc - YAML Form module translation hooks.
- YamlFormAdminSettingsTest.php in src/
Tests/ YamlFormAdminSettingsTest.php
File
- src/
Utility/ YamlFormTidy.php, line 10
Namespace
Drupal\yamlform\UtilityView source
class YamlFormTidy {
/**
* Tidy export YAML includes tweaking array layout and multiline strings.
*
* @param string $yaml
* The output generated from \Drupal\Core\Serialization\Yaml::encode.
*
* @return string
* The encoded data.
*/
public static function tidy($yaml) {
static $unescaper;
if (!isset($unescaper)) {
$unescaper = new Unescaper();
}
// Remove return after array delimiter.
$yaml = preg_replace('#(\\n[ ]+-)\\n[ ]+#', '\\1 ', $yaml);
// Support YAML newlines preserved syntax via pipe (|).
$lines = explode("\n", $yaml);
foreach ($lines as $index => $line) {
if (empty($line) || strpos($line, '\\n') === FALSE) {
continue;
}
if (preg_match('/^([ ]*(?:- )?)([a-z_]+|\'[^\']+\'|"[^"]+"): (\'|")(.+)\\3$/', $line, $match)) {
$prefix = $match[1];
$indent = str_repeat(' ', strlen($prefix));
$name = $match[2];
$quote = $match[3];
$value = $match[4];
if ($quote == "'") {
$value = rtrim($unescaper
->unescapeSingleQuotedString($value));
}
else {
$value = rtrim($unescaper
->unescapeDoubleQuotedString($value));
}
if (strpos($value, '<') === FALSE) {
$lines[$index] = $prefix . $name . ": |\n{$prefix} " . str_replace("\n", "\n{$prefix} ", $value);
}
else {
$value = preg_replace('~\\R~u', "\n", $value);
$value = preg_replace('#\\s*</p>#', '</p>', $value);
$value = str_replace("\n", "\n{$indent} ", $value);
$lines[$index] = $prefix . $name . ": |\n{$indent} " . $value;
}
}
}
$yaml = implode("\n", $lines);
return trim($yaml);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
YamlFormTidy:: |
public static | function | Tidy export YAML includes tweaking array layout and multiline strings. |