module Language.Haskell.HsColour (Output(..), ColourPrefs(..),
hscolour) where
import Language.Haskell.HsColour.Colourise (ColourPrefs(..))
import qualified Language.Haskell.HsColour.TTY as TTY
import qualified Language.Haskell.HsColour.HTML as HTML
import qualified Language.Haskell.HsColour.CSS as CSS
import qualified Language.Haskell.HsColour.ACSS as ACSS
import qualified Language.Haskell.HsColour.InlineCSS as ICSS
import qualified Language.Haskell.HsColour.LaTeX as LaTeX
import qualified Language.Haskell.HsColour.MIRC as MIRC
import Data.List(mapAccumL, isPrefixOf)
import Data.Maybe
import Language.Haskell.HsColour.Output
hscolour :: Output
-> ColourPrefs
-> Bool
-> Bool
-> String
-> Bool
-> String
-> String
hscolour :: Output
-> ColourPrefs
-> Bool
-> Bool
-> String
-> Bool
-> String
-> String
hscolour Output
output ColourPrefs
pref Bool
anchor Bool
partial String
title Bool
False =
(if Bool
partial then forall a. a -> a
id else Output -> String -> String -> String
top'n'tail Output
output String
title) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Output -> ColourPrefs -> Bool -> Int -> String -> String
hscolour' Output
output ColourPrefs
pref Bool
anchor Int
1
hscolour Output
output ColourPrefs
pref Bool
anchor Bool
partial String
title Bool
True =
(if Bool
partial then forall a. a -> a
id else Output -> String -> String -> String
top'n'tail Output
output String
title) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Lit] -> [String]
chunk Int
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Lit] -> [Lit]
joinL forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> [Lit]
classify forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
inlines
where
chunk :: Int -> [Lit] -> [String]
chunk Int
_ [] = []
chunk Int
n (Code String
c: [Lit]
cs) = Output -> ColourPrefs -> Bool -> Int -> String -> String
hscolour' Output
output ColourPrefs
pref Bool
anchor Int
n String
c
forall a. a -> [a] -> [a]
: Int -> [Lit] -> [String]
chunk (Int
n forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. Foldable t => t a -> Int
length (String -> [String]
lines String
c)) [Lit]
cs
chunk Int
n (Lit String
c: [Lit]
cs) = String
c forall a. a -> [a] -> [a]
: Int -> [Lit] -> [String]
chunk Int
n [Lit]
cs
hscolour' :: Output
-> ColourPrefs
-> Bool
-> Int
-> String
-> String
hscolour' :: Output -> ColourPrefs -> Bool -> Int -> String -> String
hscolour' Output
TTY ColourPrefs
pref Bool
_ Int
_ = ColourPrefs -> String -> String
TTY.hscolour ColourPrefs
pref
hscolour' (TTYg TerminalType
tt) ColourPrefs
pref Bool
_ Int
_ = TerminalType -> ColourPrefs -> String -> String
TTY.hscolourG TerminalType
tt ColourPrefs
pref
hscolour' Output
MIRC ColourPrefs
pref Bool
_ Int
_ = ColourPrefs -> String -> String
MIRC.hscolour ColourPrefs
pref
hscolour' Output
LaTeX ColourPrefs
pref Bool
_ Int
_ = ColourPrefs -> String -> String
LaTeX.hscolour ColourPrefs
pref
hscolour' Output
HTML ColourPrefs
pref Bool
anchor Int
n = ColourPrefs -> Bool -> Int -> String -> String
HTML.hscolour ColourPrefs
pref Bool
anchor Int
n
hscolour' Output
CSS ColourPrefs
_ Bool
anchor Int
n = Bool -> Int -> String -> String
CSS.hscolour Bool
anchor Int
n
hscolour' Output
ICSS ColourPrefs
pref Bool
anchor Int
n = ColourPrefs -> Bool -> Int -> String -> String
ICSS.hscolour ColourPrefs
pref Bool
anchor Int
n
hscolour' Output
ACSS ColourPrefs
_ Bool
anchor Int
n = Bool -> Int -> String -> String
ACSS.hscolour Bool
anchor Int
n
top'n'tail :: Output
-> String
-> (String->String)
top'n'tail :: Output -> String -> String -> String
top'n'tail Output
TTY String
_ = forall a. a -> a
id
top'n'tail (TTYg TerminalType
_) String
_ = forall a. a -> a
id
top'n'tail Output
MIRC String
_ = forall a. a -> a
id
top'n'tail Output
LaTeX String
title = String -> String -> String
LaTeX.top'n'tail String
title
top'n'tail Output
HTML String
title = String -> String -> String
HTML.top'n'tail String
title
top'n'tail Output
CSS String
title = String -> String -> String
CSS.top'n'tail String
title
top'n'tail Output
ICSS String
title = String -> String -> String
ICSS.top'n'tail String
title
top'n'tail Output
ACSS String
title = String -> String -> String
CSS.top'n'tail String
title
data Lit = Code {Lit -> String
unL :: String} | Lit {unL :: String} deriving (Int -> Lit -> String -> String
[Lit] -> String -> String
Lit -> String
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [Lit] -> String -> String
$cshowList :: [Lit] -> String -> String
show :: Lit -> String
$cshow :: Lit -> String
showsPrec :: Int -> Lit -> String -> String
$cshowsPrec :: Int -> Lit -> String -> String
Show)
inlines :: String -> [String]
inlines :: String -> [String]
inlines String
s = String -> (String -> String) -> [String]
lines' String
s forall a. a -> a
id
where
lines' :: String -> (String -> String) -> [String]
lines' [] String -> String
acc = [String -> String
acc []]
lines' (Char
'\^M':Char
'\n':String
s) String -> String
acc = String -> String
acc [Char
'\n'] forall a. a -> [a] -> [a]
: String -> (String -> String) -> [String]
lines' String
s forall a. a -> a
id
lines' (Char
'\n':String
s) String -> String
acc = String -> String
acc [Char
'\n'] forall a. a -> [a] -> [a]
: String -> (String -> String) -> [String]
lines' String
s forall a. a -> a
id
lines' (Char
c:String
s) String -> String
acc = String -> (String -> String) -> [String]
lines' String
s (String -> String
acc forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char
cforall a. a -> [a] -> [a]
:))
classify :: [String] -> [Lit]
classify :: [String] -> [Lit]
classify [] = []
classify (String
x:[String]
xs) | String
"\\begin{code}"forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf`String
x
= String -> Lit
Lit String
xforall a. a -> [a] -> [a]
: String -> [String] -> [Lit]
allProg String
"code" [String]
xs
classify (String
x:[String]
xs) | String
"\\begin{spec}"forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf`String
x
= String -> Lit
Lit String
xforall a. a -> [a] -> [a]
: String -> [String] -> [Lit]
allProg String
"spec" [String]
xs
classify ((Char
'>':String
x):[String]
xs) = String -> Lit
Code (Char
'>'forall a. a -> [a] -> [a]
:String
x) forall a. a -> [a] -> [a]
: [String] -> [Lit]
classify [String]
xs
classify (String
x:[String]
xs) = String -> Lit
Lit String
xforall a. a -> [a] -> [a]
: [String] -> [Lit]
classify [String]
xs
allProg :: String -> [String] -> [Lit]
allProg String
name = [String] -> [Lit]
go
where
end :: String
end = String
"\\end{" forall a. [a] -> [a] -> [a]
++ String
name forall a. [a] -> [a] -> [a]
++ String
"}"
go :: [String] -> [Lit]
go [] = []
go (String
x:[String]
xs) | String
end `isPrefixOf `String
x
= String -> Lit
Lit String
xforall a. a -> [a] -> [a]
: [String] -> [Lit]
classify [String]
xs
go (String
x:[String]
xs) = String -> Lit
Code String
xforall a. a -> [a] -> [a]
: [String] -> [Lit]
go [String]
xs
joinL :: [Lit] -> [Lit]
joinL :: [Lit] -> [Lit]
joinL [] = []
joinL (Code String
c:Code String
c2:[Lit]
xs) = [Lit] -> [Lit]
joinL (String -> Lit
Code (String
cforall a. [a] -> [a] -> [a]
++String
c2)forall a. a -> [a] -> [a]
:[Lit]
xs)
joinL (Lit String
c :Lit String
c2 :[Lit]
xs) = [Lit] -> [Lit]
joinL (String -> Lit
Lit (String
cforall a. [a] -> [a] -> [a]
++String
c2)forall a. a -> [a] -> [a]
:[Lit]
xs)
joinL (Lit
any:[Lit]
xs) = Lit
anyforall a. a -> [a] -> [a]
: [Lit] -> [Lit]
joinL [Lit]
xs