git.iko.soy/typeable/pandoc
on master help sign in
RST reader: Handle chained link definitions.

For example, .. _hello: .. _goodbye: example.com Here both `hello` and `goodbye` should link to `example.com`. Fixes the first part of #262.

commit563c9c8687a62acc7361fb49126a1d2030f3a11e authorJohn MacFarlane <jgm@berkeley.edu> dateTue 27 Jun 2017, 14:35 (9 years ago) parenta868b23
2 files changed+29−7
src/Text/Pandoc/Readers/RST.hs+20−7 test/command/262.md+9
src/Text/Pandoc/Readers/RST.hs+20−7
@@ -31,7 +31,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3131Conversion from reStructuredText to 'Pandoc' document.
3232-}
3333module Text.Pandoc.Readers.RST ( readRST ) where
34−import Control.Monad (guard, liftM, mzero, when)
34+import Control.Monad (guard, liftM, mzero, when, forM_)
3535import Control.Monad.Identity (Identity(..))
3636import Control.Monad.Except (throwError)
3737import Data.Char (isHexDigit, isSpace, toLower, toUpper)
@@ -1054,16 +1054,29 @@ stripTicks = reverse . stripTick . reverse . stripTick
10541054 where stripTick ('`':xs) = xs
10551055 stripTick xs = xs
10561056
1057+referenceNames :: PandocMonad m => RSTParser m [String]
1058+referenceNames = do
1059+ let rn = try $ do
1060+ string ".. _"
1061+ (_, ref) <- withRaw referenceName
1062+ char ':'
1063+ return ref
1064+ first <- rn
1065+ rest <- many (try (blanklines *> rn))
1066+ return (first:rest)
1067+
10571068regularKey :: PandocMonad m => RSTParser m ()
10581069regularKey = try $ do
1059− string ".. _"
1060− (_,ref) <- withRaw referenceName
1061− char ':'
1070+ -- we allow several references to the same URL, e.g.
1071+ -- .. _hello:
1072+ -- .. _goodbye: url.com
1073+ refs <- referenceNames
10621074 src <- targetURI
1063− let key = toKey $ stripTicks ref
10641075 --TODO: parse width, height, class and name attributes
1065− updateState $ \s -> s { stateKeys = M.insert key ((src,""), nullAttr) $
1066− stateKeys s }
1076+ let keys = map (toKey . stripTicks) refs
1077+ forM_ keys $ \key ->
1078+ updateState $ \s -> s { stateKeys = M.insert key ((src,""), nullAttr) $
1079+ stateKeys s }
10671080
10681081headerBlock :: PandocMonad m => RSTParser m [Char]
10691082headerBlock = do
test/command/262.mdnew file+9
@@ -0,0 +1,9 @@
1+```
2+% pandoc -f rst
3+`hello`_ and `goodbye`_
4+
5+.. _hello:
6+.. _goodbye: example.com
7+^D
8+<p><a href="example.com">hello</a> and <a href="example.com">goodbye</a></p>
9+```