packages feed

blatex 0.1.0.6 → 0.1.0.7

raw patch · 3 files changed

+131/−112 lines, 3 files

Files

+ BlaTeX.hs view
@@ -0,0 +1,109 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TypeSynonymInstances #-}+{-# LANGUAGE FlexibleInstances #-}++module BlaTeX where++--Stuff for BlazeHTML+import Control.Monad (forM_)++import Text.Blaze.Html5 as H hiding (main, map)+import Text.Blaze.Html5.Attributes as A+import Text.Blaze.Html.Renderer.Pretty++--Stuff for HaTeX+import Text.LaTeX hiding (unlines)+import Text.LaTeX.Base.Parser+import Text.LaTeX.Base.Syntax+import Data.Text (pack, unpack)+import qualified Data.Text.IO as T++--Stuff for TagSoup+import Text.HTML.TagSoup++--Stuff for Dates+import Data.Dates++--Other stuff I'm using+import Data.List.Split+import Data.Either+import Control.Applicative+import Files++instance Show Html where+  show = renderHtml++postsToHtml :: [Post] -> Html+postsToHtml xs = do+  ul ! A.id "blog-posts" $+    forM_ xs h+  where+    h s = li ! class_ "blog-post" $+          a ! href (stringValue ("posts/"++fileName s++".pdf")) $+          toHtml (postTitle s)+          +data Post = Post {+  fileName :: String+  , postTitle :: String+  , postAuthor :: String+  , postDate :: DateTime+  , syntaxTree :: LaTeX+    }+    deriving (Eq)++instance Ord Post where+  compare (Post _ _ _ d1 _) (Post _ _ _ d2 _) = compare d1 d2++instance Show Post where+  show (Post fn t a d _) = fn ++ " is a post called " ++ t ++ " written by " ++ (a)++getPDF :: FilePath -> Either String String+getPDF xs = if length splitUp == 2 +            then +              if splitUp !! 1 == "pdf" +              then Right (splitUp !! 0) +              else Left "Not a pdf file"+            else Left "Not a file (probably a folder)"+  where splitUp = splitOn "." xs++extractFromArgs :: String -> [[TeXArg]] -> Either String Text+extractFromArgs _ (((FixArg (TeXRaw s)):_):_) = Right s+extractFromArgs s [] = Left ("Command not found: " ++ s)+extractFromArgs s _ = Left ("Could not parse arguments passed to " ++ s ++ " command")++getCommandValue :: String -> LaTeX -> Either String Text+getCommandValue s = (extractFromArgs s . lookForCommand s) ++--Converts either to maybe (for use by maybe applicative)+--eToM :: Either a a -> Maybe a+--eToM e = case e of+--   Left _ -> Nothing+--   Right d -> (Just d)++hackyTypeMagic :: Either String (Either b c) -> Either String c+hackyTypeMagic (Left e) = Left e+hackyTypeMagic (Right (Left _)) = Left "Could not parse date" +hackyTypeMagic (Right (Right r)) = Right r++createPost :: String -> Either ParseError LaTeX -> DateTime -> Either String Post+createPost _ (Left err) _ = Left (show err) --This is sketchy, I don't like just "show"ing the ParseError+createPost s (Right t)  time = Post <$> pure s <*> title <*> author <*> date <*> pure t+  where +    date = hackyTypeMagic (fmap (parseDate time) (unpack <$> (getCommandValue "date" t)))+    author = unpack <$> (getCommandValue "author" t)+    title = unpack <$> (getCommandValue "title" t)++fileNameToPost :: String -> IO (Either String Post) +fileNameToPost fn = do+  latexFile <- fmap (parseLaTeX . pack) (readFile ("posts/"++fn++".tex"))+  t <- getCurrentDateTime+  return (createPost fn latexFile t)++injectPosts :: String -> Html -> Either String String +injectPosts layout ul = if length splitFile == 2+                        then Right (renderTags (beginning ++ parseTags (show ul) ++ end))+                        else Left "Broken layout file"+  where+    splitFile = splitOn [(TagOpen "ul" [("id","blog-posts")]), (TagClose "ul")] (parseTags layout)+    beginning = splitFile !! 0+    end = splitFile !! 1 --safe indexing?
Main.hs view
@@ -1,111 +1,10 @@-{-# LANGUAGE OverloadedStrings #-}-{-# LANGUAGE TypeSynonymInstances #-}-{-# LANGUAGE FlexibleInstances #-}----Stuff for BlazeHTML-import Control.Monad (forM_)--import Text.Blaze.Html5 as H hiding (main, map)-import Text.Blaze.Html5.Attributes as A-import Text.Blaze.Html.Renderer.Pretty----Stuff for HaTeX-import Text.LaTeX hiding (unlines)-import Text.LaTeX.Base.Parser-import Text.LaTeX.Base.Syntax-import Data.Text (pack, unpack)-import qualified Data.Text.IO as T----Stuff for TagSoup-import Text.HTML.TagSoup----Stuff for Dates-import Data.Dates----Other stuff I'm using import System.Directory -import Data.List.Split+import Data.Either import Data.List-import Data.Maybe-import Control.Applicative import System.Environment (getArgs) import System.Process (readProcess) import Files--instance Show Html where-  show = renderHtml--postsToHtml :: [Post] -> Html-postsToHtml xs = do-  ul ! A.id "blog-posts" $-    forM_ xs h-  where-    h s = li ! class_ "blog-post" $-          a ! href (stringValue ("posts/"++fileName s++".pdf")) $-          toHtml (postTitle s)-          -data Post = Post {-  fileName :: String-  , postTitle :: String-  , postAuthor :: String-  , postDate :: DateTime-  , syntaxTree :: LaTeX-    }-    deriving (Eq)--instance Ord Post where-  compare (Post _ _ _ d1 _) (Post _ _ _ d2 _) = compare d1 d2--instance Show Post where-  show (Post fn t a d _) = fn ++ " is a post called " ++ t ++ " written by " ++ (a)--getPDF :: FilePath -> Maybe String-getPDF xs = if length splitUp == 2 -            then -              if splitUp !! 1 == "pdf" -              then Just (splitUp !! 0) -              else Nothing-            else Nothing-  where splitUp = splitOn "." xs--extractFromArgs :: [[TeXArg]] -> Maybe Text-extractFromArgs (((FixArg (TeXRaw s)):_):_) = Just s-extractFromArgs _ = Nothing--getCommandValue :: String -> LaTeX -> Maybe Text-getCommandValue s = (extractFromArgs . lookForCommand s) ----Converts either to maybe (for use by maybe applicative)---eToM :: Either a a -> Maybe a---eToM e = case e of---   Left _ -> Nothing---   Right d -> (Just d)--eToM :: Maybe (Either l r) -> Maybe r-eToM Nothing = Nothing-eToM (Just (Left _)) = Nothing-eToM (Just (Right r)) = Just r--createPost :: String -> Either ParseError LaTeX -> DateTime -> Maybe Post-createPost _ (Left err) _ = Nothing-createPost s (Right t)  time = Post <$> pure s <*> title <*> author <*> date <*> pure t-  where -    date = eToM (fmap (parseDate time) (unpack <$> (getCommandValue "date" t)))-    author = unpack <$> (getCommandValue "author" t)-    title = unpack <$> (getCommandValue "title" t)--fileNameToPost :: String -> IO (Maybe Post) -fileNameToPost fn = do-  latexFile <- fmap (parseLaTeX . pack) (readFile ("posts/"++fn++".tex"))-  t <- getCurrentDateTime-  return (createPost fn latexFile t)--injectPosts :: String -> Html -> String -injectPosts layout ul = renderTags (beginning ++ parseTags (show ul) ++ end)-  where-    splitFile = splitOn [(TagOpen "ul" [("id","blog-posts")]), (TagClose "ul")] (parseTags layout)-    beginning = splitFile !! 0-    end = splitFile !! 1 --safe indexing?+import BlaTeX  main = do   args <- getArgs@@ -113,12 +12,20 @@     ["build"] -> do       --get all pdf files from directory       putStrLn "Getting directory contents"-      fileNames <- fmap (catMaybes . map getPDF) (getDirectoryContents "posts")+      fileNamesAndBrokenFiles <- fmap (map getPDF) (getDirectoryContents "posts")+      let fileNames = rights fileNamesAndBrokenFiles+      let brokenFiles = lefts fileNamesAndBrokenFiles+      --print brokenFiles       --print fileNames        --turn the list of files into a list of posts       putStrLn "Turning directory contents into posts"-      posts <- fmap (reverse . sort . catMaybes) (mapM fileNameToPost fileNames)+      postsToBeCreated <- (mapM fileNameToPost fileNames)+      let posts = (reverse . sort . rights) postsToBeCreated+      let brokenPosts = lefts postsToBeCreated+      if length (brokenPosts) > 1 +        then print brokenPosts+        else putStrLn "All posts are well formed"       --print posts        --generate a ul from the list of posts@@ -132,13 +39,15 @@        --put the ul into the layout file       putStrLn "Inserting HTML element into layout file"-      let outputFile = layoutFile `injectPosts` generatedHtml--      --put the results into the index.html file-      putStrLn "Writing resulting file into index.html"-      writeFile "index.html" outputFile+      let injectedOutput = layoutFile `injectPosts` generatedHtml+      case injectedOutput of+        (Left e) -> putStrLn e+        (Right s) -> do+          --put the results into the index.html file+          putStrLn "Writing resulting file into index.html"+          writeFile "index.html" s+          putStrLn "Success building!" -      putStrLn "Success building!"      ["init"] -> do       --Create the basic layout file
blatex.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/  name:                blatex-version:             0.1.0.6+version:             0.1.0.7 synopsis:            Blog in LaTeX description:         Static site generator that lets you write your blog in LaTeX and publish it to github pages. description:         Markdown and HTML are the standard tools used to write your every day tech blog with. But they have pretty weak support for embedding mathematical formulas, and are not conducive to writing for an extended period of time. Plus, they aren't even Turing complete! So use BlaTeX to start blogging in LaTeX!@@ -35,6 +35,7 @@   -- hs-source-dirs:         default-language:    Haskell2010   other-modules:       Files+                     , BlaTeX    source-repository head   type:     git