aboutsummaryrefslogtreecommitdiff
path: root/LaTeX.icl
blob: fba573e04d1209eb2ccd5f7b3ed7b18abb885c77 (plain) (blame)
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
implementation module LaTeX

from StdFunc import seq
from StdList import ++, map
from StdOverloaded import class toString(..), class +++(..)
from StdString import instance +++ {#Char}

from Text import class Text(replaceSubString,concat), instance Text String

instance toString [a] | toString a
where toString xs = concat (map toString xs)

instance toString LaTeX
where
	toString (Command "justifies" []) = "\n\\justifies{}"
	toString (Command cmd lts) = "\\" +++ cmd +++
		concat ["{" +++ toString lt +++ "}" \\ lt <- lts]
	toString (Environment env lt) = toString
		([Command "begin" [Text env]] ++ lt ++ [Command "env" [Text env]])
	toString (Math True lt) = "$$" +++ toString lt +++ "$$"
	toString (Math False lt) = "$" +++ toString lt +++ "$"
	toString (List lts) = concat (map toString lts)
	toString (Raw s) = s
	toString (Text s) = escape s
	where
		escape :: !String -> String // From Text.LaTeX
		escape s = seq (map (\(x,y) -> replaceSubString x y) escape`) s
		where
			escape` = [ ("\\", "\\textbackslash{}")
			          , ("^", "\\textasciicircum{}")
			          , ("~", "\\textasciitilde{}")
			          , ("*", "\\textasteriskcentered{}")
			          , ("|", "\\textbar{}")
			          , ("$", "\\textdollar{}")
			          , (">", "\\textgreater{}")
			          , ("<", "\\textless{}")
			          , ("\"", "\\textquotedblright{}")
			          , ("'", "\\textquoteright{}")
			          , ("_", "\\textunderscore{}")
			          , ("&", "\\&{}")
			          ]