Show available definitions
This commit is contained in:
parent
e0f75fe5b8
commit
5a02d92848
53
ParseHtml.hs
53
ParseHtml.hs
@ -1,17 +1,52 @@
|
||||
module ParseHtml (printDifferentDef) where
|
||||
module ParseHtml (printDifferentDef, differentDefTags, parseDefTags) where
|
||||
|
||||
import Text.HTML.TagSoup
|
||||
|
||||
printDifferentDef :: String -> IO ()
|
||||
printDifferentDef x = putStrLn (getDifferentDefMessages x)
|
||||
data WordDefHeader = WordDefHeader WordName WordType
|
||||
data WordName = WordName String
|
||||
data WordType = WordType String
|
||||
|
||||
getDifferentDefMessages :: String -> String
|
||||
getDifferentDefMessages x = renderTags (differentDefTags x)
|
||||
printDifferentDef :: String -> IO ()
|
||||
printDifferentDef x = putStrLn (getDifferentDef x)
|
||||
|
||||
|
||||
getDifferentDef :: String -> String
|
||||
getDifferentDef x = getDifferentDefMessages (length xs) ++ renderWordList xs
|
||||
where xs = (parseDefTags . differentDefTags) x
|
||||
|
||||
renderWordList :: [WordDefHeader] -> String
|
||||
renderWordList [] = ""
|
||||
renderWordList (x:xs) = (renderString x) ++ "\n" ++ (renderWordList xs)
|
||||
|
||||
getDifferentDefMessages :: Int -> String
|
||||
getDifferentDefMessages 0 = "Aucune définition disponible."
|
||||
getDifferentDefMessages 1 = "Une définition disponible : \n"
|
||||
getDifferentDefMessages _ = "Plusieurs définitions disponibles : \n"
|
||||
|
||||
renderString :: WordDefHeader -> String
|
||||
renderString (WordDefHeader n t) = (getNameString n) ++ (getTypeString t)
|
||||
|
||||
getNameString :: WordName -> String
|
||||
getNameString (WordName s) = s
|
||||
|
||||
getTypeString :: WordType -> String
|
||||
getTypeString (WordType s) = s
|
||||
|
||||
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 :: String -> [Tag String]
|
||||
differentDefTags =
|
||||
differentDefTags =
|
||||
filter (~== TagText "") . (takeWhile (~/= "<div id=contentbox>"))
|
||||
. (dropWhile (~/= "<div id=vtoolbar>")) . parseTags
|
||||
|
||||
numberOfDef :: [Tag String] -> Int
|
||||
numberOfDef tags = ((length tags) + 1) `div` 3
|
||||
|
Loading…
Reference in New Issue
Block a user