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