1 -------------------------------------------------------------------------------- 2 {-# LANGUAGE OverloadedStrings #-} 3 {-# LANGUAGE QuasiQuotes #-} 4 {-# LANGUAGE FlexibleContexts #-} 5 module Main where 6 import System.FilePath (makeRelative, (</>), takeDirectory) 7 import System.Environment (getArgs) 8 import qualified System.FilePath.Find as F (find, always, extension) 9 import System.FilePath.Find ((==?)) 10 import System.Directory (createDirectoryIfMissing, doesFileExist) 11 import Text.Pandoc (writeMarkdown, writeHtml5String, Pandoc(..), runPure, PandocError(..)) 12 import Text.Pandoc.Readers.TikiWiki (readTikiWiki) 13 import Data.List (intercalate) 14 import Data.Maybe (fromJust, isJust) 15 import qualified Data.Text as T 16 import HBlog.Lib 17 18 -------------------------------------------------------------------------------- 19 main :: IO () 20 main = do 21 args <- getArgs 22 case args of 23 indir:outdir:[] -> maybeTree indir outdir 24 _ -> putStrLn "Need exactly two arguments, input directory and output directory." 25 26 maybeTree :: String -> String -> IO () 27 maybeTree inthing outthing = do 28 isInFile <- doesFileExist inthing 29 if isInFile then 30 handleFile inthing outthing Nothing 31 else 32 walkTree inthing outthing 33 34 walkTree :: String -> String -> IO () 35 walkTree indir outdir = do 36 putStrLn $ "Searching for files ending in .tiki" 37 files <- F.find F.always (F.extension ==? ".tiki") indir 38 putStrLn $ "Files found: " ++ (intercalate " " files) 39 _ <- mapM (handleFileAndDir indir outdir) files 40 return () 41 42 handleFileAndDir :: FilePath -> FilePath -> FilePath -> IO () 43 handleFileAndDir indir outdir fpath = do 44 let shortname = makeRelative indir fpath 45 let shortnameBase = maybe (error $ "Can't strip .tiki from file name " ++ fpath) T.unpack $ 46 T.stripSuffix ".tiki" $ T.pack $ shortname 47 _ <- createDirectoryIfMissing True (takeDirectory $ (outdir </> shortname)) 48 handleFile (indir </> shortname) (outdir </> shortnameBase ++ ".md") $ Just (outdir </> shortnameBase ++ ".html") 49 50 handleFile :: FilePath -> FilePath -> Maybe FilePath -> IO () 51 handleFile infile outfileMD outfileHTML = do 52 putStrLn $ "Processing file " ++ infile 53 body <- readFile $ infile 54 if T.isInfixOf "~tc~" (T.pack body) then 55 error $ "We do not support ~tc~ style comments, in file " ++ infile ++ " , for the reasons I (rlpowell) described at https://github.com/jgm/pandoc/issues/2552 . Erroring out to stop you from accidentally revealing something secret." 56 else 57 do 58 let pandoc = unTiki infile body 59 pandocString = case runPure $ writeMarkdown hblogPandocWriterOptions pandoc of 60 Left (PandocSomeError err) -> "tiki_to_md handleFile: unknown error: " ++ err 61 Left _ -> "tiki_to_md handleFile: unknown error!" 62 Right item' -> T.unpack item' 63 in do 64 _ <- writeFile outfileMD pandocString 65 if isJust outfileHTML then 66 let pandocHTML = case runPure $ writeHtml5String hblogPandocWriterOptions pandoc of 67 Left (PandocSomeError err) -> "tiki_to_md handleFile: HTML: unknown error: " ++ err 68 Left _ -> "tiki_to_md handleFile: HTML: unknown error!" 69 Right item' -> T.unpack item' 70 in do 71 _ <- writeFile (fromJust outfileHTML) pandocHTML 72 return () 73 else 74 return () 75 76 unTiki :: FilePath -> String -> Pandoc 77 unTiki filePath body = 78 let pandocEither = runPure $ readTikiWiki hblogPandocReaderOptions $ T.pack body in 79 case pandocEither of 80 Left e -> error $ "Pandoc error! on file " ++ filePath ++ ": " ++ (show e) 81 Right doc -> doc