aboutsummaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-06-27 15:03:16 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2017-06-27 15:03:16 +0200
commit33a29fbf8720c0d7eec40b7014e3f819b05474ef (patch)
treef8a1cd6375025546fae1238dfe25b0233d31e912 /src/Text
parent563c9c8687a62acc7361fb49126a1d2030f3a11e (diff)
downloadpandoc-33a29fbf8720c0d7eec40b7014e3f819b05474ef.tar.gz
RST reader: support anchors.
E.g. `hello` .. _hello: paragraph This is supported by putting "paragraph" in a Div with id `hello`. Closes #262.
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Readers/RST.hs24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs
index c790d5188..2daf60a89 100644
--- a/src/Text/Pandoc/Readers/RST.hs
+++ b/src/Text/Pandoc/Readers/RST.hs
@@ -170,7 +170,8 @@ parseRST = do
-- go through once just to get list of reference keys and notes
-- docMinusKeys is the raw document with blanks where the keys were...
docMinusKeys <- concat <$>
- manyTill (referenceKey <|> noteBlock <|> citationBlock <|>
+ manyTill (referenceKey <|> anchorDef <|>
+ noteBlock <|> citationBlock <|>
headerBlock <|> lineClump) eof
setInput docMinusKeys
setPosition startPos
@@ -217,6 +218,7 @@ block = choice [ codeBlock
, fieldList
, include
, directive
+ , anchor
, comment
, header
, hrule
@@ -1072,12 +1074,32 @@ regularKey = try $ do
-- .. _goodbye: url.com
refs <- referenceNames
src <- targetURI
+ guard $ not (null src)
--TODO: parse width, height, class and name attributes
let keys = map (toKey . stripTicks) refs
forM_ keys $ \key ->
updateState $ \s -> s { stateKeys = M.insert key ((src,""), nullAttr) $
stateKeys s }
+anchorDef :: PandocMonad m => RSTParser m [Char]
+anchorDef = try $ do
+ (refs, raw) <- withRaw (try (referenceNames <* blanklines))
+ let keys = map stripTicks refs
+ forM_ keys $ \rawkey ->
+ updateState $ \s -> s { stateKeys =
+ M.insert (toKey rawkey) (('#':rawkey,""), nullAttr) $ stateKeys s }
+ -- keep this for 2nd round of parsing, where we'll add the divs (anchor)
+ return raw
+
+anchor :: PandocMonad m => RSTParser m Blocks
+anchor = try $ do
+ refs <- referenceNames
+ blanklines
+ b <- block
+ -- put identifier on next block:
+ let addDiv ref = B.divWith (ref, [], [])
+ return $ foldr addDiv b refs
+
headerBlock :: PandocMonad m => RSTParser m [Char]
headerBlock = do
((txt, _), raw) <- withRaw (doubleHeader' <|> singleHeader')