-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_table.hs
executable file
·82 lines (73 loc) · 3.33 KB
/
create_table.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env stack
-- stack script --resolver lts-21.17 --package bibtex --package parsec --package table-layout
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad (forM_)
import Data.List (intercalate, sortBy)
import Data.Maybe (fromMaybe)
import Data.Ord (Down (..))
import System.IO (hPutStrLn, hPutStr, IOMode(..), withFile)
import Text.BibTeX.Entry (T (..))
import Text.BibTeX.Parse (file)
import Text.Parsec.String (parseFromFile)
data ArchiveStatus = ArchiveAvailable | ArchiveInProcess | ArchiveUnavailable deriving (Show)
showStatus :: ArchiveStatus -> String
showStatus ArchiveAvailable = "available"
showStatus ArchiveUnavailable = "unavailable"
showStatus ArchiveInProcess = "inProcess"
readStatus :: String -> ArchiveStatus
readStatus "available" = ArchiveAvailable
readStatus "unavailable" = ArchiveUnavailable
readStatus "inProcess" = ArchiveInProcess
readStatus s = error $ "cannot read " ++ s
data TableRow = TableRow {
trBibKey :: String,
trMaybeFirstAuthor :: Maybe String,
trMaybeYear :: Maybe String,
trMaybeMonth :: Maybe String,
trStatusPCA :: ArchiveStatus,
trStatusPAA :: ArchiveStatus,
trStatusPMA :: ArchiveStatus
} deriving (Show)
main :: IO ()
main = do
Right bibEntries <- parseFromFile file "bibliography.bib"
statusTable <- readStatusTable "publication_list.tsv"
let rows = do
(key, (statusPCA, statusPAA, statusPMA)) <- statusTable
let row = case findBibKey key bibEntries of
Nothing -> TableRow key Nothing Nothing Nothing statusPCA statusPAA statusPMA
Just bibEntry ->
let bibFields = fields bibEntry
year = "year" `lookup` bibFields
month = "month" `lookup` bibFields
author = Nothing
in TableRow key author year month statusPCA statusPAA statusPMA
return row
let tableBody = do
TableRow k a y m s1 s2 s3 <- rows
let aStr = fromMaybe "n/a" a
yStr = fromMaybe "n/a" y
mStr = fromMaybe "n/a" m
dateStr = yStr ++ if mStr /= "n/a" then "-" ++ mStr else ""
return [k, aStr, dateStr, showStatus s1, showStatus s2, showStatus s3]
withFile "publication_table.md" WriteMode $ \h -> do
hPutStr h "| "
hPutStr h $ intercalate " | " ["Key", "First Author", "Date", "Status PCA", "Status PAA", "Status PMA"]
hPutStrLn h " |"
hPutStrLn h "|---|---|---|---|---|---|"
forM_ tableBody $ \r -> do
hPutStr h "| "
hPutStr h $ intercalate " | " r
hPutStrLn h "| "
hPutStrLn h "|---|---|---|---|---|---|"
readStatusTable :: FilePath -> IO [(String, (ArchiveStatus, ArchiveStatus, ArchiveStatus))]
readStatusTable fn = map processLine . tail . lines <$> readFile fn
where
processLine line =
let w = words line
key = head w
[s1, s2, s3] = map readStatus . tail $ w
in (key, (s1, s2, s3))
findBibKey :: String -> [T] -> Maybe T
findBibKey _ [] = Nothing
findBibKey key (entry:restEntries) = if key == identifier entry then Just entry else findBibKey key restEntries