packages feed

iso3166-country-codes-0.1: Data/ISO3166_CountryCodes/GenerateDataDeclaration.hs

{- | generate language code data type from the official list
     downloaded from <http://www.iso.org/iso/iso3166_en_code_lists.txt>

     use the text version because it's easy to parse and the
     xml version doesn't add anything, so I can avoid
     depending on an xml library for the moment

     The code_lists file contains non-ASCII characters,
     output (in the country names) and Haskell source is Unicode
     the output here requires encoding to UTF-8, something that
     Haskell IO ought to do itself.

-}
module Main where
import Getter
import Data.Char
import Codec.Binary.UTF8.String
import Data.List

main
  = do output_preamble
       generate_country_codes

output_preamble
  = readFile preamble >>= putStr

generate_country_codes
    = do text <- fmap lines $
                 get_country_code_list
         let codes = [(cc,name) |
                      l <- text,
                      let cc = filter (isAlpha) $
                               dropWhile (==';') $
                               dropWhile (/=';') $
                               l
                          name = takeWhile (/=';') l,
                      length cc == 2
                     ]
         gen_data_decl codes
         gen_alist_decl codes

gen_data_decl codes
    = do putStrLn "data CountryCode = "
         sequence_ $ fmap putStrLn $ intersperse "   |" $ fmap country_alternative codes
         putStrLn "   deriving (P.Eq,P.Read,P.Show,P.Enum,P.Bounded,P.Ord)"

country_alternative (code, name)
  = "   "++code++" -- ^ " ++ encodeString name

gen_alist_decl codes
    = do mapM_ putStrLn $
           ["",
             "{-|",
             "  convert a country code to the official (English) name of the country",
             "",
             "   see @'readableCountryName'@ for something with a more pleasing word order and capitalisation",
             "-}",
             "countryNameFromCode:: CountryCode -> String"
           ]
         sequence_ $ fmap make_clause codes

make_clause (constr, countryName)
    = putStrLn $ "countryNameFromCode " ++ constr ++ " = \"" ++ encodeString countryName ++ "\""

preamble = "Data/ISO3166_CountryCodes.preamble.hs"
{- Copyright © 2010 Jón Fairbairn
-}