fromhtml 0.1.0.1 → 0.1.0.2
raw patch · 2 files changed
+58/−103 lines, 2 filesdep −pandocPVP ok
version bump matches the API change (PVP)
Dependencies removed: pandoc
API changes (from Hackage documentation)
Files
- fromhtml.cabal +3/−6
- src/Text/FromHTML.hs +55/−97
fromhtml.cabal view
@@ -2,11 +2,11 @@ -- -- see: https://github.com/sol/hpack ----- hash: 0c6fecd839a6db4cc6216bfa4615d4d11c490a117c453c3e075bf236e25a1551+-- hash: 413af0a60b6c440ea079116a600662b218b5105d19367f3d1a245101ba62f82a name: fromhtml-version: 0.1.0.1-synopsis: Simple library for transformation of HTML to other formats+version: 0.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 homepage: https://github.com/MarekSuchanek/FromHTML#readme@@ -37,7 +37,6 @@ build-depends: base >=4.7 && <5 , bytestring- , pandoc >=2.2 , process , text default-language: Haskell2010@@ -52,7 +51,6 @@ base >=4.7 && <5 , bytestring , fromhtml- , pandoc >=2.2 , process , text default-language: Haskell2010@@ -69,7 +67,6 @@ base >=4.7 && <5 , bytestring , fromhtml- , pandoc >=2.2 , process , text default-language: Haskell2010
src/Text/FromHTML.hs view
@@ -8,8 +8,8 @@ Portability : POSIX Simplified API for transformation of HTML to other formats with Pandoc-and wkhtmltopdf in Haskell code. It requires @wkhtmltopdf@ installed-locally (see <https://wkhtmltopdf.org wkhtmltopdf.org>).+and wkhtmltopdf in Haskell code. It requires @wkhtmltopdf@ and @pandoc@+to be installed locally. -} module Text.FromHTML ( fromHTML@@ -18,39 +18,30 @@ -- import Debug.Trace +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 qualified Data.ByteString.Lazy as BL -import qualified Text.Pandoc as Pandoc-import qualified Text.Pandoc.Templates as PandocTemplates-import qualified Text.Pandoc.Writers as PandocWriters-import qualified Text.Pandoc.Error as PandocError-import qualified Text.Pandoc.PDF as PandocPDF- import GHC.IO.Handle import System.Process import System.IO.Unsafe -- | 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 alias for Pure Pandoc writer-type Writer = (Pandoc.WriterOptions -> Pandoc.Pandoc -> Pandoc.PandocPure B.ByteString)+ | LaTeX+ | RTF+ | RST+ | Markdown+ | AsciiDoc+ | Docx+ | ODT+ | DokuWiki+ | MediaWiki+ | EPUB2+ | EPUB3+ | PDF+ deriving (Show, Read, Enum, Bounded, Eq) -- | Helper function to translate Either to Maybe@@ -63,89 +54,56 @@ -- eitherToMaybe (Right x) = Just x -- eitherToMaybe (Left x) = traceShow x Nothing -readerOptions = Pandoc.def { Pandoc.readerStandalone = True }-writerOptions = Pandoc.def+str2BS :: String -> B.ByteString+str2BS = E.encodeUtf8 . T.pack -- | Transform given HTML as String to selected format fromHTML :: ExportType -> String -> Maybe B.ByteString-fromHTML HTML html = Just . E.encodeUtf8 . T.pack $ html -- HTML is already provided!-fromHTML PDF html = writerHTML2PDF html-fromHTML extp html = case html2pd html of- Just pd -> eitherToMaybe . Pandoc.runPure $ runWriter extp pd- Nothing -> Nothing---runWriter :: ExportType -> Pandoc.Pandoc -> Pandoc.PandocPure B.ByteString-runWriter extp pd = do- template <- getTemplate extp- let opts = writerOptions { Pandoc.writerTemplate = template }- writer extp opts pd---getTemplate :: ExportType -> Pandoc.PandocPure (Maybe String)-getTemplate HTML = Just <$> PandocTemplates.getDefaultTemplate "html5"-getTemplate LaTeX = Just <$> PandocTemplates.getDefaultTemplate "latex"-getTemplate RTF = Just <$> PandocTemplates.getDefaultTemplate "rtf"-getTemplate RST = Just <$> PandocTemplates.getDefaultTemplate "rst"-getTemplate Markdown = Just <$> PandocTemplates.getDefaultTemplate "markdown"-getTemplate AsciiDoc = Just <$> PandocTemplates.getDefaultTemplate ""-getTemplate Docx = Just <$> PandocTemplates.getDefaultTemplate "docx"-getTemplate ODT = Just <$> PandocTemplates.getDefaultTemplate "odt"-getTemplate DokuWiki = Just <$> PandocTemplates.getDefaultTemplate "dokuwiki"-getTemplate MediaWiki = Just <$> PandocTemplates.getDefaultTemplate "mediawiki"-getTemplate EPUB2 = Just <$> PandocTemplates.getDefaultTemplate "epub2"-getTemplate EPUB3 = Just <$> PandocTemplates.getDefaultTemplate "epub3"-getTemplate _ = return Nothing-+fromHTML HTML html = Just . str2BS $ html -- HTML is already provided!+fromHTML PDF html = makePDF (str2BS html)+fromHTML extp html = makePD extp (str2BS html) -html2pd :: String -> Maybe Pandoc.Pandoc-html2pd html = eitherToMaybe . Pandoc.runPure $ Pandoc.readHtml readerOptions (T.pack html)+type Input = B.ByteString+type Output = B.ByteString+type Command = Input -> IO (Maybe Output)+type Process = IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) --- | Ugly PDF writer from HTML--- writerHTML2PDF opts pd = fixError . unsafePerformIO . Pandoc.runIO $ PandocPDF.makePDF "wkhtmltopdf" ["--quiet"] PandocWriters.writeHtml5String opts pd--- where--- fixError (Left pderr) = Left pderr--- fixError (Right (Left bserr)) = Left . PandocError.PandocSomeError . T.unpack . E.decodeUtf8 . BL.toStrict $ bserr--- fixError (Right (Right x)) = Right (BL.toStrict x)+makePDF :: Input -> Maybe Output+makePDF html = unsafePerformIO $ wkhtmltopdf html --- | Wrapping HTML to PDF conversion which is unsafe-writerHTML2PDF :: String -> Maybe B.ByteString-writerHTML2PDF = Just . unsafePerformIO . html2pdf+makePD :: ExportType -> Input -> Maybe Output+makePD expt html = unsafePerformIO $ pandoc expt html -- | Simple conversion of HTML to PDF using process wkhtmltopdf-html2pdf :: String -> IO B.ByteString-html2pdf html = do- (Just stdin, Just stdout, _, _) <- createProcess cprocess- hPutStr stdin html >> hClose stdin- B.hGetContents stdout+wkhtmltopdf :: Input -> IO (Maybe Output)+wkhtmltopdf = perform cprocess where- procWith p = p { std_out = CreatePipe- , std_in = CreatePipe- } opts = ["--quiet", "--encoding", "utf-8", "-", "-"] cprocess = procWith $ proc "wkhtmltopdf" opts --- | Select Writer based on given ExportType-writer :: ExportType -> Writer-writer = wrapWriter . pandocWriter- where- wrapWriter :: Pandoc.Writer Pandoc.PandocPure -> Writer- wrapWriter (Pandoc.TextWriter tw) = \opts pd -> E.encodeUtf8 <$> tw opts pd- wrapWriter (Pandoc.ByteStringWriter bsw) = \opts pd -> BL.toStrict <$> bsw opts pd+-- | Simple conversion of HTML to PDF using process wkhtmltopdf+pandoc :: ExportType -> Input -> IO (Maybe Output)+pandoc expt = perform cprocess+ where+ format = exportType2PD expt+ opts = ["-s", "-f", "html", "-t", format, "-o", "-"]+ cprocess = procWith $ proc "pandoc" opts --- | Pick Pandoc writer for pure transformation-pandocWriter :: ExportType -> Pandoc.Writer Pandoc.PandocPure-pandocWriter HTML = Pandoc.TextWriter PandocWriters.writeHtml5String-pandocWriter LaTeX = Pandoc.TextWriter PandocWriters.writeLaTeX-pandocWriter RTF = Pandoc.TextWriter PandocWriters.writeRTF-pandocWriter RST = Pandoc.TextWriter PandocWriters.writeRST-pandocWriter Markdown = Pandoc.TextWriter PandocWriters.writeMarkdown-pandocWriter AsciiDoc = Pandoc.TextWriter PandocWriters.writeAsciiDoc-pandocWriter DokuWiki = Pandoc.TextWriter PandocWriters.writeDokuWiki-pandocWriter MediaWiki = Pandoc.TextWriter PandocWriters.writeMediaWiki-pandocWriter Docx = Pandoc.ByteStringWriter PandocWriters.writeDocx-pandocWriter ODT = Pandoc.ByteStringWriter PandocWriters.writeODT-pandocWriter EPUB2 = Pandoc.ByteStringWriter PandocWriters.writeEPUB2-pandocWriter EPUB3 = Pandoc.ByteStringWriter PandocWriters.writeEPUB3-pandocWriter PDF = pandocWriter HTML -- cannot be done as PandocPure+perform :: CreateProcess -> Input -> IO (Maybe Output)+perform cprocess input = do+ (Just stdin, Just stdout, Just stderr, _) <- createProcess cprocess+ B.hPutStr stdin input >> hClose stdin+ errors <- B.hGetContents stderr+ case errors of+ "" -> Just <$> B.hGetContents stdout+ _ -> return Nothing+++procWith p = p { std_out = CreatePipe+ , std_in = CreatePipe+ , std_err = CreatePipe+ }++exportType2PD :: ExportType -> String+exportType2PD = map C.toLower . show