https://wiki.haskell.org/index.php?title=Haskell_Quiz/GEDCOM/Solution_Abhinav&feed=atom&action=history
Haskell Quiz/GEDCOM/Solution Abhinav - Revision history
2024-03-19T11:07:33Z
Revision history for this page on the wiki
MediaWiki 1.35.5
https://wiki.haskell.org/index.php?title=Haskell_Quiz/GEDCOM/Solution_Abhinav&diff=50000&oldid=prev
Abhinav.sarkar: added category:code
2012-08-23T14:34:27Z
<p>added category:code</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 14:34, 23 August 2012</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 114:</td>
<td colspan="2" class="diff-lineno">Line 114:</td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Source: https://github.com/abhin4v/rubyquiz/blob/master/GedcomParser.hs</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Source: https://github.com/abhin4v/rubyquiz/blob/master/GedcomParser.hs</div></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>[Category:Code]]</div></td>
</tr>
</table>
Abhinav.sarkar
https://wiki.haskell.org/index.php?title=Haskell_Quiz/GEDCOM/Solution_Abhinav&diff=47898&oldid=prev
Abhinav.sarkar at 16:58, 4 August 2012
2012-08-04T16:58:00Z
<p></p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 16:58, 4 August 2012</td>
</tr><tr>
<td colspan="2" class="diff-lineno">Line 4:</td>
<td colspan="2" class="diff-lineno">Line 4:</td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- Example GEDCOM document at</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- Example GEDCOM document at</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- http://cpansearch.perl.org/src/PJCJ/Gedcom-1.16/royal.ged</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>-- http://cpansearch.perl.org/src/PJCJ/Gedcom-1.16/royal.ged</div></td>
</tr>
<tr>
<td colspan="2" class="diff-empty"> </td>
<td class="diff-marker">+</td>
<td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>-- Copyright (C) 2012 Abhinav Sarkar</div></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td>
</tr>
<tr>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>{-# LANGUAGE NoMonomorphismRestriction, RecordWildCards #-}</div></td>
<td class="diff-marker"> </td>
<td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>{-# LANGUAGE NoMonomorphismRestriction, RecordWildCards #-}</div></td>
</tr>
</table>
Abhinav.sarkar
https://wiki.haskell.org/index.php?title=Haskell_Quiz/GEDCOM/Solution_Abhinav&diff=47895&oldid=prev
Abhinav.sarkar: New page: <haskell> -- A GEDCOM to XML converter written using Parsec as a -- solution for rubyquiz 6 (http://rubyquiz.com/quiz6.html). -- Example GEDCOM document at -- http://cpansearch.perl.org/sr...
2012-08-04T16:55:35Z
<p>New page: <haskell> -- A GEDCOM to XML converter written using Parsec as a -- solution for rubyquiz 6 (http://rubyquiz.com/quiz6.html). -- Example GEDCOM document at -- http://cpansearch.perl.org/sr...</p>
<p><b>New page</b></p><div><haskell><br />
-- A GEDCOM to XML converter written using Parsec as a<br />
-- solution for rubyquiz 6 (http://rubyquiz.com/quiz6.html).<br />
-- Example GEDCOM document at<br />
-- http://cpansearch.perl.org/src/PJCJ/Gedcom-1.16/royal.ged<br />
<br />
{-# LANGUAGE NoMonomorphismRestriction, RecordWildCards #-}<br />
<br />
module GedcomParser where<br />
<br />
import Text.Parsec hiding (spaces, Line)<br />
import System.IO<br />
<br />
-- a line in a GEDCOM document<br />
data Line = Line {<br />
lineLevel :: Int,<br />
lineTag :: String,<br />
lineValue :: Maybe String,<br />
lineId :: Maybe String<br />
} deriving (Show)<br />
<br />
-- an element in a GEDCOM document<br />
data Elem = Elem {<br />
elemTag :: String,<br />
elemValue :: Maybe String,<br />
elemId :: Maybe String,<br />
elemChildren :: [Elem]<br />
} deriving (Show)<br />
<br />
indent n = concat . (replicate n) $ " "<br />
<br />
trimValue value = case value of<br />
Nothing -> Nothing<br />
Just v<br />
| v == "" -> Nothing<br />
| otherwise -> Just v<br />
<br />
normalizeValue = maybe "" id<br />
<br />
spaces = many (char ' ' <|> tab)<br />
whitespaces = many (char ' ' <|> tab <|> newline)<br />
<br />
-- parses a line<br />
line level = do<br />
string (show level)<br />
spaces<br />
id <- optionMaybe $ between (char '@') (char '@') (many1 alphaNum)<br />
spaces<br />
tag <- many1 upper<br />
spaces<br />
value <- fmap trimValue $ optionMaybe $ manyTill (anyChar) newline<br />
return $ Line level tag value id<br />
<br />
-- parses an element<br />
element level = do<br />
ml <- optionMaybe $ line level<br />
case ml of<br />
Nothing -> fail ("invalid level " ++ show level)<br />
Just Line{..} -> do<br />
children <- many (element $ level + 1)<br />
return $ Elem lineTag lineValue lineId children<br />
<br />
-- parses a document<br />
document = (element 0) `endBy` whitespaces<br />
<br />
-- normalizes an element by merging values of CONC and CONT<br />
-- elements with parent element value<br />
normalizeElem element =<br />
let<br />
conChildren = filter concOrCont $ elemChildren element<br />
text = foldl (\t el -> t<br />
++ (if elemTag el == "CONC" then "\n" else " ")<br />
++ normalizeValue (elemValue el))<br />
"" conChildren<br />
nonConChildren = filter (not . concOrCont) $ elemChildren element<br />
in<br />
element { elemValue = trimValue $<br />
Just (normalizeValue (elemValue element) ++ text),<br />
elemChildren = map normalizeElem nonConChildren }<br />
where<br />
concOrCont el = elemTag el `elem` ["CONC", "CONT"]<br />
<br />
-- normalizes a document<br />
normalizeDoc = map normalizeElem<br />
<br />
-- converts an element to XML<br />
elemToXml indentation Elem{..} =<br />
indent indentation<br />
++ "<" ++ elemTag<br />
++ maybe "" (\i -> " id=\"@" ++ i ++ "@\"") elemId<br />
++ case elemChildren of<br />
[] -> ">" ++ normalizeValue elemValue ++ "</" ++ elemTag ++ ">"<br />
_ -> maybe "" (\v -> " value=\"" ++ v ++ "\"") elemValue ++ ">\n"<br />
++ unlines (map (elemToXml (indentation + 1)) elemChildren)<br />
++ indent indentation ++ "</" ++ elemTag ++ ">"<br />
<br />
-- converts a document to XML<br />
documentToXml doc = "<DOCUMENT>\n"<br />
++ (unlines . map (elemToXml 1) $ doc)<br />
++ "</DOCUMENT>"<br />
<br />
-- converts a GEDCOM document supplied through STDIN into XML<br />
-- and prints to STDOUT<br />
main = do<br />
text <- getContents<br />
case parse document "GEDCOM Parser" text of<br />
Right [] -> return ()<br />
Right doc -> putStrLn $ documentToXml (normalizeDoc doc)<br />
Left e -> print e<br />
</haskell><br />
<br />
'''Description:''' This solution uses Parsec to parse a GEDCOM file and then converts the parsed data to XML.<br />
<br />
Source: https://github.com/abhin4v/rubyquiz/blob/master/GedcomParser.hs</div>
Abhinav.sarkar