pdf-slave 1.1.0.0 → 1.2.0.0
raw patch · 4 files changed
+102/−81 lines, 4 filesdep ~pdf-slave-templatePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: pdf-slave-template
API changes (from Hackage documentation)
- Text.PDF.Slave: loadTemplateInMemory :: TemplateFile -> Sh (Either String Template)
+ Text.PDF.Slave: loadTemplateInMemory :: TemplateFile -> FilePath -> Sh (Either String Template)
- Text.PDF.Slave: renderBundleToPDF :: Template -> FilePath -> Sh PDFContent
+ Text.PDF.Slave: renderBundleToPDF :: Template -> Sh PDFContent
- Text.PDF.Slave.Render: loadTemplateInMemory :: TemplateFile -> Sh (Either String Template)
+ Text.PDF.Slave.Render: loadTemplateInMemory :: TemplateFile -> FilePath -> Sh (Either String Template)
- Text.PDF.Slave.Render: renderBundleToPDF :: Template -> FilePath -> Sh PDFContent
+ Text.PDF.Slave.Render: renderBundleToPDF :: Template -> Sh PDFContent
Files
- CHANGELOG.md +12/−0
- app/Main.hs +76/−66
- pdf-slave.cabal +2/−2
- src/Text/PDF/Slave/Render.hs +12/−13
CHANGELOG.md view
@@ -1,3 +1,15 @@+1.2.0.0+=======++* Add `version` command to CLI.++* Fix: `renderBundleToPDF` doesn't take base directory.++* Fix: `loadTemplateInMemory` now takes base directory.++* Strong distinguish between bundle and ordinary template format. Bundle templates+have to define `bundle: true` inside the YAML files.+ 1.1.0.0 =======
app/Main.hs view
@@ -4,9 +4,11 @@ import Data.Maybe (fromMaybe) import Data.Monoid import Data.Text (pack)+import Data.Version (showVersion) import Data.Yaml (decodeEither', encode) import Filesystem.Path.CurrentOS (directory) import Options.Applicative as O+import Paths_pdf_slave (version) import Prelude hiding (FilePath) import Shelly @@ -25,6 +27,8 @@ | PackBundle -- | Make a template distributed over files from all-in bundle | UnpackBundle+ -- | Print version number+ | PrintVersion -- | CLI options data Options = Options {@@ -69,77 +73,83 @@ (O.command "pdf" $ info (pure GeneratePDF) $ progDesc "Generate PDF from template") <> (O.command "pack" $ info (pure PackBundle) $ progDesc "Construct all-in bundle from template distributed over files") <> (O.command "unpack" $ info (pure UnpackBundle) $ progDesc "Deconstruct all-in bundle to several files")+ <> (O.command "version" $ info (pure PrintVersion) $ progDesc "Print program version") -- | Execute PDF slave pdfSlave :: Options -> IO ()-pdfSlave Options{..} = shelly $ do- -- define base directory- baseDir <- case templatePath of- Nothing -> pwd- Just p -> canonic $ directory p- -- read template- (templateContent, contentFilename) <- case templatePath of- Nothing -> do- cnt <- liftIO $ BS.getContents- return (cnt, fromText "<stdin>")- Just p -> (,p) <$> readBinary p- -- read optional input overwrite- optInput <- case inputOverwritePath of- Nothing -> return Nothing- Just p -> do- cnt <- readBinary p- case A.eitherDecode' . BZ.fromStrict $ cnt of- Left e -> fail $ "Failed to read input overwrite file: " <> show e- Right i -> return $ Just i- -- process particular CLI action- case cliCommand of- -- Generate PDF from template or bundle- GeneratePDF -> do- -- parse template contents- res <- parseBundleOrTemplate contentFilename templateContent- -- render- bs <- case res of- Left bundle -> do- let bundle' = fromMaybe bundle $ (\i -> bundle { templateInput = i }) <$> optInput- renderBundleToPDF bundle' baseDir- Right template -> do- inputOverwrite <- sequence $ fmap toTextWarn inputOverwritePath- let template' = fromMaybe template $- (const $ template { templateFileInput = inputOverwrite }) <$> optInput- renderTemplateToPDF template' baseDir- -- output results- case pdfOutputPath of- Nothing -> liftIO $ BS.putStr bs- Just outputPath -> writeBinary outputPath bs+pdfSlave Options{..} = case cliCommand of+ PrintVersion -> putStrLn $ "pdf-slave " <> showVersion version+ _ -> doMainActions+ where+ doMainActions = shelly $ do+ -- define base directory+ baseDir <- case templatePath of+ Nothing -> pwd+ Just p -> canonic $ directory p+ -- read template+ (templateContent, contentFilename) <- case templatePath of+ Nothing -> do+ cnt <- liftIO $ BS.getContents+ return (cnt, fromText "<stdin>")+ Just p -> (,p) <$> readBinary p+ -- read optional input overwrite+ optInput <- case inputOverwritePath of+ Nothing -> return Nothing+ Just p -> do+ cnt <- readBinary p+ case A.eitherDecode' . BZ.fromStrict $ cnt of+ Left e -> fail $ "Failed to read input overwrite file: " <> show e+ Right i -> return $ Just i+ -- process particular CLI action+ case cliCommand of+ -- Generate PDF from template or bundle+ GeneratePDF -> do+ -- parse template contents+ res <- parseBundleOrTemplate contentFilename templateContent+ -- render+ bs <- case res of+ Left bundle -> do+ let bundle' = fromMaybe bundle $ (\i -> bundle { templateInput = i }) <$> optInput+ renderBundleToPDF bundle'+ Right template -> do+ inputOverwrite <- sequence $ fmap toTextWarn inputOverwritePath+ let template' = fromMaybe template $+ (const $ template { templateFileInput = inputOverwrite }) <$> optInput+ renderTemplateToPDF template' baseDir+ -- output results+ case pdfOutputPath of+ Nothing -> liftIO $ BS.putStr bs+ Just outputPath -> writeBinary outputPath bs - -- Pack bundle from distrubuted template format- PackBundle -> case decodeEither' templateContent of- Left e -> fail $ "Failed to parse template: " <> show e- Right tf -> do- res <- loadTemplateInMemory tf- case res of- Left e -> fail $ "Failed to pack bundle: " <> show e- Right t -> do- let bs = encode t- case pdfOutputPath of- Nothing -> liftIO $ BS.putStr bs- Just outputPath -> do- writeBinary outputPath bs- outputPath' <- toTextWarn outputPath- echo $ "Bundle is written to " <> outputPath'+ -- Pack bundle from distrubuted template format+ PackBundle -> case decodeEither' templateContent of+ Left e -> fail $ "Failed to parse template: " <> show e+ Right tf -> do+ res <- loadTemplateInMemory tf baseDir+ case res of+ Left e -> fail $ "Failed to pack bundle: " <> show e+ Right t -> do+ let bs = encode t+ case pdfOutputPath of+ Nothing -> liftIO $ BS.putStr bs+ Just outputPath -> do+ writeBinary outputPath bs+ outputPath' <- toTextWarn outputPath+ echo $ "Bundle is written to " <> outputPath' - -- Unpack bundle to distributed template format- UnpackBundle -> case decodeEither' templateContent of- Left e -> fail $ "Failed to parse template: " <> show e- Right t -> case pdfOutputPath of- Nothing -> fail "Expecting --output parameter as destination folder"- Just outputFolder -> do- mkdir_p outputFolder- tf <- storeTemplateInFiles t outputFolder- let templateFilename = outputFolder </> templateName t <.> "yaml"- writeBinary templateFilename $ encode tf- outputFolder' <- toTextWarn outputFolder- echo $ "Bundle is unpacked to " <> outputFolder'+ -- Unpack bundle to distributed template format+ UnpackBundle -> case decodeEither' templateContent of+ Left e -> fail $ "Failed to parse template: " <> show e+ Right t -> case pdfOutputPath of+ Nothing -> fail "Expecting --output parameter as destination folder"+ Just outputFolder -> do+ mkdir_p outputFolder+ tf <- storeTemplateInFiles t outputFolder+ let templateFilename = outputFolder </> templateName t <.> "yaml"+ writeBinary templateFilename $ encode tf+ outputFolder' <- toTextWarn outputFolder+ echo $ "Bundle is unpacked to " <> outputFolder'+ _ -> return () main :: IO () main = execParser opts >>= pdfSlave
pdf-slave.cabal view
@@ -1,5 +1,5 @@ name: pdf-slave-version: 1.1.0.0+version: 1.2.0.0 synopsis: Tool to generate PDF from haskintex templates and YAML input description: Please see README.md homepage: https://github.com/NCrashed/pdf-slave#readme@@ -31,7 +31,7 @@ , exceptions >= 0.8 && < 0.9 , haskintex >= 0.6 && < 0.8 , HaTeX >= 3.16 && < 3.18- , pdf-slave-template == 1.1.0.0+ , pdf-slave-template == 1.2.0.0 , shelly >= 1.6 && < 1.7 , system-filepath >= 0.4 && < 0.5 , unordered-containers >= 0.2 && < 0.3
src/Text/PDF/Slave/Render.hs view
@@ -80,7 +80,7 @@ case res of Left bundle -> do let bundle' = fromMaybe bundle $ fmap (\i -> bundle { templateInput = Just i }) bundleInput- renderBundleToPDF bundle' baseDir+ renderBundleToPDF bundle' Right template -> renderTemplateToPDF template baseDir -- | Try to parse either a bundle or template file@@ -109,7 +109,7 @@ Left e -> throwM $ BundleFormatError filename e Right bundle -> do let bundle' = fromMaybe bundle $ fmap (\i -> bundle { templateInput = Just i }) bundleInput- renderBundleToPDF bundle' (directory filename)+ renderBundleToPDF bundle' -- | Helper to render from template file renderFromFileToPDF :: FilePath -- ^ Path to 'TemplateFile'@@ -122,11 +122,10 @@ -- | Unpack bundle, render the template, cleanup and return PDF renderBundleToPDF :: Template -- ^ Input all-in template- -> FilePath -- ^ Base directory -> Sh PDFContent-renderBundleToPDF bundle baseDir = withTmpDir $ \unpackDir -> do+renderBundleToPDF bundle = withTmpDir $ \unpackDir -> do template <- storeTemplateInFiles bundle unpackDir- renderTemplateToPDF template baseDir+ renderTemplateToPDF template unpackDir -- | Render template and return content of resulted PDF file renderTemplateToPDF :: TemplateFile -- ^ Input template@@ -138,7 +137,7 @@ Nothing -> return Nothing Just inputName -> do let inputNamePath = fromText inputName- cnt <- readBinary inputNamePath+ cnt <- readBinary (baseDir </> inputNamePath) case A.eitherDecode' . BZ.fromStrict $ cnt of Left e -> throwM $ InputFileFormatError inputNamePath e Right a -> return $ Just a@@ -244,14 +243,14 @@ whenJust (Just a) f = f a -- | Load all external references of template into memory-loadTemplateInMemory :: TemplateFile -> Sh (Either String Template)-loadTemplateInMemory TemplateFile{..} = do+loadTemplateInMemory :: TemplateFile -> FilePath -> Sh (Either String Template)+loadTemplateInMemory TemplateFile{..} baseDir = do inputCnt <- case templateFileInput of Nothing -> return $ Right Nothing Just fname -> do- cnt <- readBinary . fromText $ fname+ cnt <- readBinary $ baseDir </> fromText fname return $ fmap Just . A.eitherDecode' . BZ.fromStrict $ cnt- body <- readfile . fromText $ templateFileBody+ body <- readfile $ baseDir </> fromText templateFileBody deps <- M.traverseWithKey loadDep templateFileDeps return $ Template <$> pure templateFileName@@ -261,16 +260,16 @@ <*> pure templateFileHaskintexOpts where loadDep name d = let- filename = fromText name+ filename = baseDir </> fromText name in case d of BibtexDepFile -> do cnt <- readfile filename return . pure $ BibtexDep cnt TemplateDepFile body -> do- tmpl <- chdir filename $ loadTemplateInMemory body+ tmpl <- loadTemplateInMemory body filename return $ TemplateDep <$> tmpl TemplatePdfDepFile body -> do- tmpl <- chdir filename $ loadTemplateInMemory body+ tmpl <- loadTemplateInMemory body filename return $ TemplatePdfDep <$> tmpl OtherDepFile -> do cnt <- readBinary filename