[help]如何令长字符串的yaml输出可读性更好
发布于 11 年前 作者 shiedman 11621 次浏览 最后一次编辑是 8 年前
var book={
    'title':'THE PROBLEMS OF PHILOSOPHY',
    'author':'Bertrand Russell',
    'preface':'In the following pages I have confined myself in the main to those problems of philosophy in regard to which I thought it possible to say something positive and constructive, since merely negative criticism seemed out of place...'
}

一般的yaml库格式化输出的结果大致都类似于

title: THE PROBLEMS OF PHILOSOPHY
author: Bertrand Russell
preface: "In the following pages I have confined myself in the main to those problems of philosophy in regard to which I thought it possible to say something positive and constructive, since merely negative criticism seemed out of place..."

这种双引号前后一闭合,管你多长的字符通通放一行,中间还可能插入各种恶心转义字符例如\"\n,可读性真的很差。python/node.js/ruby/java的yaml库都翻了遍,python比较接近*1。各种库对输入的parse/load都很强大,但是反过来输出yaml时,就开始变成各种反人类亲机器的文件格式,完全抛弃yaml的可读性。

求推荐yaml库,可通过简单的设置或者扩展,自定义长字符串以区块的形式的输出,多于80列则自动换行,如下

title: THE PROBLEMS OF PHILOSOPHY
author: Bertrand Russell
preface: >
    In the following pages I have confined myself in the main to those problems of 
    philosophy in regard to which I thought it possible to say something positive 
    and constructive, since merely negative criticism seemed out of place...

或者是扩展性比较高的格式化输出工具。

回到顶部