From 0cb0a5e239d3ea65b579d68d17f1de9f624c7ab6 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 3 Feb 2023 09:39:44 -0800 Subject: [PATCH 1/2] Commonmark.Html: export transform. This is a function that allows you to walk the tree of HTML nodes, applying a transformation. --- commonmark/src/Commonmark/Html.hs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/commonmark/src/Commonmark/Html.hs b/commonmark/src/Commonmark/Html.hs index 6226eb2..7c82ac5 100644 --- a/commonmark/src/Commonmark/Html.hs +++ b/commonmark/src/Commonmark/Html.hs @@ -13,6 +13,7 @@ module Commonmark.Html , renderHtml , escapeURI , escapeHtml + , transform ) where @@ -463,3 +464,8 @@ escapeURIChar c '#','!','$','\'','(',')','*','+',',', ';','='] +-- | Walk HTML nodes bottom-up, applying a function. +transform :: (Html a -> Html a) -> Html a -> Html a +transform f (HtmlElement et name attr (Just elt)) = + f $ HtmlElement et name attr (Just (transform f elt)) +transform f elt = f elt From ef35cd4b905149bd21f7f54d7d6d9c03e4553971 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 4 Feb 2023 10:32:08 -0800 Subject: [PATCH 2/2] Commonmark.Html: export Html constructors and transformM. API change: - Export constructors for Html. - Export ElementType - Re-export Attribute - Export transformM, a monadic version of transform. --- commonmark/src/Commonmark/Html.hs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/commonmark/src/Commonmark/Html.hs b/commonmark/src/Commonmark/Html.hs index 7c82ac5..dee7e1a 100644 --- a/commonmark/src/Commonmark/Html.hs +++ b/commonmark/src/Commonmark/Html.hs @@ -4,7 +4,9 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE UndecidableInstances #-} module Commonmark.Html - ( Html + ( Html(..) + , ElementType(..) + , Attribute , htmlInline , htmlBlock , htmlText @@ -14,6 +16,7 @@ module Commonmark.Html , escapeURI , escapeHtml , transform + , transformM ) where @@ -469,3 +472,10 @@ transform :: (Html a -> Html a) -> Html a -> Html a transform f (HtmlElement et name attr (Just elt)) = f $ HtmlElement et name attr (Just (transform f elt)) transform f elt = f elt + +-- | Walk HTML nodes bottom-up, applying a function, in a monadic context. +transformM :: Monad m => (Html a -> m (Html a)) -> Html a -> m (Html a) +transformM f (HtmlElement et name attr (Just elt)) = do + elt' <- transformM f elt + f $ HtmlElement et name attr (Just elt') +transformM f elt = f elt