You are here

public static function YamlFormTidy::tidy in YAML Form 8

Tidy export YAML includes tweaking array layout and multiline strings.

Parameters

string $yaml: The output generated from \Drupal\Core\Serialization\Yaml::encode.

Return value

string The encoded data.

11 calls to YamlFormTidy::tidy()
DebugYamlFormHandler::submitForm in tests/modules/yamlform_test/src/Plugin/YamlFormHandler/DebugYamlFormHandler.php
Submit form submission form.
drush_yamlform_tidy in drush/yamlform.drush.inc
Implements drush_hook_COMMAND().
template_preprocess_yamlform_submission_yaml in includes/yamlform.theme.inc
Prepares variables for form submission YAML template.
YamlFormAdminSettingsTest::testAdminSettings in src/Tests/YamlFormAdminSettingsTest.php
Tests form admin settings.
YamlFormCodeMirror::valueCallback in src/Element/YamlFormCodeMirror.php
Determines how user input is mapped to an element's #value property.

... See full list

File

src/Utility/YamlFormTidy.php, line 21

Class

YamlFormTidy
Provides YAML tidy function.

Namespace

Drupal\yamlform\Utility

Code

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);
}