You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i've just started using turtle and i've made this script to count the number of lines in an ebook:
#!/usr/bin/env stack
-- stack script --resolver=lts-16.27 --package turtle --package protolude --package text
{-# LANGUAGE OverloadedStrings, NoImplicitPrelude, TypeApplications, LambdaCase #-}
moduleMainwhereimportProtoludehiding (FilePath)
importData.Maybe (fromJust)
importTurtle
tempFileLocation ="/tmp/pdf_counter_tmp.pdf"checkPrograms::Shell()
checkPrograms =dolet programs ::IsStrings=> [s]
programs = ["ebook-convert", "pdfinfo"]
locations <-traverse which programs
when (any isNothing locations) $doprint$"you need to have all these programs on your PATH: "++show (programs :: [Text])
exit (ExitFailure1)
isSuccess ExitSuccess=True
isSuccess (ExitFailure _) =False
textPath path = toText path &\caseLeft t ->docase textToLine t ofJust t -> echo ("invalid filepath: "<> t)
Nothing-> echo "filepath contains a newline in it"return t
Right t ->return t
convertFile::FilePath->ShellBool
convertFile path =do
pathT <- textPath path
exitCode <-if hasExtension path "pdf"thenproc"cp" [pathT, tempFileLocation] empty
elseproc"ebook-convert" [pathT, tempFileLocation, "--paper-size", "letter"] empty
return$ isSuccess exitCode
pagesPattern::PatternInteger
pagesPattern = prefix "Pages:"*> space *> decimal
countPages::FilePath->ShellInteger
countPages path =do
conversionSuccessful <- convertFile path
guard conversionSuccessful
info <- inproc "pdfinfo" [tempFileLocation] empty
select $ match pagesPattern $ lineToText info
countDirectory::FilePath->Shell (Integer, FilePath)
countDirectory dir =do
file <- lsif (fmap isRegularFile . stat) dir
count <- countPages file
return (count, fromJust $ stripPrefix dir file)
main::IO()
main =do
sh checkPrograms
path <- getArgs >>=\case
[path] ->return$ decodeString path
_ ->doputStrLn ("this program expects exactly one argument of a file or directory"::Text) ::IO()
exitFailure
isDir <- isDirectory <$> stat path
if isDir
then view $ countDirectory path
else view $ countPages path
i would expect this, when run on a file, to simply print the number of lines in that file
instead, when run on a non-pdf file it returns that and also prints the entire ebook-convert output:
colby@desktop ~/c/p/src> ./Main.hs ~/Documents/books/short/anonymous-desert.epub
1% Converting input to HTML...
InputFormatPlugin: EPUB Input running
on /home/colby/Documents/books/short/anonymous-desert.epub
Parsing all content...
34% Running transforms on e-book...
Merging user specified metadata...
Detecting structure...
Flattening CSS and remapping font sizes...
Source base font size is 12.00000pt
Removing fake margins...
Cleaning up manifest...
Trimming unused files from manifest...
Creating PDF Output...
67% Running PDF Output plugin
68% Parsed all content for markup transformation
70% Completed markup transformation
90% Rendered all HTML as PDF
91% Added links to PDF content
100% Updated metadata in PDF
PDF output written to /tmp/pdf_counter_tmp.pdf
Output saved to /tmp/pdf_counter_tmp.pdf
111
it's possible that this is just some weird ebook-convert behavior, i haven't really looked into it all that much
The text was updated successfully, but these errors were encountered:
@ikea-shark-official: inproc does not capture stderr, so it will forward anything that the subprocess emits to stderr to the console. If you use inprocWithErr then it will capture both stdout and stderr.
i've just started using turtle and i've made this script to count the number of lines in an ebook:
i would expect this, when run on a file, to simply print the number of lines in that file
instead, when run on a non-pdf file it returns that and also prints the entire ebook-convert output:
it's possible that this is just some weird ebook-convert behavior, i haven't really looked into it all that much
The text was updated successfully, but these errors were encountered: