Skip to content

Commit b0b378e

Browse files
committed
Tag identifiers of included sections with title of included file
1 parent ad1f72c commit b0b378e

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

IncludeFilter.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,33 @@ import Text.Pandoc.Walk
8282

8383
stripPandoc :: Int -> Either PandocError Pandoc -> [Block]
8484
stripPandoc _ (Left _) = [Null]
85-
stripPandoc changeInHeaderLevel (Right (Pandoc meta blocks)) = maybe id (:) (title meta) $ modBlocks
85+
stripPandoc changeInHeaderLevel (Right (Pandoc meta blocks)) = maybe id (:) title modBlocks
8686
where
87-
modBlocks = modifyHeaderLevelBlockWith changeInHeaderLevel <$> blocks
88-
title (Meta (Map.lookup "title" -> Just (MetaInlines inls))) = do
87+
getTitle (Meta (Map.lookup "title" -> Just (MetaInlines inls))) = Just inls
88+
getTitle _ = Nothing
89+
theTitle = getTitle meta
90+
modBlocks = modifyHeaderLevelBlockWith changeInHeaderLevel (titleRef <$> theTitle) <$> blocks
91+
title = do
92+
inls <- theTitle
8993
guard $ changeInHeaderLevel > 0
9094
Just $ Header changeInHeaderLevel (titleRef inls,["section-title"],[]) inls
91-
title _ = Nothing
9295
-- WARNING titleRef doesn't check that titles are unique; for that try uniqueIdent.
9396
titleRef = stringify . fmap (lowerCase . dashFromSpace)
9497
dashFromSpace Space = Str "-"
9598
dashFromSpace x = x
9699
lowerCase (Str x) = Str (fmap C.toLower x)
97100
lowerCase x = x
98101

99-
modifyHeaderLevelBlockWith :: Int -> Block -> Block
100-
modifyHeaderLevelBlockWith n (Header int att inls) = Header (int + n) att inls
101-
modifyHeaderLevelBlockWith _ x = x
102+
amendIdentifier :: String -> Attr -> Attr
103+
amendIdentifier title (ident, cls, kvs) = (concat [title, "-", ident], cls, kvs)
104+
105+
modifyHeaderLevelBlockWith :: Int -> Maybe String -> Block -> Block
106+
modifyHeaderLevelBlockWith n mtitle (Header int att inls) =
107+
Header (int + n) (maybe id amendIdentifier mtitle att) inls
108+
modifyHeaderLevelBlockWith _ _ x = x
102109

103110
modifyHeaderLevelWith :: Int -> Pandoc -> Pandoc
104-
modifyHeaderLevelWith n = walk (modifyHeaderLevelBlockWith n)
111+
modifyHeaderLevelWith n = walk (modifyHeaderLevelBlockWith n mempty)
105112

106113
ioReadMarkdown :: String -> IO(Either PandocError Pandoc)
107114
ioReadMarkdown content = return $! readMarkdown def content

0 commit comments

Comments
 (0)