{- |
Module      :  ./Common/Unlit.hs
Description :  unlit a source string
Copyright   :  (c) Christian Maeder, DFKI GmbH 2010
License     :  GPLv2 or higher, see LICENSE.txt
Maintainer  :  Christian.Maeder@dfki.de
Stability   :  provisional
Portability :  portable

a simple version to unlit a source string
-}

module Common.Unlit (unlit) where

import Data.List

unlit :: String -> String
unlit :: String -> String
unlit s :: String
s = let cs :: [String]
cs = Bool -> [String] -> [String]
getCode Bool
True ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines String
s in
  if (String -> Bool) -> [String] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
cs then "" else [String] -> String
unlines [String]
cs

-- keep code positions intact
getCode :: Bool -> [String] -> [String]
getCode :: Bool -> [String] -> [String]
getCode d :: Bool
d l :: [String]
l = case [String]
l of
  [] -> []
  s :: String
s : r :: [String]
r -> if Bool -> Bool
not Bool
d Bool -> Bool -> Bool
&& String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf "\\end{code}" String
s
           Bool -> Bool -> Bool
|| Bool
d Bool -> Bool -> Bool
&& String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
isPrefixOf "\\begin{code}" String
s
      then "" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: Bool -> [String] -> [String]
getCode (Bool -> Bool
not Bool
d) [String]
r
      else (case String
s of
      '>' : t :: String
t | Bool
d -> ' ' Char -> String -> String
forall a. a -> [a] -> [a]
: String
t
      _ -> if Bool
d then "" else String
s) String -> [String] -> [String]
forall a. a -> [a] -> [a]
: Bool -> [String] -> [String]
getCode Bool
d [String]
r