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