2011-01-19 18:36:27 -08:00
|
|
|
{-
|
2015-04-26 10:18:29 -07:00
|
|
|
Copyright (C) 2011-2015 John MacFarlane <jgm@berkeley.edu>
|
2011-01-19 18:36:27 -08:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2015-02-18 13:04:37 +00:00
|
|
|
the Free Software Foundation; Either version 2 of the License, or
|
2011-01-19 18:36:27 -08:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
-}
|
|
|
|
|
|
|
|
{- |
|
|
|
|
Module : Text.Pandoc.Readers.Native
|
2015-04-26 10:18:29 -07:00
|
|
|
Copyright : Copyright (C) 2011-2015 John MacFarlane
|
2012-07-26 22:32:53 -07:00
|
|
|
License : GNU GPL, version 2 or above
|
2011-01-19 18:36:27 -08:00
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
|
|
|
Stability : alpha
|
|
|
|
Portability : portable
|
|
|
|
|
2011-01-21 09:00:05 -08:00
|
|
|
Conversion of a string representation of a pandoc type (@Pandoc@,
|
|
|
|
@[Block]@, @Block@, @[Inline]@, or @Inline@) to a @Pandoc@ document.
|
2011-01-19 18:36:27 -08:00
|
|
|
-}
|
|
|
|
module Text.Pandoc.Readers.Native ( readNative ) where
|
|
|
|
|
|
|
|
import Text.Pandoc.Definition
|
2012-08-09 08:11:28 -07:00
|
|
|
import Text.Pandoc.Shared (safeRead)
|
2016-12-10 16:52:35 +01:00
|
|
|
import Text.Pandoc.Options (ReaderOptions)
|
2011-01-19 18:36:27 -08:00
|
|
|
|
2016-11-28 17:13:46 -05:00
|
|
|
import Control.Monad.Except (throwError)
|
2015-02-18 13:04:37 +00:00
|
|
|
import Text.Pandoc.Error
|
2016-11-27 16:38:46 +01:00
|
|
|
import Text.Pandoc.Class
|
2015-02-18 13:04:37 +00:00
|
|
|
|
2011-01-19 18:36:27 -08:00
|
|
|
-- | Read native formatted text and return a Pandoc document.
|
|
|
|
-- The input may be a full pandoc document, a block list, a block,
|
|
|
|
-- an inline list, or an inline. Thus, for example,
|
|
|
|
--
|
|
|
|
-- > Str "hi"
|
|
|
|
--
|
|
|
|
-- will be treated as if it were
|
|
|
|
--
|
2013-05-10 22:53:35 -07:00
|
|
|
-- > Pandoc nullMeta [Plain [Str "hi"]]
|
2011-01-19 18:36:27 -08:00
|
|
|
--
|
2016-11-27 16:38:46 +01:00
|
|
|
readNative :: PandocMonad m
|
2016-12-10 16:52:35 +01:00
|
|
|
=> ReaderOptions
|
|
|
|
-> String -- ^ String to parse (assuming @'\n'@ line endings)
|
2016-11-28 17:13:46 -05:00
|
|
|
-> m Pandoc
|
2016-12-10 16:52:35 +01:00
|
|
|
readNative _ s =
|
2016-11-28 17:13:46 -05:00
|
|
|
case maybe (Pandoc nullMeta <$> readBlocks s) Right (safeRead s) of
|
|
|
|
Right doc -> return doc
|
|
|
|
Left _ -> throwError $ PandocParseError "couldn't read native"
|
2011-01-19 18:36:27 -08:00
|
|
|
|
2015-02-18 13:04:37 +00:00
|
|
|
readBlocks :: String -> Either PandocError [Block]
|
|
|
|
readBlocks s = maybe ((:[]) <$> readBlock s) Right (safeRead s)
|
2011-01-19 18:36:27 -08:00
|
|
|
|
2015-02-18 13:04:37 +00:00
|
|
|
readBlock :: String -> Either PandocError Block
|
|
|
|
readBlock s = maybe (Plain <$> readInlines s) Right (safeRead s)
|
2011-01-19 18:36:27 -08:00
|
|
|
|
2015-02-18 13:04:37 +00:00
|
|
|
readInlines :: String -> Either PandocError [Inline]
|
|
|
|
readInlines s = maybe ((:[]) <$> readInline s) Right (safeRead s)
|
2011-01-19 18:36:27 -08:00
|
|
|
|
2015-02-18 13:04:37 +00:00
|
|
|
readInline :: String -> Either PandocError Inline
|
2016-12-01 12:13:51 -05:00
|
|
|
readInline s = maybe (Left . PandocParseError $ "Could not read: " ++ s) Right (safeRead s)
|
2011-01-19 18:36:27 -08:00
|
|
|
|