<commit_before>{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
{-# OPTIONS -Wall -fno-warn-missing-signatures #-}
import Language.LaTeX
import qualified Language.LaTeX.Builder as B
import qualified Language.LaTeX.Builder.Babel as B
import qualified Language.LaTeX.Length as L
import qualified Language.LaTeX.Builder.Internal as BI
-- import qualified Language.LaTeX.Builder.Math as M
-- import qualified Language.LaTeX.Builder.Graphics as G
-- import qualified Language.LaTeX.Builder.Rotating as R
-- import qualified Language.LaTeX.Builder.Color as C

-- import Data.Ratio ((%))
-- import Data.Char
import Data.List.Split
import Data.List
import Data.String
import Data.Maybe
import Data.Foldable (foldMap)
import Data.Monoid.Unicode
import Text.CSV (parseCSV, CSV, Record, Field)
import System.Environment
import qualified Safe
import System.IO (stdout,hSetEncoding,utf8)

mainArgs [] = do hSetEncoding stdout utf8
                 putStr . fromAddrs =<< getContents
  where fromAddrs = either error id           . showLaTeX    -- show
                                              . body         -- LaTeX it
                  . fromMaybe []              . addrFromCSV  -- load records
                  . either (error . show) id  . parseCSV "-" -- parse CSV
mainArgs _ = error "Usage: csv-addresses-to-pdf"

main = mainArgs =<< getArgs

-- paraNoindent = B.para . (B.noindent ⊕)

preamb = B.useBabel B.francais []
       ⊕ BI.usepackage ["utf8"] (BI.pkgName "inputenc")

data Addr = Addr { name    :: String
                 , street  :: String
                 , zipcode :: String
                 , city    :: String
                 , country :: Maybe String
                 }

texLines = mconcat . intersperse B.newline . filter (/= mempty)

texAddr :: Addr -> ParItem
texAddr Addr{..} =
  B.para (texLines
   [fromString name
   ,fromString street
   ,zc
   ,foldMap fromString country])
  ⊕ B.vspace (L.em 0.6)
  where zc = if null zipcode then fromString city
             else if null city then fromString zipcode
             else fromString zipcode ⊕ B.space ⊕ fromString city

texAddrs :: [Addr] -> ParItem
texAddrs = foldMap texAddrPage . chunk 20 -- . prolongateByMod def 2
  where
    -- def = Addr "" "" "" "" Nothing
    cols = (2 :: Int)
    wi = L.linewidth / fromIntegral cols
    texAddrPage addrs =
      B.tabular (replicate cols B.l) (
          intersperse (B.cells (replicate cols "")) .
          map (B.cells . texAddrRow) $ chunk cols addrs)
      ⊕
      B.newpage

    texAddrRow a =
      take cols (map (B.minipage wi . texAddr) a
                 ++ repeat mempty)

{-
-- think more about this
foldl'Append :: (a -> b -> a) -> a -> (a -> [b]) -> [b] -> [b]
foldl'Append _ z k []     = k z
foldl'Append f z k (x:xs) = z `seq` (x : foldl'Append f (f z x) k xs)

prolongateByMod :: a -> Int -> [a] -> [a]<commit_msg>Highlight missing fields in red.<commit_after>{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
{-# OPTIONS -Wall -fno-warn-missing-signatures #-}
import Language.LaTeX
import qualified Language.LaTeX.Builder as B
import qualified Language.LaTeX.Builder.Babel as B
import qualified Language.LaTeX.Length as L
import qualified Language.LaTeX.Builder.Internal as BI
-- import qualified Language.LaTeX.Builder.Math as M
-- import qualified Language.LaTeX.Builder.Graphics as G
-- import qualified Language.LaTeX.Builder.Rotating as R
import qualified Language.LaTeX.Builder.Color as C

-- import Data.Ratio ((%))
import Data.Char
import Data.List.Split
import Data.List
import Data.String
import Data.Maybe
import Data.Foldable (foldMap)
import Data.Monoid.Unicode
import Text.CSV (parseCSV, CSV, Record, Field)
import System.Environment
import qualified Safe
import System.IO (stdout,hSetEncoding,utf8)

mainArgs [] = do hSetEncoding stdout utf8
                 putStr . fromAddrs =<< getContents
  where fromAddrs = either error id           . showLaTeX    -- show
                                              . body         -- LaTeX it
                  . fromMaybe []              . addrFromCSV  -- load records
                  . either (error . show) id  . parseCSV "-" -- parse CSV
mainArgs _ = error "Usage: csv-addresses-to-pdf"

main = mainArgs =<< getArgs

-- paraNoindent = B.para . (B.noindent ⊕)

preamb = B.useBabel B.francais []
       ⊕ BI.usepackage ["utf8"] (BI.pkgName "inputenc")

data Addr = Addr { name    :: String
                 , street  :: String
                 , zipcode :: String
                 , city    :: String
                 , country :: Maybe String
                 }

texLines = mconcat . intersperse B.newline . filter (/= mempty)

texAddr :: Addr -> ParItem
texAddr Addr{..} =
  B.para (texLines
   [ck "Nom" name
   ,ck "Adresse" street
   ,ck "Code Postal" zipcode ⊕ B.space ⊕ ck "Ville" city
   ,foldMap fromString country])
  ⊕ B.vspace (L.em 0.6)
  where ck nam x
         | all isSpace x = C.textcolor C.red (fromString nam)
         | otherwise     = fromString x


texAddrs :: [Addr] -> ParItem
texAddrs = foldMap texAddrPage . chunk 20 -- . prolongateByMod def 2
  where
    -- def = Addr "" "" "" "" Nothing
    cols = (2 :: Int)
    wi = L.linewidth / fromIntegral cols
    texAddrPage addrs =
      B.tabular (replicate cols B.l) (
          intersperse (B.cells (replicate cols "")) .
          map (B.cells . texAddrRow) $ chunk cols addrs)
      ⊕
      B.newpage

    texAddrRow a =
      take cols (map (B.minipage wi . texAddr) a
                 ++ repeat mempty)

{-
-- think more about this
foldl'Append :: (a -> b -> a) -> a -> (a -> [b]) -> [b] -> [b]
foldl'Append _ z k []     = k z
foldl'Append f z k (x:xs) = z `seq` (x : foldl'Append f (f z x) k xs)

prolongateByMod :: a -> Int -> [a] -> [a]
{'fraction_non_alphanumeric': 0.11532413070068145, 'fraction_numerical': 0.0031452035645640397, 'mean_word_length': 2.219347581552306, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5264331', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1796, 'n_words': 563}