packages feed

fromhtml 1.0.0 → 1.0.1

raw patch · 4 files changed

+35/−48 lines, 4 filesdep +process-extrasdep −processPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependencies added: process-extras

Dependencies removed: process

API changes (from Hackage documentation)

- Text.FromHTML: fromHTML :: ExportType -> String -> Either Output Output
+ Text.FromHTML: fromHTML :: ExportType -> String -> IO (Either Output Output)

Files

ChangeLog.md view
@@ -1,3 +1,11 @@-# Changelog for transdoc+# Changelog for FromHTML -## Unreleased changes+## [1.0.1] - 2018-08-19++### Changed+- Getting rid of ugly unsafe performing IO+- Using process-extra with ByteString to avoid encoding problems++## [1.0.0] - 2018-08-09++Initial release
app/Main.hs view
@@ -15,7 +15,8 @@   let outfile = args !! 1   let format = (read $ args !! 2) :: ExportType   html <- readFile infile-  case fromHTML format html of+  result <- fromHTML format html+  case result of     Right res -> B.writeFile outfile res     Left err -> do       putStrLn "Couldn't transform that document:"
fromhtml.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 08d57d6634624b09295e2c27ebf5c576c61f1089eeb2f6fcf95493311e3faa01+-- hash: 597d2bb8a4387fffb12cb8c6659c2e7f725ef980e87580aeb755d4d2069fcfef  name:           fromhtml-version:        1.0.0+version:        1.0.1 synopsis:       Simple adapter for transformation of HTML to other formats description:    Please see the README on GitHub at <https://github.com/MarekSuchanek/FromHTML#readme> category:       Text@@ -34,10 +34,11 @@   hs-source-dirs:       src   default-extensions: OverloadedStrings+  ghc-options: -fwarn-unused-imports   build-depends:       base >=4.7 && <5     , bytestring-    , process+    , process-extras     , text   default-language: Haskell2010 @@ -51,6 +52,6 @@       base >=4.7 && <5     , bytestring     , fromhtml-    , process+    , process-extras     , text   default-language: Haskell2010
src/Text/FromHTML.hs view
@@ -22,12 +22,9 @@ import qualified Data.ByteString as B  import           Control.Exception-import           Data.Semigroup-import           GHC.IO.Handle import           GHC.IO.Encoding import           System.Exit-import           System.Process-import           System.IO.Unsafe+import           System.Process.ByteString  -- | Allowed export types data ExportType = HTML@@ -45,64 +42,44 @@                 | PDF                 deriving (Show, Read, Enum, Bounded, Eq) -type Input = String+type Input = B.ByteString type Output = B.ByteString type Command = Input -> IO (Either Output Output)-type Process = IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)  str2BS :: String -> B.ByteString str2BS = E.encodeUtf8 . T.pack  -- | Transform given HTML as String to selected format-fromHTML :: ExportType -> String -> Either Output Output-fromHTML HTML html = Right . str2BS $ html  -- HTML is already provided!-fromHTML PDF html = makePDF html-fromHTML extp html = makePD extp html--makePDF :: Input -> Either Output Output-makePDF html = unsafePerformIO $ wkhtmltopdf html--makePD :: ExportType -> Input -> Either Output Output-makePD expt html = unsafePerformIO $ pandoc expt html+fromHTML :: ExportType -> String -> IO (Either Output Output)+fromHTML HTML html = return $ Right (str2BS html)  -- HTML is already provided!+fromHTML PDF html = wkhtmltopdf (str2BS html)+fromHTML extp html = pandoc extp (str2BS html)  -- | Simple conversion of HTML to PDF using process wkhtmltopdf wkhtmltopdf :: Command-wkhtmltopdf = perform cprocess-    where-        opts = ["--quiet", "--encoding", "utf-8", "-", "-"]-        cprocess = procWith $ proc "wkhtmltopdf" opts+wkhtmltopdf = perform "wkhtmltopdf" ["--quiet", "--encoding", "utf-8", "-", "-"]  -- | Simple conversion of HTML to some format using process pandoc pandoc :: ExportType -> Command-pandoc expt = perform cprocess+pandoc expt = perform "pandoc" args     where         format = exportType2PD expt-        opts = ["-s", "-f", "html", "-t", format, "-o", "-"]-        cprocess = procWith $ proc "pandoc" opts+        args = ["-s", "-f", "html", "-t", format, "-o", "-"]  -- | Perform process (catched IOException)-perform :: CreateProcess -> Command-perform cprocess input = catch (performUnsafe cprocess input)-        (\e -> do let err = show (e :: IOException)-                  return . Left $ "IOException: " <> str2BS err)+perform :: String -> [String] -> Command+perform cmd args input = catch (performUnsafe cmd args input)+        (\e -> do let err = show (e :: SomeException)+                  return . Left $ "Exception: " <> str2BS err)  -- | Perform process (no caching exceptions)-performUnsafe :: CreateProcess -> Command-performUnsafe cprocess input = do+performUnsafe :: String -> [String] -> Command+performUnsafe cmd args input = do     setLocaleEncoding utf8  -- don't know what was locales are there...-    (Just stdin, Just stdout, Just stderr, p) <- createProcess cprocess-    hPutStr stdin input >> hClose stdin-    exitCode <- waitForProcess p-    errors <- B.hGetContents stderr-    output <- B.hGetContents stdout+    (exitCode, stdout, stderr) <- readProcessWithExitCode cmd args input     case exitCode of-      ExitSuccess -> return $ Right output-      _           -> return . Left $ "Exit(" <> str2BS (show exitCode) <> "): " <> errors--procWith p = p { std_out = CreatePipe-               , std_in  = CreatePipe-               , std_err = CreatePipe-               }+      ExitSuccess -> return $ Right stdout+      _           -> return . Left $ str2BS (show exitCode) <> ": " <> stderr  exportType2PD :: ExportType -> String exportType2PD = map C.toLower . show