packages feed

gridtables 0.0.3.0 → 0.1.0.0

raw patch · 7 files changed

+120/−9 lines, 7 filesdep ~textPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: text

API changes (from Hackage documentation)

+ Text.GridTable.ArrayTable: [arrayTableFoot] :: ArrayTable a -> Maybe RowIndex
- Text.GridTable.ArrayTable: ArrayTable :: Array CellIndex (GridCell a) -> Maybe RowIndex -> Array ColIndex (Alignment, Int) -> ArrayTable a
+ Text.GridTable.ArrayTable: ArrayTable :: Array CellIndex (GridCell a) -> Maybe RowIndex -> Maybe RowIndex -> Array ColIndex (Alignment, Int) -> ArrayTable a

Files

CHANGELOG.md view
@@ -2,6 +2,12 @@  `gridtables` uses [PVP Versioning][]. +## gridtables-0.1.0.0++Release pending.++-   Added support for table foots.+ ## gridtables-0.0.3.0  Released 2022-08-18.
LICENSE view
@@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 Albert Krewinkel+Copyright © 2022 RStudio, PBC  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
README.md view
@@ -22,7 +22,8 @@  The tables are intended to look good when viewed in a monospace font. Therefore, wide and full-width characters, as those in East-Asian scripts, are counted as two characters.+Asian scripts, are counted as two characters, while zero-width and+combining characters are treated as if they have no width.  ## Column alignments @@ -44,6 +45,29 @@     +------+--------+-------+     | a 1  | b 2    | c 3   |     +------+--------+-------+++## Table Foot++This library implements an extension that enables to create tables+with table foots: If the *last* separator line is a part+separator, i.e., if it consists of `=` instead of `-`, then all+rows after the *second-to-last* part separator are treated as the+table foot.++E.g., consider the following table:++    +------+-------++    | Item | Price |+    +======+=======++    | Eggs | 5£    |+    +------+-------++    | Spam | 3£    |+    +======+=======++    | Sum  | 8£    |+    +======+=======+++Here, the last row, containing "Sum" and "8£", would be the table+foot.   ## Algorithm
gridtables.cabal view
@@ -1,6 +1,6 @@ cabal-version:       2.4 name:                gridtables-version:             0.0.3.0+version:             0.1.0.0 synopsis:            Parser for reStructuredText-style grid tables. description:         Provides a parser for plain-text representations of                      tables. This package supports table headers, cells@@ -12,7 +12,7 @@ license-file:        LICENSE author:              Albert Krewinkel maintainer:          Albert Krewinkel <albert@zeitkraut.de>-copyright:           © 2022 Albert Krewinkel+copyright:           © 2022 RStudio, PBC category:            Text extra-doc-files:     README.md                    , CHANGELOG.md
src/Text/GridTable/ArrayTable.hs view
@@ -3,7 +3,7 @@ {-# LANGUAGE LambdaCase                 #-} {- | Module      : Text.GridTable.ArrayTable-Copyright   : © 2022 Albert Krewinkel+Copyright   : © 2022 RStudio, PBC License     : MIT Maintainer  : Albert Krewinkel <albert@zeitkraut.de> @@ -32,6 +32,7 @@ data ArrayTable a = ArrayTable   { arrayTableCells    :: Array CellIndex (GridCell a)   , arrayTableHead     :: Maybe RowIndex+  , arrayTableFoot     :: Maybe RowIndex   , arrayTableColSpecs :: Array ColIndex (Alignment, Int)   }   deriving stock (Eq, Show)
src/Text/GridTable/Trace.hs view
@@ -43,6 +43,9 @@   let charGrid = toCharGrid lines       specs1   = colSpecsInLine '-' charGrid 1       partSeps = findSeparators charGrid+      -- The first separator can never be a part separator line (with+      -- =), but it can contain column alignment markers, so it is+      -- always normalized it as well.       charGrid' = convertToNormalLines (1:map partSepLine partSeps) charGrid       traceInfo = traceCharGrid charGrid' initialTraceInfo   in if Set.null (gridCells traceInfo)@@ -416,13 +419,19 @@                    ++ repeat AlignDefault)                  (map fromCharCol colwidths)       lastCol = ColIndex (length colwidths)-      tableHead = subtract 1 <$>-                  foldr ((<|>) . (`Map.lookup` rowindex) . partSepLine)-                        Nothing-                        partSeps+      mlastLine = Set.lookupMax (gridRowSeps traceInfo)+      tableHead = case partSeps of+        ps:_   -> subtract 1 <$> partSepLine ps `Map.lookup` rowindex+        []     -> Nothing+      tableFoot = case reverse partSeps of+        rps:rps':_ | Just (partSepLine rps) == mlastLine ->+          partSepLine rps' `Map.lookup` rowindex+        _ ->+          Nothing   in ArrayTable      { arrayTableCells = runSTArray (toMutableArray traceInfo rowindex colindex)      , arrayTableHead = tableHead+     , arrayTableFoot = tableFoot      , arrayTableColSpecs = listArray (1, lastCol) colSpecs      } 
test/test-gridtables.hs view
@@ -59,6 +59,7 @@        Right (ArrayTable               { arrayTableCells = listArray gbounds [ContentCell 1 1 [" one "]]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [5]               }) @@ -78,6 +79,7 @@                                  , ContentCell 1 1 [" two "]                                  ]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [5, 5]               }) @@ -98,6 +100,7 @@                                  , ContentCell 1 1 [" fish "]                                  ]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [4, 6]               }) @@ -119,6 +122,7 @@                                  , ContentCell 1 1 [" two "]                                  ]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [5]               }) @@ -142,6 +146,7 @@                 , ContentCell 1 1 [" three "]                 ]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [5, 7]               }) @@ -166,6 +171,7 @@                   , ContentCell 1 1 ["  2  "]                   ]                 , arrayTableHead = Just 1+                , arrayTableFoot = Nothing                 , arrayTableColSpecs = defaultAlign [5, 5]                 }) @@ -188,6 +194,7 @@                   , ContentCell 1 1 [" 3     "]                   ]                 , arrayTableHead = Just 1+                , arrayTableFoot = Nothing                 , arrayTableColSpecs = listArray (1, 3)                                        [ (AlignLeft, 6)                                        , (AlignCenter, 8)@@ -195,7 +202,58 @@                                        ]                 })     ]+  , testGroup "table foot"+    [ testCase "simple foot" $+      let gt = T.unlines+               [ "+------+-------+"+               , "| Item | Price |"+               , "+======+=======+"+               , "| Eggs | 5£    |"+               , "+------+-------+"+               , "| Spam | 3£    |"+               , "+======+=======+"+               , "| Sum  | 8£    |"+               , "+======+=======+"+               ]+      in parse' gridTable gt @?=+         Right (ArrayTable+                { arrayTableCells = listArray ((1,1), (4, 2))+                  [ ContentCell 1 1 [" Item "]+                  , ContentCell 1 1 [" Price "]+                  , ContentCell 1 1 [" Eggs "]+                  , ContentCell 1 1 [" 5£    "]+                  , ContentCell 1 1 [" Spam "]+                  , ContentCell 1 1 [" 3£    "]+                  , ContentCell 1 1 [" Sum  "]+                  , ContentCell 1 1 [" 8£    "]+                  ]+                , arrayTableHead = Just 1+                , arrayTableFoot = Just 4+                , arrayTableColSpecs = defaultAlign [6, 7]+                }) +    , testCase "table without body" $+      let gt = T.unlines+               [ "+------+-------+"+               , "| Item | Price |"+               , "+======+=======+"+               , "| Sum  | 8£    |"+               , "+======+=======+"+               ]+      in parse' gridTable gt @?=+         Right (ArrayTable+                { arrayTableCells = listArray ((1,1), (2, 2))+                  [ ContentCell 1 1 [" Item "]+                  , ContentCell 1 1 [" Price "]+                  , ContentCell 1 1 [" Sum  "]+                  , ContentCell 1 1 [" 8£    "]+                  ]+                , arrayTableHead = Just 1+                , arrayTableFoot = Just 2+                , arrayTableColSpecs = defaultAlign [6, 7]+                })+    ]+   , testCase "marker in first line" $     let gt = T.unlines              [ "+:-----+:------:+------:+"@@ -215,6 +273,7 @@                 , ContentCell 1 1 [" c 3   "]                 ]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = listArray (1, 3)                                      [ (AlignLeft, 6)                                      , (AlignCenter, 8)@@ -234,6 +293,7 @@                 { arrayTableCells = listArray ((1,1), (1, 2))                   [ ContentCell 1 1 ["魚"] , ContentCell 1 1 [" x "]]                 , arrayTableHead = Nothing+                , arrayTableFoot = Nothing                 , arrayTableColSpecs = defaultAlign [2, 3]                 }) @@ -248,6 +308,7 @@                 { arrayTableCells = listArray ((1,1), (1, 2))                   [ ContentCell 1 1 ["x\8203y"] , ContentCell 1 1 [" z "]]                 , arrayTableHead = Nothing+                , arrayTableFoot = Nothing                 , arrayTableColSpecs = defaultAlign [2, 3]                 }) @@ -262,6 +323,7 @@                 { arrayTableCells = listArray ((1,1), (1, 2))                   [ ContentCell 1 1 ["魚\8203y"] , ContentCell 1 1 [" z "]]                 , arrayTableHead = Nothing+                , arrayTableFoot = Nothing                 , arrayTableColSpecs = defaultAlign [3, 3]                 }) @@ -276,6 +338,7 @@                 { arrayTableCells = listArray ((1,1), (1, 2))                   [ ContentCell 1 1 ["y\8203魚"] , ContentCell 1 1 [" z "]]                 , arrayTableHead = Nothing+                , arrayTableFoot = Nothing                 , arrayTableColSpecs = defaultAlign [3, 3]                 }) @@ -290,6 +353,7 @@                 { arrayTableCells = listArray ((1,1), (1, 2))                   [ ContentCell 1 1 ["a\8204\8205b"] , ContentCell 1 1 [" c "]]                 , arrayTableHead = Nothing+                , arrayTableFoot = Nothing                 , arrayTableColSpecs = defaultAlign [2, 3]                 }) @@ -304,6 +368,7 @@                 { arrayTableCells = listArray ((1,1), (1, 2))                   [ ContentCell 1 1 ["12345"] , ContentCell 1 1 ["a"]]                 , arrayTableHead = Nothing+                , arrayTableFoot = Nothing                 , arrayTableColSpecs = defaultAlign [10, 1]                 }) @@ -323,6 +388,7 @@               { arrayTableCells = listArray gbounds                                  [ ContentCell 1 1 [" one"]]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [5]               }) @@ -337,6 +403,7 @@               { arrayTableCells = listArray ((1,1), (1,1))                                  [ ContentCell 1 1 [" 1 "]]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [3]               }) @@ -356,6 +423,7 @@                                  , ContentCell 1 1 ["one"]                                  , ContentCell 1 1 ["two"]]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [3, 3]               }) @@ -380,6 +448,7 @@                                  , ContentCell 1 1 ["more text"]                                  ]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [4, 9]               }) @@ -399,6 +468,7 @@                                  , ContentCell 1 1 ["one"]                                  , ContentCell 1 1 ["two"]]               , arrayTableHead = Nothing+              , arrayTableFoot = Nothing               , arrayTableColSpecs = defaultAlign [3, 3]               }) @@ -433,6 +503,7 @@                  , ContentCell 1 1 "3"                  ]                , arrayTableHead = Nothing+               , arrayTableFoot = Nothing                , arrayTableColSpecs = defaultAlign [5, 7]                } :: ArrayTable Text       in rows gt @?= [ [Cell "1" 2 1, Cell "2" 1 1]