diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2023-10-10 14:52:49 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2023-10-10 14:52:49 +0200 |
commit | efbae930411084096342c4f98abb0694b54368e9 (patch) | |
tree | 29025b44b78fa590730907cbb035655e09b39434 | |
parent | Improve python helpers documentation. (diff) | |
download | vimwiki-scripts-efbae930411084096342c4f98abb0694b54368e9.tar.gz vimwiki-scripts-efbae930411084096342c4f98abb0694b54368e9.tar.xz |
Add tex support for handlingar.
Tex files are now passed through `latexmk -pdf` before being included as
PDF files in the resulting PDF.
-rw-r--r-- | hs/src/Handlingar.hs | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/hs/src/Handlingar.hs b/hs/src/Handlingar.hs index 96ee6cf..4da8541 100644 --- a/hs/src/Handlingar.hs +++ b/hs/src/Handlingar.hs @@ -23,11 +23,17 @@ import Data.Text.IO (putStrLn, writeFile) import Mail (getMail, MailPart(..), getBytes, getFile) import Network.URI (URI, parseURI, uriScheme, uriPath) import Network.URI.Encode (encodeWith, decode) -import System.Directory (makeAbsolute) -import System.FilePath (dropFileName, takeExtension, (<.>)) +import System.Directory (makeAbsolute, createDirectoryIfMissing, copyFile) +import System.FilePath (dropFileName, takeExtension, (<.>), takeBaseName) import System.IO (Handle) import System.Posix.Directory (changeWorkingDirectory, getWorkingDirectory) -import System.Process (cleanupProcess) +import System.Process + ( createProcess + , cleanupProcess + , shell + , CreateProcess(cwd) + , waitForProcess + ) import Tex (toTex, TeX (..)) import Text.DocTemplates (toVal, Val(SimpleVal, ListVal), Context(Context)) import Text.Pandoc @@ -42,8 +48,8 @@ import Text.Pandoc , writePlain ) import Text.Pandoc.Builder -import Text.Pandoc.Extract (AppendixItem, getHeadingData, extractKV) -import Text.Pandoc.Items (comment, dlist) +import Text.Pandoc.Extract (AppendixItem, getHeadingData, extractKV) +import Text.Pandoc.Items (comment, dlist) import Text.Pandoc.Templates (compileDefaultTemplate) import Text.Pandoc.Walk (walk, walkM) import Text.Pandoc.Writers.LaTeX (writeLaTeX) @@ -51,6 +57,7 @@ import Util (joinBy, splitBy, uncurry3, (<&>)) import qualified Data.Map.Lazy as Map import qualified Data.Set as Set import qualified Data.Text.IO as T +import Text.URI.Decode (urlDecode) -- TODO pandoc possibly contains a better way to handle attachements, -- something about media bag @@ -148,7 +155,19 @@ handleFile fname ".pdf" = do (Format "latex") (mconcat $ fmap toTex lines) return . Just $ [ Plain [ inline ] ] -handleFile fname _ = return . Just $ [Para [Str . pack $ fname]] +handleFile fname ".tex" = do + aname <- liftIO $ makeAbsolute fname + let dest = "/tmp/vimwiki-script/" ++ (takeBaseName aname) + liftIO $ createDirectoryIfMissing True dest + liftIO (copyFile aname $ dest ++ "/doc.tex") + (_, _, _, handle) <- liftIO $ createProcess ((shell "latexmk -pdf doc") { cwd = Just dest }) + liftIO $ waitForProcess handle + handleFile (dest ++ "/doc.pdf") ".pdf" + -- return . Just $ [ Plain [ Str . pack $ "TEX compiled " ++ dest ] ] +-- handleFile fname _ = return . Just $ [Para [Str . pack $ fname]] +handleFile fname _ = do + text <- liftIO $ T.readFile fname + return . Just $ [CodeBlock ("", [], []) text] findAlternative :: [MailPart] -> Maybe MailPart findAlternative [] = Nothing @@ -202,7 +221,8 @@ handleMailBody ports mail = img ] return img - -- "application/pdf" -> () + -- TODO + -- ("application", "pdf") -> do _ -> return [ Header 2 ("", [], []) [ Str "Attachment omitted" ] , dlist [ ("Content-Type", contentType mail) , ("Filename", show . filename $ mail) ] @@ -251,7 +271,10 @@ handleBilaga currentPage url = case parseURI . encodeWith (`elem` uriChars) . un Just uri -> case uriScheme uri of ('w':'n':'.':wikiname) -> return . Just $ [Para [Str $ "Interwiki" <> pack wikiname]] "file:" -> do - let fname = uriPath uri + let (Just fname) = urlDecode $ uriPath uri + liftIO $ do + print $ "raw = " ++ uriPath uri + print $ "fname = " ++ fname handleFile fname $ takeExtension fname "mail:" -> Just <$> handleMailLink uri _ -> return $ Just [Para [Str . pack . show $ uri]] @@ -277,7 +300,7 @@ main args = do [file, heading] -> do cwd <- getWorkingDirectory - changeWorkingDirectory (dropFileName file) + changeWorkingDirectory $ dropFileName file text <- T.readFile file |