aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc
diff options
context:
space:
mode:
authorJohn MacFarlane <fiddlosopher@gmail.com>2013-02-21 19:53:35 -0800
committerJohn MacFarlane <fiddlosopher@gmail.com>2013-02-21 19:53:35 -0800
commit07e8cedf2bddcbf13bfd69a415107b2a9aa074aa (patch)
treed9e045913e7baee9a724fa8adb8e0ff4a4a5aef8 /src/Text/Pandoc
parent7a97369d011cdbdac52a2e0df6bcbba078852a57 (diff)
downloadpandoc-07e8cedf2bddcbf13bfd69a415107b2a9aa074aa.tar.gz
Make `implicit_header_references` work with explicit header ids.
(Markdown reader.)
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r--src/Text/Pandoc/Parsing.hs4
-rw-r--r--src/Text/Pandoc/Readers/Markdown.hs39
2 files changed, 25 insertions, 18 deletions
diff --git a/src/Text/Pandoc/Parsing.hs b/src/Text/Pandoc/Parsing.hs
index 0bb609e5d..44a64c80a 100644
--- a/src/Text/Pandoc/Parsing.hs
+++ b/src/Text/Pandoc/Parsing.hs
@@ -794,7 +794,7 @@ data ParserState = ParserState
stateAuthors :: [[Inline]], -- ^ Authors of document
stateDate :: [Inline], -- ^ Date of document
stateHeaderTable :: [HeaderType], -- ^ Ordered list of header types used
- stateHeaders :: [[Inline]], -- ^ List of headers (used for implicit ref links)
+ stateHeaders :: M.Map Inlines String, -- ^ List of headers and ids (used for implicit ref links)
stateIdentifiers :: [String], -- ^ List of header identifiers used
stateNextExample :: Int, -- ^ Number of next example
stateExamples :: M.Map String Int, -- ^ Map from example labels to numbers
@@ -823,7 +823,7 @@ defaultParserState =
stateAuthors = [],
stateDate = [],
stateHeaderTable = [],
- stateHeaders = [],
+ stateHeaders = M.empty,
stateIdentifiers = [],
stateNextExample = 1,
stateExamples = M.empty,
diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs
index e037c0897..29eac02bf 100644
--- a/src/Text/Pandoc/Readers/Markdown.hs
+++ b/src/Text/Pandoc/Readers/Markdown.hs
@@ -390,15 +390,22 @@ header = setextHeader <|> atxHeader <?> "header"
-- returns unique identifier
addToHeaderList :: Attr -> F Inlines -> MarkdownParser Attr
addToHeaderList (ident,classes,kvs) text = do
- let headerList = B.toList $ runF text defaultParserState
- updateState $ \st -> st{ stateHeaders = headerList : stateHeaders st }
- (do guardEnabled Ext_auto_identifiers
- ids <- stateIdentifiers `fmap` getState
- let id' = if null ident
- then uniqueIdent headerList ids
- else ident
- updateState $ \st -> st{ stateIdentifiers = id' : ids }
- return (id',classes,kvs)) <|> return ("",classes,kvs)
+ let header' = runF text defaultParserState
+ exts <- getOption readerExtensions
+ let insert' = M.insertWith (\_new old -> old)
+ if null ident && Ext_auto_identifiers `Set.member` exts
+ then do
+ ids <- stateIdentifiers `fmap` getState
+ let id' = uniqueIdent (B.toList header') ids
+ updateState $ \st -> st{
+ stateIdentifiers = id' : ids,
+ stateHeaders = insert' header' id' $ stateHeaders st }
+ return (id',classes,kvs)
+ else do
+ unless (null ident) $
+ updateState $ \st -> st{
+ stateHeaders = insert' header' ident $ stateHeaders st }
+ return (ident,classes,kvs)
atxHeader :: MarkdownParser (F Blocks)
atxHeader = try $ do
@@ -1539,13 +1546,13 @@ referenceLink constructor (lab, raw) = do
case M.lookup key keys of
Nothing -> do
headers <- asksF stateHeaders
- let ref' = B.toList $ runF (if labIsRef then lab else ref)
- defaultParserState
- if implicitHeaderRefs && ref' `elem` headers
- then do
- let src = '#' : uniqueIdent ref' []
- constructor src "" <$> lab
- else (\x -> B.str "[" <> x <> B.str "]" <> B.str raw') <$> fallback
+ let ref' = runF (if labIsRef then lab else ref) defaultParserState
+ let makeFallback x = B.str "[" <> x <> B.str "]" <> B.str raw'
+ if implicitHeaderRefs
+ then case M.lookup ref' headers of
+ Just ident -> constructor ('#':ident) "" <$> lab
+ Nothing -> makeFallback <$> fallback
+ else makeFallback <$> fallback
Just (src,tit) -> constructor src tit <$> lab
bareURL :: MarkdownParser (F Inlines)