@@ -32,11 +32,10 @@ module Text.Libyaml
3232 , FormatOptions
3333 , defaultFormatOptions
3434 , setWidth
35− , setCollectionTagRendering
36− , setPlainTagRendering
37− , setQuotedTagRendering
38− , renderNone
39− , renderAll
35+ , setTagRendering
36+ , renderScalarTags
37+ , renderAllTags
38+ , renderNoTags
4039 , renderUriTags
4140 -- * Error handling
4241 , YamlException (..)
@@ -127,6 +126,11 @@ data Tag = StrTag
127126 | NoTag
128127 deriving (Show, Eq, Read, Data, Typeable)
129128
129+tagSuppressed :: Tag -> Bool
130+tagSuppressed (NoTag) = True
131+tagSuppressed (UriTag "") = True
132+tagSuppressed _ = False
133+
130134type AnchorName = String
131135type Anchor = Maybe AnchorName
132136
@@ -471,8 +475,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
471475 len' = fromIntegral len :: CInt
472476 let thetag' = tagToString thetag
473477 withCString thetag' $ \tag' -> do
474− let pi0 = implicitPlain e
475− qi = implicitQuoted e
478+ let pi0 = tagsImplicit e
476479 (pi, style) =
477480 case style0 of
478481 PlainNoTag -> (1, Plain)
@@ -488,7 +491,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
488491 value' -- value
489492 len' -- length
490493 pi -- plain_implicit
491− qi -- quoted_implicit
494+ pi -- quoted_implicit
492495 style' -- style
493496 Just anchor' ->
494497 withCString anchor' $ \anchorP' -> do
@@ -500,7 +503,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
500503 value' -- value
501504 len' -- length
502505 0 -- plain_implicit
503− qi -- quoted_implicit
506+ pi -- quoted_implicit
504507 style' -- style
505508 EventSequenceStart tag style Nothing ->
506509 withCString (tagToString tag) $ \tag' -> do
@@ -509,7 +512,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
509512 er
510513 nullPtr
511514 tagP
512− (implicitColl e)
515+ (tagsImplicit e)
513516 (toEnum $ fromEnum style)
514517 EventSequenceStart tag style (Just anchor) ->
515518 withCString (tagToString tag) $ \tag' -> do
@@ -520,7 +523,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
520523 er
521524 anchorP
522525 tagP
523− (implicitColl e)
526+ (tagsImplicit e)
524527 (toEnum $ fromEnum style)
525528 EventSequenceEnd ->
526529 c_yaml_sequence_end_event_initialize er
@@ -531,7 +534,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
531534 er
532535 nullPtr
533536 tagP
534− (implicitColl e)
537+ (tagsImplicit e)
535538 (toEnum $ fromEnum style)
536539 EventMappingStart tag style (Just anchor) ->
537540 withCString (tagToString tag) $ \tag' -> do
@@ -542,7 +545,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
542545 er
543546 anchorP
544547 tagP
545− (implicitColl e)
548+ (tagsImplicit e)
546549 (toEnum $ fromEnum style)
547550 EventMappingEnd ->
548551 c_yaml_mapping_end_event_initialize er
@@ -555,17 +558,10 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
555558 unless (ret == 1) $ throwIO $ ToEventRawException ret
556559 f er
557560 where
558− implicitColl (EventMappingStart NoTag _ _) = 1
559− implicitColl (EventSequenceStart NoTag _ _) = 1
560− implicitColl (EventMappingStart (UriTag "") _ _) = 1
561− implicitColl (EventSequenceStart (UriTag "") _ _) = 1
562− implicitColl evt = toImplicitParam $ formatOptionsRenderCollectionTags opts evt
563− implicitPlain (EventScalar _ NoTag _ _) = 1
564− implicitPlain (EventScalar _ (UriTag "") _ _) = 1
565− implicitPlain evt = toImplicitParam $ formatOptionsRenderPlainScalarTags opts evt
566− implicitQuoted (EventScalar _ NoTag _ _) = 1
567− implicitQuoted (EventScalar _ (UriTag "") _ _) = 1
568− implicitQuoted evt = toImplicitParam $ formatOptionsRenderQuotedScalarTags opts evt
561+ tagsImplicit (EventScalar _ t _ _) | tagSuppressed t = 1
562+ tagsImplicit (EventMappingStart t _ _) | tagSuppressed t = 1
563+ tagsImplicit (EventSequenceStart t _ _) | tagSuppressed t = 1
564+ tagsImplicit evt = toImplicitParam $ formatOptionsRenderTags opts evt
569565
570566newtype ToEventRawException = ToEventRawException CInt
571567 deriving (Show, Typeable)
@@ -691,21 +687,32 @@ data TagRender = Explicit | Implicit
691687
692688toImplicitParam :: TagRender -> CInt
693689toImplicitParam Explicit = 0
694−toImplicitParam _ = 1
690+toImplicitParam Implicit = 1
695691
696−-- | A value for 'formatOptionsRenderCollectionTags' that renders no
697−-- tags.
692+-- | A value for 'formatOptionsRenderTags' that renders no
693+-- collection tags but all scalar tags (unless suppressed with styles
694+-- 'NoTag or 'PlainNoTag').
695+--
696+-- @since 0.11.1.0
697+renderScalarTags :: Event -> TagRender
698+renderScalarTags (EventScalar _ _ _ _) = Explicit
699+renderScalarTags (EventSequenceStart _ _ _) = Implicit
700+renderScalarTags (EventMappingStart _ _ _) = Implicit
701+renderScalarTags _ = Implicit
702+
703+-- | A value for 'formatOptionsRenderTags' that renders all
704+-- tags (except 'NoTag' tag and 'PlainNoTag' style).
698705--
699706-- @since 0.11.1.0
700−renderNone :: Event -> TagRender
701−renderNone _ = Implicit
707+renderAllTags :: Event -> TagRender
708+renderAllTags _ = Explicit
702709
703−-- | A value for 'formatOptionsRenderCollectionTags' that renders all
704−-- tags (except 'NoTag' and 'PlainNoTag' for flow scalars).
710+-- | A value for 'formatOptionsRenderTags' that renders no
711+-- tags.
705712--
706713-- @since 0.11.1.0
707−renderAll :: Event -> TagRender
708−renderAll _ = Explicit
714+renderNoTags :: Event -> TagRender
715+renderNoTags _ = Implicit
709716
710717-- | A value for 'formatOptionsRenderCollectionTags' that renders tags
711718-- which are instances of 'UriTag'
@@ -722,9 +729,7 @@ renderUriTags _ = Implicit
722729-- @since 0.10.2.0
723730data FormatOptions = FormatOptions
724731 { formatOptionsWidth :: Maybe Int
725− , formatOptionsRenderCollectionTags :: Event -> TagRender
726− , formatOptionsRenderPlainScalarTags :: Event -> TagRender
727− , formatOptionsRenderQuotedScalarTags :: Event -> TagRender
732+ , formatOptionsRenderTags :: Event -> TagRender
728733 }
729734
730735-- |
@@ -732,9 +737,7 @@ data FormatOptions = FormatOptions
732737defaultFormatOptions :: FormatOptions
733738defaultFormatOptions = FormatOptions
734739 { formatOptionsWidth = Just 80 -- by default the width is set to 0 in the C code, which gets turned into 80 in yaml_emitter_emit_stream_start
735− , formatOptionsRenderCollectionTags = renderNone
736− , formatOptionsRenderPlainScalarTags = renderAll
737− , formatOptionsRenderQuotedScalarTags = renderAll
740+ , formatOptionsRenderTags = renderScalarTags
738741 }
739742
740743-- | Set the maximum number of columns in the YAML output, or 'Nothing' for infinite. By default, the limit is 80 characters.
@@ -743,23 +746,11 @@ defaultFormatOptions = FormatOptions
743746setWidth :: Maybe Int -> FormatOptions -> FormatOptions
744747setWidth w opts = opts { formatOptionsWidth = w }
745748
746−-- | Control when and whether tags on collections are rendered to output.
747−--
748−-- @since 0.11.1.0
749−setCollectionTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
750−setCollectionTagRendering f opts = opts { formatOptionsRenderCollectionTags = f }
751−
752−-- | Control when and whether tags on plain scalars are rendered to output.
753−--
754−-- @since 0.11.1.0
755−setPlainTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
756−setPlainTagRendering f opts = opts { formatOptionsRenderPlainScalarTags = f }
757−
758−-- | Control when and whether tags on quoted scalars are rendered to output.
749+-- | Control when and whether tags are rendered to output.
759750--
760751-- @since 0.11.1.0
761−setQuotedTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
762−setQuotedTagRendering f opts = opts { formatOptionsRenderQuotedScalarTags = f }
752+setTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
753+setTagRendering f opts = opts { formatOptionsRenderTags = f }
763754
764755encode :: MonadResource m => ConduitM Event o m ByteString
765756encode = encodeWith defaultFormatOptions