summaryrefslogtreecommitdiff
path: root/Text/Pandoc/Readers/Plain.hs
blob: a66b1cd563c1b7630e0377e22f75bea53d3b28d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{-
Copyright (C) 2017  defanor <defanor@uberspace.net>

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
the Free Software Foundation, either version 3 of the License, or
(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, see <http://www.gnu.org/licenses/>.
-}

{- |
Module      :  Text.Pandoc.Readers.Plain
Maintainer  :  defanor <defanor@uberspace.net>
Stability   :  unstable
Portability :  portable
-}

{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Readers.Plain ( readPlain
                                 , lineToInlines
                                 ) where

import Text.Pandoc.Definition
import Text.Pandoc.Class
import qualified Data.Text as T


-- | Translates a text line into a list of 'Inline' elements suitable
-- for further processing.
lineToInlines :: String -> [Inline]
lineToInlines [] = []
lineToInlines (' ':rest) = Space : lineToInlines rest
lineToInlines s = let (cur, next) = break (== ' ') s
                  in Str cur : lineToInlines next

-- | Reads plain text, always succeeding and producing a single
-- 'Plain' block.
readPlain :: PandocMonad m => T.Text -> m Pandoc
readPlain = pure . Pandoc mempty . pure . LineBlock
  . map (\l -> (lineToInlines $ T.unpack l))
  . T.lines . T.filter (/= '\r')