Shared: export extractIds.

This will be used to help with #1745.
This commit is contained in:
John MacFarlane 2017-03-12 12:42:03 +01:00
parent cf0a1b2034
commit 0ef1e51211

View file

@ -64,6 +64,7 @@ module Text.Pandoc.Shared (
Element (..), Element (..),
hierarchicalize, hierarchicalize,
uniqueIdent, uniqueIdent,
extractIds,
inlineListToIdentifier, inlineListToIdentifier,
isHeaderBlock, isHeaderBlock,
headerShift, headerShift,
@ -529,6 +530,25 @@ headerLtEq level (Header l _ _) = l <= level
headerLtEq level (Div ("",["references"],[]) (Header l _ _ : _)) = l <= level headerLtEq level (Div ("",["references"],[]) (Header l _ _ : _)) = l <= level
headerLtEq _ _ = False headerLtEq _ _ = False
-- | Extract the identifiers from a block element.
extractBlockIds :: Block -> Set.Set String
extractBlockIds (Header _ (ident,_,_) _) | not (null ident) =
Set.singleton ident
extractBlockIds (Div (ident,_,_) _) | not (null ident) =
Set.singleton ident
extractBlockIds _ = Set.empty
-- | Extract the identifiers from an inline element.
extractInlineIds :: Inline -> Set.Set String
extractInlineIds (Span (ident,_,_) _) | not (null ident) =
Set.singleton ident
extractInlineIds _ = Set.empty
-- | Extract the identifiers from a pandoc document.
extractIds :: Pandoc -> Set.Set String
extractIds doc =
query extractBlockIds doc `Set.union` query extractInlineIds doc
-- | Generate a unique identifier from a list of inlines. -- | Generate a unique identifier from a list of inlines.
-- Second argument is a list of already used identifiers. -- Second argument is a list of already used identifiers.
uniqueIdent :: [Inline] -> Set.Set String -> String uniqueIdent :: [Inline] -> Set.Set String -> String