dirtsimple/clean-yaml

输出友好的diff和可读的YAML

v0.1.3 2019-09-08 20:30 UTC

This package is auto-updated.

Last update: 2024-09-09 07:56:59 UTC


README

虽然Symfony的Yaml组件有一个出色的解析器和基本符合规范的输出器,但有时你真的需要你的YAML输出可以被人类阅读,并在更改时生成干净的diff。例如,PostmarkImposer 从任意WordPress数据生成YAML输出,它们的diff是修订控制和配置管理的重要部分。

虽然JSON(以及Symfony相当接近JSON的YAML输出)可以在一定程度上进行diff,但diff往往很“嘈杂”,充满了额外的标点符号更改和过长的行,尤其是当字符串包含多行文本或HTML内容时。

因此,这个库为Symfony的YAML输出器提供了一个包装器,使用不同的缩进算法,优先考虑diff性和可读性,同时仍然生成符合规范的输出,可以完全回绕。具体来说,它

  • 仅在当前行可以容纳在指定的行长度内时才内联数据结构
  • 不内联超过一个级别的嵌套数据结构,除非所有子结构都是空的(例如 []{}
  • 将包含换行符的字符串拆分为多行输出,并使用正确的chomping运算符(因为Symfony的 DUMP_MULTI_LINE_LITERAL_BLOCK 标志仅适用于以正好一个 \n 结尾的字符串 -- 没有更多,也没有更少。)

遵循这些规则产生的输出仍然是符合规范的,但避免了长行内联数据,除非长行本身是字符串的一部分。(权衡是,产生的输出在行数和总文件大小上始终比Symfony生成的输出要大,因为内联的东西更少,因此需要更多的换行符和缩进。)

要使用此库,请要求 dirtsimple/clean-yamluse dirtsimple\CleanYaml;,然后调用 CleanYaml::dump($data),可选地传递额外的参数,例如宽度(默认为120)和缩进大小(默认为2)。返回值是一个包含完整YAML文档的字符串,该文档始终包含一个尾随换行符,即使顶层值是一个标量。

没有标志可以控制输出:库的行为大致相当于使用Symfony的 DUMP_EXCEPTION_ON_INVALID_TYPE | DUMP_OBJECT_AS_MAP | DUMP_MULTI_LINE_LITERAL_BLOCK | DUMP_EMPTY_ARRAY_AS_SEQUENCE,除了修复了最后两个标志行为中的各种怪癖。(Symfony的输出对于字面块缺少适当的chomp设置,有时即使你要求它不要这样做,它仍然会将嵌套的空数组输出为对象;另一方面,CleanYaml将顶层空数组视为映射,并将所有其他空数组视为序列。)

dump()的第二个参数是你想要的输出宽度。如果数据结构可以在这个空间内容纳,包括当前的缩进和键(如果有),则会内联数据结构。对于太长或太宽而无法适应空间或缩进太深的字符串,行仍然会超过这个大小。第三个参数是每个嵌套级别将缩进的空格数。

以下是默认设置下的示例输出

-
  id: 5509c1f
  elType: widget
  settings:
    title: 'Free download: No credit card required'
    tags: [ these, are, random, tags, that, fit, 'on', this, line ]
    text: |-
      Here is some indented text that is on multiple lines.  It doesn't rewrap
      because the line feeds are as originally given, and clean-yaml doesn't
      do folding, because that would introduce additional noise into diffs that
      didn't directly come from changes to the underlying data.  The last line
      doesn't end with a `\n`, so the `|-` chomp operator is used.
    align: center
    typography_typography: custom
    typography_font_weight: bold
    background_color: '#23a455'
    border_radius: { unit: px, top: '18', right: '18', bottom: '18', left: '18', isLinked: true }
  elements: []
  timestamp: 2016-05-27T00:00:00+00:00
  not_a_timestamp: '2016-05-27T00:00:00+00:00'

现在,使用较窄的宽度(40)和较宽的缩进(4)显示相同的数据

-
    id: 5509c1f
    elType: widget
    settings:
        title: 'Free download: No credit card required'
        tags:
            - these
            - are
            - random
            - tags
            - that
            - fit
            - 'on'
            - this
            - line
        text: |-
            Here is some indented text that is on multiple lines.  It doesn't rewrap
            because the line feeds are as originally given, and clean-yaml doesn't
            do folding, because that would introduce additional noise into diffs that
            didn't directly come from changes to the underlying data.  The last line
            doesn't end with a `\n`, so the `|-` chomp operator is used.
        align: center
        typography_typography: custom
        typography_font_weight: bold
        background_color: '#23a455'
        border_radius:
            unit: px
            top: '18'
            right: '18'
            bottom: '18'
            left: '18'
            isLinked: true
    elements: []
    timestamp: 2016-05-27T00:00:00+00:00
    not_a_timestamp: '2016-05-27T00:00:00+00:00'