packages feed

pandoc-vimhl 0.1.1.0 → 0.1.2.0

raw patch · 4 files changed

+53/−35 lines, 4 files

Files

+ Changelog.md view
@@ -0,0 +1,8 @@+### 0.1.2.0++- Better treatment of handles of temporary files.+- Using *WriteMode* for vim process *std_in* prevents vim from getting+  unresponsive on *Ctrl-C* interrupts while still doing well its tasks.+- Using */dev/null* for vim process *std_out*.+- Exit program as soon as vim fails.+
LICENSE view
@@ -1,7 +1,7 @@ The following license covers this documentation, and the source code, except where otherwise indicated. -Copyright 2016, Alexey Radkov. All rights reserved.+Copyright 2016-2018, 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:
pandoc-vimhl.cabal view
@@ -1,15 +1,16 @@ name:                pandoc-vimhl-version:             0.1.1.0+version:             0.1.2.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>+        <http://github.com/lyokha/vim-publish-helper vim-publish-helper>. homepage:            http://github.com/lyokha/vim-publish-helper license:             BSD3 license-file:        LICENSE+extra-source-files:  Changelog.md author:              Alexey Radkov <alexey.radkov@gmail.com> maintainer:          Alexey Radkov <alexey.radkov@gmail.com>-copyright:           2016 Alexey Radkov+copyright:           2016-2018 Alexey Radkov category:            Text build-type:          Simple cabal-version:       >= 1.8
vimhl.hs view
@@ -9,11 +9,13 @@ import System.Directory import System.FilePath import System.Process+import System.Exit import Data.Char (toLower) import Data.Maybe (fromMaybe) import Control.Arrow (first, (&&&)) import Control.Monad-import Control.Conditional+import Control.Exception (bracket)+import Control.Conditional hiding (unless)  vimHl :: Maybe Format -> Block -> IO Block vimHl (Just fm@(Format fmt)) (CodeBlock (_, cls@(ft:_), namevals) contents)@@ -22,6 +24,7 @@                 unwords [cmd fmt, nmb]                 where cmd "html"  = "MakeHtmlCodeHighlight"                       cmd "latex" = "MakeTexCodeHighlight"+                      cmd x       = error $ "Unexpected format '" ++ x ++ "'"                       nmb | "numberLines" `elem` cls =                               fromMaybe "-1" $ lookup "startfrom" namevals'                           | otherwise = ""@@ -32,37 +35,43 @@                 maybe "" (unwords . map cmd . filter (not . null . snd) .                     map (matchRegexAll (dl"=") &&& id) . splitRegex (dl",")) $                         lookup "vars" namevals'-                where cmd (Nothing           , x) = mkCmd x "1"-                      cmd (Just ("", _, y, _), _) = error $ "Bare value '" ++-                                                        y ++ "' found in vars"-                      cmd (Just ( x, _, y, _), _) = mkCmd x  y+                where cmd (Nothing, x) = mkCmd x "1"+                      cmd (Just ("", _, y, _), _) =+                          error $ "Bare value '" ++ y ++ "' found in vars"+                      cmd (Just (x, _, y, _), _) = mkCmd x y                       mkCmd x y = "--cmd 'let g:" ++ x ++ " = \"" ++ y ++ "\"'"-                      dl        = mkRegex . ("\\s*" ++) . (++ "\\s*")-            vimrccmd = 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 <- vimrccmd-                {- 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+                      dl = mkRegex . ("\\s*" ++) . (++ "\\s*")+        vimrccmd <- do+            home <- getHomeDirectory `catchIOError` const (return "")+            let vimrc  = home </> ".vimrc.pandoc"+                exists = doesFileExist &&>+                    (getPermissions >=> return . readable)+                (&&>) = liftM2 (<&&>)+                ($>)  = liftM2 (<$>)+            (bool "" . ("--noplugin -u '" ++) . (++ "'")) $> exists $ vimrc+        block <- withSystemTempFile "_vimhl_src." $ \src hsrc -> do+            hPutStr hsrc contents >> hFlush hsrc+            bracket (emptySystemTempFile "_vimhl_dst.") removeFile $+                \dst -> do+                    {- vim must think that it was launched from a terminal,+                     - otherwise it won't load its usual environment and the+                     - syntax engine! Using WriteMode for stdin prevents vim+                     - from getting unresponsive on Ctrl-C interrupts while+                     - still doing well its task (vim checks that input is a+                     - terminal using isatty(), however it does not check the+                     - mode of the handle). -}+                    hin <- openFile "/dev/tty" WriteMode+                    hout <- openFile "/dev/null" WriteMode+                    (_, _, _, handle) <- createProcess+                        (shell $ unwords+                            ["vim -Nen", cmds, vimrccmd, colorscheme+                            ,"-c 'set ft=" ++ ft, "|", vimhlcmd ++ "' -c 'w!"+                            ,dst ++ "' -c 'qa!'", src+                            ]+                        ) {std_in = UseHandle hin, std_out = UseHandle hout}+                    r <- waitForProcess handle+                    unless (r == ExitSuccess) $ exitWith r+                    readFile dst         return $ RawBlock fm block     where namevals' = map (first $ map toLower) namevals vimHl _ cb = return cb