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