packages feed

pandoc-logic-proof (empty) → 0.2.0.4

raw patch · 5 files changed

+252/−0 lines, 5 filesdep +basedep +pandocdep +pandoc-logic-proof

Dependencies added: base, pandoc, pandoc-logic-proof, pandoc-types, text

Files

+ CHANGELOG.md view
@@ -0,0 +1,11 @@+# Changelog++0.2.0.4 Packaged as a Nix flake.++0.2.0.3 Improved error message, updated dependencies.++0.2.0.2 Reformatted source code.++0.2.0.0 Factored out transform into a separate library for use with Hakyll.++0.1.0.0 Initial release
+ LICENSE view
@@ -0,0 +1,29 @@+BSD 3-Clause License++Copyright (c) 2022-2023, Amy de Buitléir+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this+   list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice,+   this list of conditions and the following disclaimer in the documentation+   and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its+   contributors may be used to endorse or promote products derived from+   this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ app/Main.hs view
@@ -0,0 +1,17 @@+{-|+Module      : Main+Description : An easy way to create tables with wrapped text in Markdown.+Copyright   : (c) 2020-2023 Amy de Buitléir+License     : BSD--3+Maintainer  : amy@nualeargais.ie+Stability   : experimental+Portability : POSIX++See <https://github.com/mhwombat/pandoc-logic-proof> for information+on how to use this filter.+-}+import Text.Pandoc.Filters.LogicProof (formatProofs)+import Text.Pandoc.JSON               (toJSONFilter)++main :: IO ()+main = toJSONFilter formatProofs
+ pandoc-logic-proof.cabal view
@@ -0,0 +1,46 @@+cabal-version:   3.0+name:            pandoc-logic-proof+version:         0.2.0.4+synopsis:        A pandoc filter that provides a Markdown extension for logic proofs.+description:+  For more information and a tutorial on how to use this package,+  please see the README at <https://github.com/mhwombat/pandoc-logic-proof#readme>.+homepage:        https://github.com/mhwombat/pandoc-logic-proof+bug-reports:     https://github.com/mhwombat/pandoc-logic-proof/issues+license:         BSD-3-Clause+license-file:    LICENSE+author:          Amy de Buitléir+maintainer:      amy@nualeargais.ie+copyright:       (c) 2020-2023 Amy de Buitléir+category:        Text+build-type:      Simple+extra-doc-files: CHANGELOG.md++source-repository head+  type:              git+  location:          https://github.com/mhwombat/pandoc-logic-proof.git++common common-stuff+  default-language: Haskell2010+  default-extensions: ImportQualifiedPost+  build-depends:+    base >= 4.16.4 && < 4.17,+    pandoc-types >= 1.22.2 && < 1.23+  ghc-options: -Wall  -Wunused-packages++library+  import:           common-stuff+  hs-source-dirs:   src+  exposed-modules:+    Text.Pandoc.Filters.LogicProof+  build-depends:+    pandoc >= 2.19.2 && < 2.20,+    text >= 1.2.5 && < 1.3++executable pandoc-logic-proof+  import:           common-stuff+  hs-source-dirs:   app+  main-is:          Main.hs+  build-depends:+    pandoc-logic-proof+
+ src/Text/Pandoc/Filters/LogicProof.hs view
@@ -0,0 +1,149 @@+{-|+Module      : LogicProof+Description : Provides a way to write logic proofs in Markdown.+Copyright   : (c) 2020-2023 Amy de Buitléir+License     : BSD--3+Maintainer  : amy@nualeargais.ie+Stability   : experimental+Portability : POSIX++See <https://github.com/mhwombat/pandoc-logic-proof> for information+on how to use this filter.+-}++{-# LANGUAGE OverloadedStrings #-}++module Text.Pandoc.Filters.LogicProof+  (+    transform,+    formatProofs+  ) where++import Data.Foldable    (foldl')+import Data.Text        qualified as T+import Text.Pandoc      qualified as P+import Text.Pandoc.Walk (walk)+++-- | A transformation that can be used with Hakyll.+transform :: P.Pandoc -> P.Pandoc+transform = walk formatProofs++-- | Exported for use by the executable.+formatProofs :: P.Block -> P.Block+formatProofs x@(P.CodeBlock (_,cs,_) s)+  | null cs                = x+  | head cs == "logicproof" = proofToTable . renumber $ parseProof s+  | otherwise              = x+formatProofs x = x++type Proof = [ProofRow]++type ProofRow = [T.Text]++proofToTable :: Proof -> P.Block+proofToTable p = P.Table attr defaultTableCaption colSpecs+                    defaultTableHeader [toTableBody p]+                    defaultTableFooter+  where attr = ("",["logicproof"],[])+        colSpecs = replicate 3 defaultColSpec++toTableBody :: Proof -> P.TableBody+toTableBody p = P.TableBody P.nullAttr (P.RowHeadColumns 0) []+                  $  map toTableRow p++toTableRow :: ProofRow -> P.Row+toTableRow row+  | length row < 3 = error "short row in logic proof"+  | otherwise      = P.Row P.nullAttr cells+  where label = head row+        justification = last row+        statement = penultimate row+        depth = length row - 3+        statement' = indent depth statement+        cells = [+                  textToCell label,+                  blocksToCell . map removePara $ parseBlocks statement',+                  blocksToCell . map removePara $ parseBlocks justification+                ]++removePara :: P.Block -> P.Block+removePara (P.Para xs) = P.Plain xs+removePara x           = x++penultimate :: [a] -> a+penultimate = last . init++indent :: Int -> T.Text -> T.Text+indent 0 s = s+indent n s = filler `T.append` s+  where filler = T.pack . concat $ replicate n+                     "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"++blocksToCell :: [P.Block] -> P.Cell+blocksToCell+  = P.Cell P.nullAttr P.AlignDefault (P.RowSpan 1) (P.ColSpan 1)++textToCell :: T.Text -> P.Cell+textToCell t+  = P.Cell P.nullAttr P.AlignDefault (P.RowSpan 1) (P.ColSpan 1)+      [ P.Plain [P.Str t] ]++parseProof :: T.Text -> Proof+parseProof = map (map trim  . T.splitOn "|") . T.lines++renumber :: Proof -> Proof+renumber proofRows = map (fillInReferences refs) proofRows'+  where proofRows' = renumber' proofRows+        refs = makeLookupTable proofRows'++renumber' :: Proof -> Proof+renumber' = zipWith f [(1 :: Int)..]+  where f n row = T.pack (show n) : row++makeLookupTable :: Proof -> [(T.Text, T.Text)]+makeLookupTable = map f+  where f (new : old : _) = (T.pack ("(@" ++ T.unpack old ++ ")"), new)+        f _               = error "short row in logic proof"++fillInReferences :: [(T.Text, T.Text)] -> ProofRow -> ProofRow+fillInReferences refs (label : _ : fields)+  = (label `T.append` ".") : map (multiReplace refs) fields+fillInReferences _ _ = error "short row in logic proof"++multiReplace :: [(T.Text, T.Text)] -> T.Text -> T.Text+multiReplace refs haystack = foldl' replace haystack refs++replace :: T.Text -> (T.Text, T.Text) -> T.Text+replace haystack (needle, label) = T.replace needle label haystack++trim :: T.Text -> T.Text+trim s+  | T.null s       = s+  | T.head s == ' ' = trim (T.tail s)+  | T.last s == ' ' = trim (T.init s)+  | otherwise      = s+++++readDefaults :: P.ReaderOptions+readDefaults = P.def { P.readerStandalone = True,+                       P.readerExtensions = P.pandocExtensions }++parseBlocks :: T.Text -> [P.Block]+parseBlocks s = f . P.runPure $ P.readMarkdown readDefaults s+  where f (Right (P.Pandoc _ bs)) = bs+        f (Left e) = error $ "readMarkdown failed: " ++ show e++defaultColSpec :: P.ColSpec+defaultColSpec = (P.AlignDefault, P.ColWidthDefault)++defaultTableCaption :: P.Caption+defaultTableCaption = P.Caption Nothing []++defaultTableHeader :: P.TableHead+defaultTableHeader = P.TableHead P.nullAttr []++defaultTableFooter :: P.TableFoot+defaultTableFooter = P.TableFoot P.nullAttr []