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.
This commit is contained in:
John MacFarlane 2017-06-27 14:35:03 +02:00
parent a868b238f2
commit 563c9c8687
2 changed files with 29 additions and 7 deletions

View file

@ -31,7 +31,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Conversion from reStructuredText to 'Pandoc' document.
-}
module Text.Pandoc.Readers.RST ( readRST ) where
import Control.Monad (guard, liftM, mzero, when)
import Control.Monad (guard, liftM, mzero, when, forM_)
import Control.Monad.Identity (Identity(..))
import Control.Monad.Except (throwError)
import Data.Char (isHexDigit, isSpace, toLower, toUpper)
@ -1054,16 +1054,29 @@ stripTicks = reverse . stripTick . reverse . stripTick
where stripTick ('`':xs) = xs
stripTick xs = xs
referenceNames :: PandocMonad m => RSTParser m [String]
referenceNames = do
let rn = try $ do
string ".. _"
(_, ref) <- withRaw referenceName
char ':'
return ref
first <- rn
rest <- many (try (blanklines *> rn))
return (first:rest)
regularKey :: PandocMonad m => RSTParser m ()
regularKey = try $ do
string ".. _"
(_,ref) <- withRaw referenceName
char ':'
-- we allow several references to the same URL, e.g.
-- .. _hello:
-- .. _goodbye: url.com
refs <- referenceNames
src <- targetURI
let key = toKey $ stripTicks ref
--TODO: parse width, height, class and name attributes
updateState $ \s -> s { stateKeys = M.insert key ((src,""), nullAttr) $
stateKeys s }
let keys = map (toKey . stripTicks) refs
forM_ keys $ \key ->
updateState $ \s -> s { stateKeys = M.insert key ((src,""), nullAttr) $
stateKeys s }
headerBlock :: PandocMonad m => RSTParser m [Char]
headerBlock = do

9
test/command/262.md Normal file
View file

@ -0,0 +1,9 @@
```
% pandoc -f rst
`hello`_ and `goodbye`_
.. _hello:
.. _goodbye: example.com
^D
<p><a href="example.com">hello</a> and <a href="example.com">goodbye</a></p>
```