--convierte una lista en un string
unwords1::[String]->String
unwords1 [] = []
unwords1 (x:xs) = x ++ "\n" ++ unwords1 xs
borrarcomentarios::String->String
borrarcomentarios [] = []
borrarcomentarios [x] = [x]
borrarcomentarios (x:y:ys)
|x == '\\' && y == '%' = y:borrarcomentarios(ys)
|x == '\\' && y == '\\' = y:borrarcomentarios(ys)
-- |x == '\\' && y == '#' = y:borrarcomentarios(ys)
-- |x == '\\' && y == '$' = y:borrarcomentarios(ys)
-- |x == '\\' && y == '_' = y:borrarcomentarios(ys)
-- |x == '\\' && y == '&' = y:borrarcomentarios(ys)
-- |x == '\\' && y == '{' = y:borrarcomentarios(ys)
-- |x == '\\' && y == '}' = y:borrarcomentarios(ys)
|x== '%' = []
|otherwise =x:borrarcomentarios(y:ys)
findbegin::String->Bool
findbegin x
|x == "\\begin{document}" = True
|otherwise = False
auxhead::[String]->[String]
auxhead [] = []
auxhead (x:xs)
| findbegin(x) == False = x:auxhead(xs)
| otherwise = []
auxbody::[String]->[String]
auxbody [] = []
auxbody (x:xs)
| findbegin(x) == False = auxbody(xs)
| otherwise = xs
divibuffer::[String]->[String]
divibuffer x = [(unwords1 (auxhead x)), (unwords1 (auxbody x))]
htmlhead:: String-> String
htmlhead titulo = "\n
\n\n" ++
"" ++ titulo ++ "<\\title>\n <\\head>"
armartitulo:: String -> String -> String -> String
armartitulo titulo autor fecha = "" ++ titulo ++ "<\\h1>\n" ++
"" ++ autor ++ "<\\h2>\n" ++
"" ++ fecha ++ "<\\h2>\n"
--creamos variables de los comandos de la cabeza
-- en caso que no haya fecha definida hay que sacar la fecha del sistema
--guarda la cabeza en un arreglo dividiendo por \
slashsplit::String->[String]
slashsplit [] = []
slashsplit (x:xs)
|x == '\\' = (fst(break('\\'==) (xs))):slashsplit(snd(break('\\'==) (xs)))
|otherwise = []
-- ya se encuentra un \ se llama a esta funcion para que devuelva el nombre del comando
comando::String->String
comando [] = []
comando (x:xs)
|x == '{' = []
|x == '[' = []
|x == ' ' = []
|x == '\t' = []
|otherwise = x:comando(xs)
parametro::String->String
parametro [] = []
parametro (x:xs)
|x == '{' = tail( fst( break('}'==) (snd (break('{'==) (x:xs)))))
|x == '[' = tail( fst( break(']'==) (snd (break('['==) (x:xs)))))
|otherwise = parametro(xs)
gettitle::[String]->String
gettitle [] = []
gettitle (x:xs) = if comando(x) == "title" then parametro(x) else gettitle(xs)
getauthor::[String]->String
getauthor [] = []
getauthor (x:xs) = if comando(x) == "author" then parametro(x) else gettitle(xs)
getdate::[String]->String
getdate [] = []
getdate (x:xs) = if comando(x) == "date" then parametro(x) else gettitle(xs)
-- body
-- .--
letra::String->String
letra [] = []
letra (x:xs)
| x=='{' = []
| x=='}' = []
| x=='\\' = []
| x=='[' = []
| x==']' = []
| otherwise = x:(letra xs)
splitter_line::String->[String]
splitter_line [] = []
splitter_line (x:xs)
| x == '{' = "OPBRACE":(splitter_line xs)
| x == '}' = "CLBRACE":(splitter_line xs)
| x == '\\'= "COM":(comando xs):(splitter_line (drop (length (comando xs)) xs))
| otherwise = (letra (x:xs)):(splitter_line (drop(length(letra xs))xs))
splitter::[String]->[String]
splitter [] = []
splitter (x:xs) = (splitter_line x)++["SALTO",[]]++(splitter xs)
parsebody::[String]->String->String
parsebody (x:xs) make = unwords1 (splitter (x:xs))
-- |x == "\maketitle" = make ++ parsebody(xs)
-- |x == ""
-- .------
-- . MAIN
-- .------
execute::IO()
execute = do
-- leemos el archivo a convertir
putStr "ingrese el nombre del archivo a leer "
archivo<-getLine
texto<-readFile archivo
-- convertimos el archivo a una lista de lineas y le
-- quitamos los comentarios
let buffer = map borrarcomentarios(lines texto)
bufflista = divibuffer buffer
lista = slashsplit(head bufflista)
titulo = gettitle(lista)
autor = getauthor(lista)
fecha = getdate(lista)
maketitle = armartitulo titulo autor fecha
body = parsebody (tail bufflista) maketitle
in do
-- putStr (htmlhead titulo)
putStr body
-- putStr(unwords1 buffer)
-- putStr "cabeza \n"
-- putStr (head bufflista)
-- putStr "cuerpo \n"
-- putStr (unwords1 (tail bufflista))
-- mostramos el archivo sin comentarios
-- partimos en head y title