git.iko.soy/typeable/pandoc
on master help sign in
RST reader: support anchors.

E.g. `hello` .. _hello: paragraph This is supported by putting "paragraph" in a Div with id `hello`. Closes #262.

commit33a29fbf8720c0d7eec40b7014e3f819b05474ef authorJohn MacFarlane <jgm@berkeley.edu> dateTue 27 Jun 2017, 15:03 (9 years ago) parent563c9c8
2 files changed+40−1
src/Text/Pandoc/Readers/RST.hs+23−1 test/command/262.md+17
src/Text/Pandoc/Readers/RST.hs+23−1
@@ -170,7 +170,8 @@ parseRST = do
170170 -- go through once just to get list of reference keys and notes
171171 -- docMinusKeys is the raw document with blanks where the keys were...
172172 docMinusKeys <- concat <$>
173− manyTill (referenceKey <|> noteBlock <|> citationBlock <|>
173+ manyTill (referenceKey <|> anchorDef <|>
174+ noteBlock <|> citationBlock <|>
174175 headerBlock <|> lineClump) eof
175176 setInput docMinusKeys
176177 setPosition startPos
@@ -217,6 +218,7 @@ block = choice [ codeBlock
217218 , fieldList
218219 , include
219220 , directive
221+ , anchor
220222 , comment
221223 , header
222224 , hrule
@@ -1072,12 +1074,32 @@ regularKey = try $ do
10721074 -- .. _goodbye: url.com
10731075 refs <- referenceNames
10741076 src <- targetURI
1077+ guard $ not (null src)
10751078 --TODO: parse width, height, class and name attributes
10761079 let keys = map (toKey . stripTicks) refs
10771080 forM_ keys $ \key ->
10781081 updateState $ \s -> s { stateKeys = M.insert key ((src,""), nullAttr) $
10791082 stateKeys s }
10801083
1084+anchorDef :: PandocMonad m => RSTParser m [Char]
1085+anchorDef = try $ do
1086+ (refs, raw) <- withRaw (try (referenceNames <* blanklines))
1087+ let keys = map stripTicks refs
1088+ forM_ keys $ \rawkey ->
1089+ updateState $ \s -> s { stateKeys =
1090+ M.insert (toKey rawkey) (('#':rawkey,""), nullAttr) $ stateKeys s }
1091+ -- keep this for 2nd round of parsing, where we'll add the divs (anchor)
1092+ return raw
1093+
1094+anchor :: PandocMonad m => RSTParser m Blocks
1095+anchor = try $ do
1096+ refs <- referenceNames
1097+ blanklines
1098+ b <- block
1099+ -- put identifier on next block:
1100+ let addDiv ref = B.divWith (ref, [], [])
1101+ return $ foldr addDiv b refs
1102+
10811103headerBlock :: PandocMonad m => RSTParser m [Char]
10821104headerBlock = do
10831105 ((txt, _), raw) <- withRaw (doubleHeader' <|> singleHeader')
test/command/262.md+17
@@ -7,3 +7,20 @@
77^D
88<p><a href="example.com">hello</a> and <a href="example.com">goodbye</a></p>
99```
10+
11+```
12+% pandoc -f rst
13+`hello`_ `goodbye`_
14+
15+.. _hello:
16+.. _goodbye:
17+
18+paragraph
19+^D
20+<p><a href="#hello">hello</a> <a href="#goodbye">goodbye</a></p>
21+<div id="hello">
22+<div id="goodbye">
23+<p>paragraph</p>
24+</div>
25+</div>
26+```