41
41
import ruamel .yaml
42
42
import ruamel .yaml .parser
43
43
import ruamel .yaml .representer
44
+ import ruamel .yaml .scalarstring
44
45
import ruamel .yaml .scanner
45
46
import umsgpack
46
47
@@ -109,6 +110,7 @@ class TOMLOptions(FormatOptions):
109
110
class YAMLOptions (FormatOptions ):
110
111
indent : int = Defaults .YAML_INDENT
111
112
style : YAMLStyle = Defaults .YAML_STYLE
113
+ style_newline : YAMLStyle | None = None
112
114
width : int = Defaults .WIDTH
113
115
114
116
@@ -392,6 +394,13 @@ def output_width(value: str) -> int:
392
394
help = "YAML formatting style" ,
393
395
)
394
396
397
+ parser .add_argument (
398
+ "--yaml-style-newline" ,
399
+ choices = ["" , "'" , '"' , "|" , ">" ],
400
+ default = YAMLOptions ().style_newline ,
401
+ help = "YAML formatting style override for strings that contain a newline" ,
402
+ )
403
+
395
404
parser .add_argument (
396
405
"--yaml-width" ,
397
406
dest = "width" ,
@@ -761,15 +770,12 @@ def multilinify(item: tomlkit.items.Item) -> None:
761
770
raise ValueError (msg )
762
771
763
772
764
- def _yaml_represent_none (self , data ):
765
- return self .represent_scalar ("tag:yaml.org,2002:null" , "null" )
766
-
767
-
768
773
def _encode_yaml (
769
774
data : Document ,
770
775
* ,
771
776
indent : int | None ,
772
777
style : YAMLStyle ,
778
+ style_newline : YAMLStyle | None ,
773
779
width : int ,
774
780
) -> str :
775
781
yaml = ruamel .yaml .YAML (pure = True )
@@ -779,7 +785,15 @@ def _encode_yaml(
779
785
yaml .indent = indent
780
786
yaml .width = width
781
787
782
- yaml .representer .add_representer (type (None ), _yaml_represent_none )
788
+ def represent_none (self , data ):
789
+ return self .represent_scalar ("tag:yaml.org,2002:null" , "null" )
790
+
791
+ def represent_str (self , data ):
792
+ str_style = style_newline if "\n " in data else style
793
+ return self .represent_scalar ("tag:yaml.org,2002:str" , data , style = str_style )
794
+
795
+ yaml .representer .add_representer (type (None ), represent_none )
796
+ yaml .representer .add_representer (str , represent_str )
783
797
784
798
try :
785
799
out = StringIO ()
@@ -804,6 +818,7 @@ def format_options(
804
818
stringify : bool = False ,
805
819
width : int = Defaults .WIDTH ,
806
820
yaml_style : YAMLStyle = Defaults .YAML_STYLE ,
821
+ yaml_style_newline : YAMLStyle | None = None ,
807
822
) -> FormatOptions :
808
823
match output_format :
809
824
case "cbor" :
@@ -837,6 +852,7 @@ def format_options(
837
852
return YAMLOptions (
838
853
indent = Defaults .YAML_INDENT if indent is None else indent ,
839
854
style = yaml_style ,
855
+ style_newline = yaml_style_newline ,
840
856
width = width ,
841
857
)
842
858
@@ -918,6 +934,7 @@ def encode(
918
934
data ,
919
935
indent = options .indent ,
920
936
style = options .style ,
937
+ style_newline = options .style_newline ,
921
938
width = options .width ,
922
939
).encode (UTF_8 )
923
940
@@ -1004,6 +1021,7 @@ def main() -> None:
1004
1021
stringify = args .stringify ,
1005
1022
width = args .width ,
1006
1023
yaml_style = args .yaml_style ,
1024
+ yaml_style_newline = args .yaml_style_newline ,
1007
1025
)
1008
1026
1009
1027
remarshal (
0 commit comments