4141import  ruamel .yaml 
4242import  ruamel .yaml .parser 
4343import  ruamel .yaml .representer 
44+ import  ruamel .yaml .scalarstring 
4445import  ruamel .yaml .scanner 
4546import  umsgpack 
4647
@@ -109,6 +110,7 @@ class TOMLOptions(FormatOptions):
109110class  YAMLOptions (FormatOptions ):
110111    indent : int  =  Defaults .YAML_INDENT 
111112    style : YAMLStyle  =  Defaults .YAML_STYLE 
113+     style_newline : YAMLStyle  |  None  =  None 
112114    width : int  =  Defaults .WIDTH 
113115
114116
@@ -392,6 +394,13 @@ def output_width(value: str) -> int:
392394        help = "YAML formatting style" ,
393395    )
394396
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+ 
395404    parser .add_argument (
396405        "--yaml-width" ,
397406        dest = "width" ,
@@ -761,15 +770,12 @@ def multilinify(item: tomlkit.items.Item) -> None:
761770        raise  ValueError (msg )
762771
763772
764- def  _yaml_represent_none (self , data ):
765-     return  self .represent_scalar ("tag:yaml.org,2002:null" , "null" )
766- 
767- 
768773def  _encode_yaml (
769774    data : Document ,
770775    * ,
771776    indent : int  |  None ,
772777    style : YAMLStyle ,
778+     style_newline : YAMLStyle  |  None ,
773779    width : int ,
774780) ->  str :
775781    yaml  =  ruamel .yaml .YAML (pure = True )
@@ -779,7 +785,15 @@ def _encode_yaml(
779785    yaml .indent  =  indent 
780786    yaml .width  =  width 
781787
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 )
783797
784798    try :
785799        out  =  StringIO ()
@@ -804,6 +818,7 @@ def format_options(
804818    stringify : bool  =  False ,
805819    width : int  =  Defaults .WIDTH ,
806820    yaml_style : YAMLStyle  =  Defaults .YAML_STYLE ,
821+     yaml_style_newline : YAMLStyle  |  None  =  None ,
807822) ->  FormatOptions :
808823    match  output_format :
809824        case  "cbor" :
@@ -837,6 +852,7 @@ def format_options(
837852            return  YAMLOptions (
838853                indent = Defaults .YAML_INDENT  if  indent  is  None  else  indent ,
839854                style = yaml_style ,
855+                 style_newline = yaml_style_newline ,
840856                width = width ,
841857            )
842858
@@ -918,6 +934,7 @@ def encode(
918934                data ,
919935                indent = options .indent ,
920936                style = options .style ,
937+                 style_newline = options .style_newline ,
921938                width = options .width ,
922939            ).encode (UTF_8 )
923940
@@ -1004,6 +1021,7 @@ def main() -> None:
10041021            stringify = args .stringify ,
10051022            width = args .width ,
10061023            yaml_style = args .yaml_style ,
1024+             yaml_style_newline = args .yaml_style_newline ,
10071025        )
10081026
10091027        remarshal (
0 commit comments