on master help sign in
Extend approach to scalars
da70eafgmorpheme
libyaml/src/Text/Libyaml.hs+41−14
@@ -33,6 +33,8 @@ module Text.Libyaml
33 , defaultFormatOptions
33 , defaultFormatOptions
34 , setWidth
34 , setWidth
35 , setCollectionTagRendering
35 , setCollectionTagRendering
36+ , setPlainTagRendering
37+ , setQuotedTagRendering
36 , renderNone
38 , renderNone
37 , renderAll
39 , renderAll
38 , renderUriTags
40 , renderUriTags
@@ -469,13 +471,14 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
469 len' = fromIntegral len :: CInt
471 len' = fromIntegral len :: CInt
470 let thetag' = tagToString thetag
472 let thetag' = tagToString thetag
471 withCString thetag' $ \tag' -> do
473 withCString thetag' $ \tag' -> do
474+ let pi0 = implicitPlain e
475+ qi = implicitQuoted e
472− let (pi, style) =
476+ (pi, style) =
473− case style0 of
477+ case style0 of
474− PlainNoTag -> (1, Plain)
478+ PlainNoTag -> (1, Plain)
475− x -> (0, x)
479+ x -> (pi0, x)
476 style' = toEnum $ fromEnum style
480 style' = toEnum $ fromEnum style
477 tagP = castPtr tag'
481 tagP = castPtr tag'
478− qi = if null thetag' then 1 else 0
479 case anchor of
482 case anchor of
480 Nothing ->
483 Nothing ->
481 c_yaml_scalar_event_initialize
484 c_yaml_scalar_event_initialize
@@ -506,7 +509,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
506 er
509 er
507 nullPtr
510 nullPtr
508 tagP
511 tagP
509− (implicit e)
512+ (implicitColl e)
510 (toEnum $ fromEnum style)
513 (toEnum $ fromEnum style)
511 EventSequenceStart tag style (Just anchor) ->
514 EventSequenceStart tag style (Just anchor) ->
512 withCString (tagToString tag) $ \tag' -> do
515 withCString (tagToString tag) $ \tag' -> do
@@ -517,7 +520,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
517 er
520 er
518 anchorP
521 anchorP
519 tagP
522 tagP
520− (implicit e)
523+ (implicitColl e)
521 (toEnum $ fromEnum style)
524 (toEnum $ fromEnum style)
522 EventSequenceEnd ->
525 EventSequenceEnd ->
523 c_yaml_sequence_end_event_initialize er
526 c_yaml_sequence_end_event_initialize er
@@ -528,7 +531,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
528 er
531 er
529 nullPtr
532 nullPtr
530 tagP
533 tagP
531− (implicit e)
534+ (implicitColl e)
532 (toEnum $ fromEnum style)
535 (toEnum $ fromEnum style)
533 EventMappingStart tag style (Just anchor) ->
536 EventMappingStart tag style (Just anchor) ->
534 withCString (tagToString tag) $ \tag' -> do
537 withCString (tagToString tag) $ \tag' -> do
@@ -539,7 +542,7 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
539 er
542 er
540 anchorP
543 anchorP
541 tagP
544 tagP
542− (implicit e)
545+ (implicitColl e)
543 (toEnum $ fromEnum style)
546 (toEnum $ fromEnum style)
544 EventMappingEnd ->
547 EventMappingEnd ->
545 c_yaml_mapping_end_event_initialize er
548 c_yaml_mapping_end_event_initialize er
@@ -552,9 +555,17 @@ toEventRaw opts e f = allocaBytes eventSize $ \er -> do
552 unless (ret == 1) $ throwIO $ ToEventRawException ret
555 unless (ret == 1) $ throwIO $ ToEventRawException ret
553 f er
556 f er
554 where
557 where
558+ implicitColl (EventMappingStart NoTag _ _) = 1
555− implicit (EventSequenceStart NoTag _ _) = 1
559+ implicitColl (EventSequenceStart NoTag _ _) = 1
556− implicit (EventMappingStart NoTag _ _) = 1
560+ implicitColl (EventMappingStart (UriTag "") _ _) = 1
561+ implicitColl (EventSequenceStart (UriTag "") _ _) = 1
557− implicit e = toEnum $ fromEnum $ formatOptionsRenderCollectionTags opts e
562+ implicitColl evt = toEnum $ fromEnum $ formatOptionsRenderCollectionTags opts evt
563+ implicitPlain (EventScalar _ NoTag _ _) = 1
564+ implicitPlain (EventScalar _ (UriTag "") _ _) = 1
565+ implicitPlain evt = toEnum $ fromEnum $ formatOptionsRenderPlainScalarTags opts evt
566+ implicitQuoted (EventScalar _ NoTag _ _) = 1
567+ implicitQuoted (EventScalar _ (UriTag "") _ _) = 1
568+ implicitQuoted evt = toEnum $ fromEnum $ formatOptionsRenderQuotedScalarTags opts evt
558
569
559newtype ToEventRawException = ToEventRawException CInt
570newtype ToEventRawException = ToEventRawException CInt
560 deriving (Show, Typeable)
571 deriving (Show, Typeable)
@@ -686,7 +697,7 @@ renderNone :: Event -> TagRender
686renderNone _ = Implicit
697renderNone _ = Implicit
687
698
688-- | A value for 'formatOptionsRenderCollectionTags' that renders all
699-- | A value for 'formatOptionsRenderCollectionTags' that renders all
689−-- tags.
700+-- tags (except 'NoTag' and 'PlainNoTag' for flow scalars).
690--
701--
691-- @since 0.11.1.0
702-- @since 0.11.1.0
692renderAll :: Event -> TagRender
703renderAll :: Event -> TagRender
@@ -708,6 +719,8 @@ renderUriTags _ = Implicit
708data FormatOptions = FormatOptions
719data FormatOptions = FormatOptions
709 { formatOptionsWidth :: Maybe Int
720 { formatOptionsWidth :: Maybe Int
710 , formatOptionsRenderCollectionTags :: Event -> TagRender
721 , formatOptionsRenderCollectionTags :: Event -> TagRender
722+ , formatOptionsRenderPlainScalarTags :: Event -> TagRender
723+ , formatOptionsRenderQuotedScalarTags :: Event -> TagRender
711 }
724 }
712
725
713-- |
726-- |
@@ -716,6 +729,8 @@ defaultFormatOptions :: FormatOptions
716defaultFormatOptions = FormatOptions
729defaultFormatOptions = FormatOptions
717 { 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
730 { 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
718 , formatOptionsRenderCollectionTags = renderNone
731 , formatOptionsRenderCollectionTags = renderNone
732+ , formatOptionsRenderPlainScalarTags = renderAll
733+ , formatOptionsRenderQuotedScalarTags = renderAll
719 }
734 }
720
735
721-- | Set the maximum number of columns in the YAML output, or 'Nothing' for infinite. By default, the limit is 80 characters.
736-- | Set the maximum number of columns in the YAML output, or 'Nothing' for infinite. By default, the limit is 80 characters.
@@ -724,12 +739,24 @@ defaultFormatOptions = FormatOptions
724setWidth :: Maybe Int -> FormatOptions -> FormatOptions
739setWidth :: Maybe Int -> FormatOptions -> FormatOptions
725setWidth w opts = opts { formatOptionsWidth = w }
740setWidth w opts = opts { formatOptionsWidth = w }
726
741
727−-- | Control when and whether collection tags are rendered to output.
742+-- | Control when and whether tags on collections are rendered to output.
728--
743--
729-- @since 0.11.1.0
744-- @since 0.11.1.0
730setCollectionTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
745setCollectionTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
731setCollectionTagRendering f opts = opts { formatOptionsRenderCollectionTags = f }
746setCollectionTagRendering f opts = opts { formatOptionsRenderCollectionTags = f }
732
747
748+-- | Control when and whether tags on plain scalars are rendered to output.
749+--
750+-- @since 0.11.1.0
751+setPlainTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
752+setPlainTagRendering f opts = opts { formatOptionsRenderPlainScalarTags = f }
753+
754+-- | Control when and whether tags on quoted scalars are rendered to output.
755+--
756+-- @since 0.11.1.0
757+setQuotedTagRendering :: (Event -> TagRender) -> FormatOptions -> FormatOptions
758+setQuotedTagRendering f opts = opts { formatOptionsRenderQuotedScalarTags = f }
759+
733encode :: MonadResource m => ConduitM Event o m ByteString
760encode :: MonadResource m => ConduitM Event o m ByteString
734encode = encodeWith defaultFormatOptions
761encode = encodeWith defaultFormatOptions
735
762