module Text.Highlighting.Kate.Format.LaTeX (
formatLaTeXInline, formatLaTeXBlock, styleToLaTeX
) where
import Text.Highlighting.Kate.Types
import Text.Printf
import Data.List (intercalate)
import Control.Monad (mplus)
import Data.Char (isSpace)
formatLaTeX :: Bool -> [SourceLine] -> String
formatLaTeX :: Bool -> [SourceLine] -> [Char]
formatLaTeX Bool
inline = forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"\n" forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (Bool -> SourceLine -> [Char]
sourceLineToLaTeX Bool
inline)
formatLaTeXInline :: FormatOptions -> [SourceLine] -> String
formatLaTeXInline :: FormatOptions -> [SourceLine] -> [Char]
formatLaTeXInline FormatOptions
_opts [SourceLine]
ls = [Char]
"\\VERB|" forall a. [a] -> [a] -> [a]
++ Bool -> [SourceLine] -> [Char]
formatLaTeX Bool
True [SourceLine]
ls forall a. [a] -> [a] -> [a]
++ [Char]
"|"
sourceLineToLaTeX :: Bool -> SourceLine -> String
sourceLineToLaTeX :: Bool -> SourceLine -> [Char]
sourceLineToLaTeX Bool
inline SourceLine
contents = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Bool -> Token -> [Char]
tokenToLaTeX Bool
inline) SourceLine
contents
tokenToLaTeX :: Bool -> Token -> String
tokenToLaTeX :: Bool -> Token -> [Char]
tokenToLaTeX Bool
inline (TokenType
NormalTok, [Char]
txt) | forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Char -> Bool
isSpace [Char]
txt = Bool -> [Char] -> [Char]
escapeLaTeX Bool
inline [Char]
txt
tokenToLaTeX Bool
inline (TokenType
toktype, [Char]
txt) = Char
'\\'forall a. a -> [a] -> [a]
:(forall a. Show a => a -> [Char]
show TokenType
toktype forall a. [a] -> [a] -> [a]
++ [Char]
"{" forall a. [a] -> [a] -> [a]
++ Bool -> [Char] -> [Char]
escapeLaTeX Bool
inline [Char]
txt forall a. [a] -> [a] -> [a]
++ [Char]
"}")
escapeLaTeX :: Bool -> String -> String
escapeLaTeX :: Bool -> [Char] -> [Char]
escapeLaTeX Bool
inline = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Char -> [Char]
escapeLaTeXChar
where escapeLaTeXChar :: Char -> [Char]
escapeLaTeXChar Char
'\\' = [Char]
"\\textbackslash{}"
escapeLaTeXChar Char
'{' = [Char]
"\\{"
escapeLaTeXChar Char
'}' = [Char]
"\\}"
escapeLaTeXChar Char
'|' = if Bool
inline
then [Char]
"\\VerbBar{}"
else [Char]
"|"
escapeLaTeXChar Char
x = [Char
x]
formatLaTeXBlock :: FormatOptions -> [SourceLine] -> String
formatLaTeXBlock :: FormatOptions -> [SourceLine] -> [Char]
formatLaTeXBlock FormatOptions
opts [SourceLine]
ls = [[Char]] -> [Char]
unlines
[[Char]
"\\begin{Shaded}"
,[Char]
"\\begin{Highlighting}[" forall a. [a] -> [a] -> [a]
++
(if FormatOptions -> Bool
numberLines FormatOptions
opts
then [Char]
"numbers=left," forall a. [a] -> [a] -> [a]
++
(if FormatOptions -> Int
startNumber FormatOptions
opts forall a. Eq a => a -> a -> Bool
== Int
1
then [Char]
""
else [Char]
",firstnumber=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show (FormatOptions -> Int
startNumber FormatOptions
opts)) forall a. [a] -> [a] -> [a]
++ [Char]
","
else [Char]
"") forall a. [a] -> [a] -> [a]
++ [Char]
"]"
,Bool -> [SourceLine] -> [Char]
formatLaTeX Bool
False [SourceLine]
ls
,[Char]
"\\end{Highlighting}"
,[Char]
"\\end{Shaded}"]
styleToLaTeX :: Style -> String
styleToLaTeX :: Style -> [Char]
styleToLaTeX Style
f = [[Char]] -> [Char]
unlines forall a b. (a -> b) -> a -> b
$
[ [Char]
"\\usepackage{color}"
, [Char]
"\\usepackage{fancyvrb}"
, [Char]
"\\newcommand{\\VerbBar}{|}"
, [Char]
"\\newcommand{\\VERB}{\\Verb[commandchars=\\\\\\{\\}]}"
, [Char]
"\\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\\\\{\\}}"
, [Char]
"% Add ',fontsize=\\small' for more characters per line"
] forall a. [a] -> [a] -> [a]
++
(case Style -> Maybe Color
backgroundColor Style
f of
Maybe Color
Nothing -> [[Char]
"\\newenvironment{Shaded}{}{}"]
Just (RGB Word8
r Word8
g Word8
b) -> [[Char]
"\\usepackage{framed}"
,forall r. PrintfType r => [Char] -> r
printf [Char]
"\\definecolor{shadecolor}{RGB}{%d,%d,%d}" Word8
r Word8
g Word8
b
,[Char]
"\\newenvironment{Shaded}{\\begin{snugshade}}{\\end{snugshade}}"])
forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (Maybe Color -> [(TokenType, TokenStyle)] -> TokenType -> [Char]
macrodef (Style -> Maybe Color
defaultColor Style
f) (Style -> [(TokenType, TokenStyle)]
tokenStyles Style
f)) (forall a. Enum a => a -> a -> [a]
enumFromTo TokenType
KeywordTok TokenType
NormalTok)
macrodef :: Maybe Color -> [(TokenType, TokenStyle)] -> TokenType -> String
macrodef :: Maybe Color -> [(TokenType, TokenStyle)] -> TokenType -> [Char]
macrodef Maybe Color
defaultcol [(TokenType, TokenStyle)]
tokstyles TokenType
tokt = [Char]
"\\newcommand{\\" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show TokenType
tokt forall a. [a] -> [a] -> [a]
++
[Char]
"}[1]{" forall a. [a] -> [a] -> [a]
++ (forall {t}. (PrintfArg t, PrintfType t) => t -> t
co forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
ul forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
bf forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Char]
it forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {t}. (PrintfArg t, PrintfType t) => t -> t
bg forall a b. (a -> b) -> a -> b
$ [Char]
"{#1}") forall a. [a] -> [a] -> [a]
++ [Char]
"}"
where tokf :: TokenStyle
tokf = case forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup TokenType
tokt [(TokenType, TokenStyle)]
tokstyles of
Maybe TokenStyle
Nothing -> TokenStyle
defStyle
Just TokenStyle
x -> TokenStyle
x
ul :: [Char] -> [Char]
ul [Char]
x = if TokenStyle -> Bool
tokenUnderline TokenStyle
tokf
then [Char]
"\\underline{" forall a. [a] -> [a] -> [a]
++ [Char]
x forall a. [a] -> [a] -> [a]
++ [Char]
"}"
else [Char]
x
it :: [Char] -> [Char]
it [Char]
x = if TokenStyle -> Bool
tokenItalic TokenStyle
tokf
then [Char]
"\\textit{" forall a. [a] -> [a] -> [a]
++ [Char]
x forall a. [a] -> [a] -> [a]
++ [Char]
"}"
else [Char]
x
bf :: [Char] -> [Char]
bf [Char]
x = if TokenStyle -> Bool
tokenBold TokenStyle
tokf
then [Char]
"\\textbf{" forall a. [a] -> [a] -> [a]
++ [Char]
x forall a. [a] -> [a] -> [a]
++ [Char]
"}"
else [Char]
x
bcol :: Maybe (Double, Double, Double)
bcol = forall a. FromColor a => Color -> a
fromColor forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` TokenStyle -> Maybe Color
tokenBackground TokenStyle
tokf :: Maybe (Double, Double, Double)
bg :: t -> t
bg t
x = case Maybe (Double, Double, Double)
bcol of
Maybe (Double, Double, Double)
Nothing -> t
x
Just (Double
r, Double
g, Double
b) -> forall r. PrintfType r => [Char] -> r
printf [Char]
"\\colorbox[rgb]{%0.2f,%0.2f,%0.2f}{%s}" Double
r Double
g Double
b t
x
col :: Maybe (Double, Double, Double)
col = forall a. FromColor a => Color -> a
fromColor forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap`
(TokenStyle -> Maybe Color
tokenColor TokenStyle
tokf forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Maybe Color
defaultcol) :: Maybe (Double, Double, Double)
co :: t -> t
co t
x = case Maybe (Double, Double, Double)
col of
Maybe (Double, Double, Double)
Nothing -> t
x
Just (Double
r, Double
g, Double
b) -> forall r. PrintfType r => [Char] -> r
printf [Char]
"\\textcolor[rgb]{%0.2f,%0.2f,%0.2f}{%s}" Double
r Double
g Double
b t
x