fromhtml 1.0.1 → 1.0.2
raw patch · 7 files changed
+166/−165 lines, 7 filessetup-changedPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- ChangeLog.md +11/−11
- LICENSE +21/−21
- README.md +16/−16
- Setup.hs +2/−2
- app/Main.hs +24/−24
- fromhtml.cabal +7/−6
- src/Text/FromHTML.hs +85/−85
ChangeLog.md view
@@ -1,11 +1,11 @@-# Changelog for FromHTML--## [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+# Changelog for FromHTML + +## [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
LICENSE view
@@ -1,21 +1,21 @@-MIT License--Copyright (c) 2017 Marek Suchánek (marek.suchanek@fit.cvut.cz)--Permission is hereby granted, free of charge, to any person obtaining a copy-of this software and associated documentation files (the "Software"), to deal-in the Software without restriction, including without limitation the rights-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell-copies of the Software, and to permit persons to whom the Software is-furnished to do so, subject to the following conditions:--The above copyright notice and this permission notice shall be included in all-copies or substantial portions of the Software.--THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE-SOFTWARE.+MIT License + +Copyright (c) 2017 Marek Suchánek (marek.suchanek@fit.cvut.cz) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.
README.md view
@@ -1,16 +1,16 @@-# FromHTML--[](LICENSE)-[](https://travis-ci.org/MarekSuchanek/FromHTML)-[](https://hackage.haskell.org/package/fromhtml)-[](http://packdeps.haskellers.com/feed?needle=fromhtml)--Simplified API for pure transformation of HTML to other formats with Pandoc in Haskell code.--## Purpose--Pandoc is awesome but using its Haskell API can be a little bit bothersome. This project aims to provide simpler API to transform documents without using monads or even some more advanced magic.--## License--This project is licensed under the MIT license - see the [LICENSE](LICENSE) file for more details.+# FromHTML + +[](LICENSE) +[](https://travis-ci.org/MarekSuchanek/FromHTML) +[](https://hackage.haskell.org/package/fromhtml) +[](http://packdeps.haskellers.com/feed?needle=fromhtml) + +Simplified API for pure transformation of HTML to other formats with Pandoc in Haskell code. + +## Purpose + +Pandoc is awesome but using its Haskell API can be a little bit bothersome. This project aims to provide simpler API to transform documents without using monads or even some more advanced magic. + +## License + +This project is licensed under the MIT license - see the [LICENSE](LICENSE) file for more details.
Setup.hs view
@@ -1,2 +1,2 @@-import Distribution.Simple-main = defaultMain+import Distribution.Simple +main = defaultMain
app/Main.hs view
@@ -1,24 +1,24 @@-module Main where--import Prelude-import qualified Data.ByteString as B-import qualified Data.ByteString.Char8 as C-import System.Environment-import System.Exit--import Text.FromHTML--main :: IO ()-main = do- args <- getArgs- let infile = head args- let outfile = args !! 1- let format = (read $ args !! 2) :: ExportType- html <- readFile infile- result <- fromHTML format html- case result of- Right res -> B.writeFile outfile res- Left err -> do- putStrLn "Couldn't transform that document:"- C.putStrLn err- exitFailure+module Main where + +import Prelude +import qualified Data.ByteString as B +import qualified Data.ByteString.Char8 as C +import System.Environment +import System.Exit + +import Text.FromHTML + +main :: IO () +main = do + args <- getArgs + let infile = head args + let outfile = args !! 1 + let format = (read $ args !! 2) :: ExportType + html <- readFile infile + result <- fromHTML format html + case result of + Right res -> B.writeFile outfile res + Left err -> do + putStrLn "Couldn't transform that document:" + C.putStrLn err + exitFailure
fromhtml.cabal view
@@ -1,11 +1,13 @@--- This file has been generated from package.yaml by hpack version 0.28.2.+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.31.2. -- -- see: https://github.com/sol/hpack ----- hash: 597d2bb8a4387fffb12cb8c6659c2e7f725ef980e87580aeb755d4d2069fcfef+-- hash: 8bab40642781a60f3d71a513789692466ac753726db91595bdc3a5af39472bb4 name: fromhtml-version: 1.0.1+version: 1.0.2 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@@ -13,14 +15,13 @@ bug-reports: https://github.com/MarekSuchanek/FromHTML/issues author: Marek Suchánek maintainer: marek.suchanek@fit.cvut.cz-copyright: 2018 Marek Suchánek+copyright: 2020 Marek Suchánek license: MIT license-file: LICENSE build-type: Simple-cabal-version: >= 1.10 extra-source-files:- ChangeLog.md README.md+ ChangeLog.md source-repository head type: git
src/Text/FromHTML.hs view
@@ -1,85 +1,85 @@-{-|-Module : Text.FromHTML-Description : Simple library for transformation of HTML to other formats-Copyright : (c) Marek Suchánek, 2018-License : MIT-Maintainer : marek.suchanek@fit.cvut.cz-Stability : experimental-Portability : POSIX--Simplified API for transformation of HTML to other formats with Pandoc-and wkhtmltopdf in Haskell code. It requires @wkhtmltopdf@ and @pandoc@-to be installed locally.--}-module Text.FromHTML- ( fromHTML- , ExportType(..)- ) where--import qualified Data.Char as C-import qualified Data.Text as T-import qualified Data.Text.Encoding as E-import qualified Data.ByteString as B--import Control.Exception-import GHC.IO.Encoding-import System.Exit-import System.Process.ByteString---- | Allowed export types-data ExportType = HTML- | LaTeX- | RTF- | RST- | Markdown- | AsciiDoc- | Docx- | ODT- | DokuWiki- | MediaWiki- | EPUB2- | EPUB3- | PDF- deriving (Show, Read, Enum, Bounded, Eq)--type Input = B.ByteString-type Output = B.ByteString-type Command = Input -> IO (Either Output Output)--str2BS :: String -> B.ByteString-str2BS = E.encodeUtf8 . T.pack---- | Transform given HTML as String to selected format-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 "wkhtmltopdf" ["--quiet", "--encoding", "utf-8", "-", "-"]---- | Simple conversion of HTML to some format using process pandoc-pandoc :: ExportType -> Command-pandoc expt = perform "pandoc" args- where- format = exportType2PD expt- args = ["-s", "-f", "html", "-t", format, "-o", "-"]---- | Perform process (catched IOException)-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 :: String -> [String] -> Command-performUnsafe cmd args input = do- setLocaleEncoding utf8 -- don't know what was locales are there...- (exitCode, stdout, stderr) <- readProcessWithExitCode cmd args input- case exitCode of- ExitSuccess -> return $ Right stdout- _ -> return . Left $ str2BS (show exitCode) <> ": " <> stderr--exportType2PD :: ExportType -> String-exportType2PD = map C.toLower . show+{-| +Module : Text.FromHTML +Description : Simple library for transformation of HTML to other formats +Copyright : (c) Marek Suchánek, 2018 +License : MIT +Maintainer : marek.suchanek@fit.cvut.cz +Stability : experimental +Portability : POSIX + +Simplified API for transformation of HTML to other formats with Pandoc +and wkhtmltopdf in Haskell code. It requires @wkhtmltopdf@ and @pandoc@ +to be installed locally. +-} +module Text.FromHTML + ( fromHTML + , ExportType(..) + ) where + +import qualified Data.Char as C +import qualified Data.Text as T +import qualified Data.Text.Encoding as E +import qualified Data.ByteString as B + +import Control.Exception +import GHC.IO.Encoding +import System.Exit +import System.Process.ByteString + +-- | Allowed export types +data ExportType = HTML + | LaTeX + | RTF + | RST + | Markdown + | AsciiDoc + | Docx + | ODT + | DokuWiki + | MediaWiki + | EPUB2 + | EPUB3 + | PDF + deriving (Show, Read, Enum, Bounded, Eq) + +type Input = B.ByteString +type Output = B.ByteString +type Command = Input -> IO (Either Output Output) + +str2BS :: String -> B.ByteString +str2BS = E.encodeUtf8 . T.pack + +-- | Transform given HTML as String to selected format +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 "wkhtmltopdf" ["--quiet", "--disable-smart-shrinking", "--encoding", "utf-8", "-", "-"] + +-- | Simple conversion of HTML to some format using process pandoc +pandoc :: ExportType -> Command +pandoc expt = perform "pandoc" args + where + format = exportType2PD expt + args = ["-s", "-f", "html", "-t", format, "-o", "-"] + +-- | Perform process (catched IOException) +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 :: String -> [String] -> Command +performUnsafe cmd args input = do + setLocaleEncoding utf8 -- don't know what was locales are there... + (exitCode, stdout, stderr) <- readProcessWithExitCode cmd args input + case exitCode of + ExitSuccess -> return $ Right stdout + _ -> return . Left $ str2BS (show exitCode) <> ": " <> stderr + +exportType2PD :: ExportType -> String +exportType2PD = map C.toLower . show