3 import Text
.HTML
.TagSoup
5 import System
.Environment
(getArgs)
8 readDoc
:: String -> Pandoc
9 readDoc
= readHtml defaultParserState
{
13 writeDoc
:: Pandoc
-> String
14 writeDoc
= writeLaTeX defaultWriterOptions
17 convertAll
:: Pandoc
-> Pandoc
18 convertAll
= processWith convertInline
20 --convertInline :: [Inline] -> [Inline]
21 --convertInline (HtmlInline s : xs) = tag2latex $ parseTags s : convertInline xs
22 --convertInline (x : xs) = x : convertInline xs
23 --convertInline [] = []
25 convertInline
:: Inline
-> Inline
26 convertInline
(HtmlInline s
) = tag2latex
$ parseTags s
29 tag2latex
:: [Tag
String] -> Inline
30 tag2latex
[TagOpen str attr
]
31 | str
== "latex" = TeX
(attr2latex attr
)
32 | startswith
"env" str
= TeX
33 ("\\begin{" ++ (drop 3 str
) ++ "}" ++ (attr2latex attr
))
35 tag2latex
[TagClose str
]
36 | str
== "latex" = TeX
"}"
37 | startswith
"env" str
= TeX
("\\end{" ++ (drop 3 str
) ++ "}")
39 tag2latex
(t
:ts
) = TeX
(strTex
(tag2latex
[t
]) ++ strTex
(tag2latex ts
))
40 where strTex
(TeX s
) = s
43 attr2latex
:: [(String, String)] -> String
44 attr2latex
(("command", c
) : as) = '\\' : (c
++ (attr2latex
as) ++ "{")
45 attr2latex
(("optarg", a
) : as) = '[' : (a
++ "]")
46 attr2latex
((_
, a
) : as) = '{' : (a
++ "}")
49 convert
:: String -> String
50 convert
= writeDoc
. convertAll
. readDoc
52 processFile
:: String -> IO ()
53 processFile fn
= catch (readFile fn
>>= ((writeFile $ newName fn
) . convert
))
54 (\_
-> hPutStr stderr ("Error processing: " ++ fn
))
55 where newName n
= (fst . break (== '.')) n
++ ".tex"
57 run
:: [String] -> IO ()