dictfr/ParseHtml.hs

54 lines
1.8 KiB
Haskell

module ParseHtml (printDef, printDifferentDef) where
import Text.HTML.TagSoup
import Prelude
import WordDef
import qualified Data.Text.Lazy as T
printDifferentDef :: T.Text -> IO ()
printDifferentDef x = putStrLn (getDifferentDef x)
getDifferentDef :: T.Text -> String
getDifferentDef x = getDifferentDefMessages (length xs) ++ renderWordList xs
where xs = (parseDefTags . differentDefTags) x
getDifferentDefMessages :: Int -> String
getDifferentDefMessages 0 = "Aucune définition disponible."
getDifferentDefMessages 1 = "Une définition disponible : \n"
getDifferentDefMessages _ = "Plusieurs définitions disponibles : \n"
parseDefTags :: [Tag String] -> [WordDefHeader]
parseDefTags [] = []
parseDefTags xs =
(WordDefHeader (WordName (renderTags (take 1 (fst st))))
(WordType (renderTags [(fst st) !! 1])))
: (parseDefTags (snd st))
where st = subTags xs
subTags :: [Tag String] -> ([Tag String],[Tag String])
subTags [] = ([],[])
subTags xs = if (length xs) `mod` 3 /= 0
then splitAt 2 xs
else ((take 1 xs) ++ (take 1 (drop 2 xs)) , drop 3 xs)
differentDefTags :: T.Text -> [Tag String]
differentDefTags =
filter (~== TagText "") . (takeWhile (~/= "<div id=contentbox>"))
. (dropWhile (~/= "<div id=vtoolbar>")) . parseTags . T.unpack
printDef :: T.Text -> IO ()
printDef = putStrLn . getDef
getDef :: T.Text -> String
getDef = (renderFullDef . getFullDef . parseFullDefTags)
getFullDef :: [Tag String] -> WordFullDef
getFullDef x = WordFullDef (WordDefHeader (WordName "lol") (WordType
"Interjection")) [WordDefSentence "Laughing Out Loud",
WordExampleSentence "ur wrong lol"]
parseFullDefTags :: T.Text -> [Tag String]
parseFullDefTags = filter (~== "<span class=tlf_cdefinition") . parseTags
. T.unpack