packages feed

pandoc-citeproc-preamble (empty) → 1.0.0

raw patch · 4 files changed

+130/−0 lines, 4 filesdep +basedep +directorydep +filepathsetup-changed

Dependencies added: base, directory, filepath, pandoc-types, process

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) Sean Whitton, 2015++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * 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.++    * Neither the name of Your name here nor the names of other+      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+OWNER 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ pandoc-citeproc-preamble.cabal view
@@ -0,0 +1,26 @@+name:                pandoc-citeproc-preamble+version:             1.0.0+synopsis:            Insert a preamble before pandoc-citeproc's bibliography+description:         Please see README.md+homepage:            https://github.com/spwhitton/pandoc-citeproc-preamble+license:             GPL-3+license-file:        LICENSE+author:              Sean Whitton+maintainer:          spwhitton@spwhitton.name+category:            Text+build-type:          Simple+cabal-version:       >=1.10++executable pandoc-citeproc-preamble+  hs-source-dirs:      src+  main-is:             pandoc-citeproc-preamble.hs+  build-depends:       base >= 4 && < 5+                     , pandoc-types+                     , process+                     , directory+                     , filepath+  default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/spwhitton/pandoc-citeproc-preamble
+ src/pandoc-citeproc-preamble.hs view
@@ -0,0 +1,72 @@+{-++    pandoc-citeproc-preamble --- Insert a preamble before pandoc-citeproc's bibliography++    Copyright (C) 2015  Sean Whitton++    This file is part of pandoc-citeproc-preamble.++    pandoc-citeproc-preamble is free software: you can redistribute it+    and/or modify it under the terms of the GNU General Public License as+    published by the Free Software Foundation, either version 3 of the+    License, or (at your option) any later version.++    pandoc-citeproc-preamble is distributed in the hope that it will+    be useful, but WITHOUT ANY WARRANTY; without even the implied warranty+    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU+    General Public License for more details.++    You should have received a copy of the GNU General Public License+    along with pandoc-citeproc-preamble.  If not, see+    <http://www.gnu.org/licenses/>.++-}++import           Data.List        (isPrefixOf)+import           System.Directory (doesFileExist)+import           System.FilePath  ((<.>), (</>))+import           System.Process   (readProcess)+import           Text.Pandoc.JSON++insertPreamble :: Block -> [Block] -> [Block]+insertPreamble preamble = foldr step []+  where+    step refs@(Div (_, ["references"], _) _) xs = preamble : refs : xs+    step x xs = x:xs++doPreamble :: Maybe Format -> Pandoc -> IO Pandoc+doPreamble maybeFormat input@(Pandoc meta blocks) =+    findPreambleFile format meta+    >>= maybe (return input)+    (\file -> do+          ls <- readFile file+          return $ Pandoc meta (insertPreamble (lsBlock ls) blocks))+  where+    lsBlock ls = RawBlock format ls+    format = maybe (Format "latex") id maybeFormat++findPreambleFile :: Format -> Meta -> IO (Maybe FilePath)+findPreambleFile (Format format) meta =+    case lookupMeta "citeproc-preamble" meta >>= toPath of+        metadataPreamble@(Just _) -> return metadataPreamble+        Nothing                   -> tryDatadir+  where+    tryDatadir = do+        defaultPreamble <- (</> "citeproc-preamble" </> "default" <.> format)+                           <$> getPandocDatadir+        doesFileExist defaultPreamble >>= \exists ->+            return $ if exists then Just defaultPreamble else Nothing++getPandocDatadir :: IO FilePath+getPandocDatadir =+    drop (length prefix) . head . filter (prefix `isPrefixOf`) . lines+    <$> readProcess "pandoc" ["--version"] ""+  where+    prefix = "Default user data directory: "++toPath                :: MetaValue -> Maybe String+toPath (MetaString s) = Just s+toPath _              = Nothing++main :: IO ()+main = toJSONFilter doPreamble