packages feed

pandoc-vimhl (empty) → 0.1.0.0

raw patch · 4 files changed

+126/−0 lines, 4 filesdep +basedep +conddep +directorysetup-changed

Dependencies added: base, cond, directory, filepath, pandoc-types, process, regex-compat, temporary

Files

+ LICENSE view
@@ -0,0 +1,25 @@+The following license covers this documentation, and the source code, except+where otherwise indicated.++Copyright 2016, Alexey Radkov. 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.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "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 HOLDERS 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,3 @@+import Distribution.Simple+main = defaultMain+
+ pandoc-vimhl.cabal view
@@ -0,0 +1,27 @@+name:                pandoc-vimhl+version:             0.1.0.0+synopsis:            Pandoc filter for native Vim code highlighting+description:         Pandoc filter for native Vim code highlighting in HTML and+        PDF documents. Requires Vim and plugin+        <http://github.com/lyokha/vim-publish-helper vim-publish-helper>+homepage:            http://github.com/lyokha/vim-publish-helper+license:             BSD3+license-file:        LICENSE+author:              Alexey Radkov <alexey.radkov@gmail.com>+maintainer:          Alexey Radkov <alexey.radkov@gmail.com>+copyright:           2016 Alexey Radkov+category:            Text+build-type:          Simple+cabal-version:       >= 1.8++executable vimhl+  build-depends:       base >= 4.7 && < 5+                     , pandoc-types >= 1.12+                     , directory+                     , filepath+                     , process+                     , temporary >= 1.1+                     , regex-compat+                     , cond >= 0.2+  main-is:             vimhl.hs+
+ vimhl.hs view
@@ -0,0 +1,71 @@+#!/usr/bin/env runhaskell++-- vimhl.hs+import Text.Pandoc.JSON+import Text.Regex (mkRegex, splitRegex)+import System.IO+import System.IO.Temp+import System.IO.Error+import System.Directory+import System.FilePath+import System.Process+import Data.Char (toLower)+import Data.Maybe (fromMaybe)+import Control.Arrow (first)+import Control.Monad+import Control.Conditional++vimHl :: Maybe Format -> Block -> IO Block+vimHl (Just fm@(Format fmt)) (CodeBlock (_, cls@(ft:_), namevals) contents)+    | lookup "hl" namevals' == Just "vim" && fmt `elem` ["html", "latex"] = do+        let vimhlcmd =+                unwords [cmd fmt, nmb]+                where cmd "html"  = "MakeHtmlCodeHighlight"+                      cmd "latex" = "MakeTexCodeHighlight"+                      nmb | "numberLines" `elem` cls =+                              fromMaybe "-1" $ lookup "startfrom" namevals'+                          | otherwise = ""+            colorscheme =+                maybe "" (("-c 'let g:PhColorscheme = \"" ++) . (++ "\"'")) $+                    lookup "colorscheme" namevals'+            cmds =+                maybe "" (unwords . map (cmd . flag) . filter (not . null) .+                            map (splitRegex $ dl"=") . splitRegex (dl",")) $+                                lookup "vars" namevals'+                where cmd (x:y:_) =+                                "--cmd 'let g:" ++ x ++ " = \"" ++ y ++ "\"'"+                      flag [x]    = [x, "1"]+                      flag x      = x+                      dl          = mkRegex . ("\\s*" ++) . (++ "\\s*")+            rccmd = do+                home <- getHomeDirectory `catchIOError` const (return "")+                let vimrc  = home `combine` ".vimrc.pandoc"+                    exists = let (&&>) = liftM2 (<&&>)+                             in doesFileExist &&>+                                 (getPermissions >=> return . readable)+                    ($>) = liftM2 (<$>)+                (bool "" . ("--noplugin -u '" ++) . (++ "'")) $> exists $ vimrc+            runVim src dst hsrc hdst = do+                hPutStr hsrc contents+                mapM_ hClose [hsrc, hdst]+                vimrc <- rccmd+                {- vim must think that it was launched from a terminal,+                 - otherwise it won't load its usual environment and the+                 - syntax engine! -}+                hin <- openFile "/dev/tty" ReadMode+                (_, Just hout, _, handle) <- createProcess (shell $ unwords+                    ["vim -Nen", cmds, vimrc, colorscheme, "-c 'set ft=" ++ ft,+                     "|", vimhlcmd ++ "' -c 'w!", dst ++ "' -c 'qa!'", src])+                    {std_in = UseHandle hin, std_out = CreatePipe}+                waitForProcess handle+                mapM_ hClose [hin, hout]+        block <- withSystemTempFile "_vimhl_src." $+                    \src hsrc -> withSystemTempFile "_vimhl_dst." $+                        \dst hdst -> runVim src dst hsrc hdst >> readFile dst+        return $ RawBlock fm block+    where namevals' = map (first $ map toLower) namevals+vimHl _ cb = return cb++main :: IO ()+main = toJSONFilter vimHl+