git.iko.soy/typeable/pandoc
on master help sign in
Shared: export extractIds.

This will be used to help with #1745.

commit0ef1e51211cd35b1ddb54a5fe8bee4247f1e1801 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 12 Mar 2017, 12:42 (10 years ago) parentcf0a1b2
1 file changed+20
src/Text/Pandoc/Shared.hs+20
src/Text/Pandoc/Shared.hs+20
@@ -64,6 +64,7 @@ module Text.Pandoc.Shared (
6464 Element (..),
6565 hierarchicalize,
6666 uniqueIdent,
67+ extractIds,
6768 inlineListToIdentifier,
6869 isHeaderBlock,
6970 headerShift,
@@ -529,6 +530,25 @@ headerLtEq level (Header l _ _) = l <= level
529530headerLtEq level (Div ("",["references"],[]) (Header l _ _ : _)) = l <= level
530531headerLtEq _ _ = False
531532
533+-- | Extract the identifiers from a block element.
534+extractBlockIds :: Block -> Set.Set String
535+extractBlockIds (Header _ (ident,_,_) _) | not (null ident) =
536+ Set.singleton ident
537+extractBlockIds (Div (ident,_,_) _) | not (null ident) =
538+ Set.singleton ident
539+extractBlockIds _ = Set.empty
540+
541+-- | Extract the identifiers from an inline element.
542+extractInlineIds :: Inline -> Set.Set String
543+extractInlineIds (Span (ident,_,_) _) | not (null ident) =
544+ Set.singleton ident
545+extractInlineIds _ = Set.empty
546+
547+-- | Extract the identifiers from a pandoc document.
548+extractIds :: Pandoc -> Set.Set String
549+extractIds doc =
550+ query extractBlockIds doc `Set.union` query extractInlineIds doc
551+
532552-- | Generate a unique identifier from a list of inlines.
533553-- Second argument is a list of already used identifiers.
534554uniqueIdent :: [Inline] -> Set.Set String -> String