-- | Convert block-level macros. Block-level macros are signalled by a first-level header containing a piece of
-- inline code starting with a client-specified magic string. For example, if the magic string is @??@, a macro
-- can be called by
--
-- > #`??MACRO_NAME MACRO_ARGS`
--
-- where @MACRO_NAME@ is the identifying name of the macro and @MACRO_ARGS@ is a space-separated list of arguments.
blockMacros
  :: Yesod master
  => String                                             -- ^ Magic string to introduce the macro
  -> Map String ([String] -> GHandler sub master Block) -- ^ Lookup table from macro names to macro functions
  -> Pandoc
  -> GHandler sub master Pandoc
blockMacros magic table p = processWithM blockMacros' p where
  blockMacros' (Header 1 [Code (splitAt (length magic) -> (magic',words -> ((flip Map.lookup table -> Just f):xs)))])
    | magic == magic' = f xs
  blockMacros' b = return b

-- | Convert block-level macros. Inline-level macros are signalled by a piece of inline code starting with a
-- client-specified magic string. For example, if the magic string is @??@, a macro can be called by
--
-- > `??MACRO_NAME MACRO_ARGS`
--
-- where @MACRO_NAME@ is the identifying name of the macro and @MACRO_ARGS@ is a space-separated list of arguments.
blockMacros
  :: Yesod master
  => String                                             -- ^ Magic string to introduce the macro
  -> Map String ([String] -> GHandler sub master Block) -- ^ Lookup table from macro names to macro functions
  -> Pandoc
  -> GHandler sub master Pandoc
blockMacros magic table p = processWithM blockMacros' p where
  blockMacros' (Header 1 [Code (splitAt (length magic) -> (magic',words -> ((flip Map.lookup table -> Just f):xs)))])
    | magic == magic' = f xs
  blockMacros' b = return b

-- | Convert block-level macros. Inline-level macros are signalled by a piece of inline code starting with a
-- client-specified magic string. For example, if the magic string is @??@, a macro can be called by
--
-- > `??MACRO_NAME MACRO_ARGS`
--
-- where @MACRO_NAME@ is the identifying name of the macro and @MACRO_ARGS@ is a space-separated list of arguments.
blockMacros magic table p = processWithM blockMacros' p where
  blockMacros' (Header 1 [Code (splitAt (length magic) -> (magic',words -> ((flip Map.lookup table -> Just f):xs)))])
    | magic == magic' = f xs
  blockMacros' b = return b

-- | Convert block-level macros. Inline-level macros are signalled by a piece of inline code starting with a
-- client-specified magic string. For example, if the magic string is @??@, a macro can be called by
--
-- > `??MACRO_NAME MACRO_ARGS`
--
-- where @MACRO_NAME@ is the identifying name of the macro and @MACRO_ARGS@ is a space-separated list of arguments.