packages feed

yesod-markdown 0.6 → 0.7

raw patch · 2 files changed

+42/−48 lines, 2 filesdep +blaze-markupdep +bytestring

Dependencies added: blaze-markup, bytestring

Files

Yesod/Markdown.hs view
@@ -31,26 +31,37 @@   )   where -import Yesod.Form (ToField(..), areq, aopt)-import Yesod.Core (RenderMessage, SomeMessage(..))-import Yesod.Form.Types-import Yesod.Widget (toWidget)-import Text.Hamlet (hamlet, Html)+import Data.Monoid (Monoid)+import Data.String (IsString)+import Data.Text (Text)+import Data.Text.Encoding (decodeUtf8With)+import Data.Text.Encoding.Error (lenientDecode)+ import Database.Persist (PersistField)+import System.Directory (doesFileExist) +import Text.Blaze (ToMarkup (toMarkup)) import Text.Blaze.Html (preEscapedToMarkup)-import Text.Pandoc import Text.HTML.SanitizeXSS (sanitizeBalance)+import Text.Hamlet (hamlet, Html)+import Text.Pandoc -import Data.Monoid      (Monoid)-import Data.String      (IsString)-import System.Directory (doesFileExist)+import Yesod.Core (RenderMessage)+import Yesod.Form (ToField(..), areq, aopt)+import Yesod.Form.Functions (parseHelper)+import Yesod.Form.Types+import Yesod.Widget (toWidget) -import qualified Data.Text as T+import qualified Data.ByteString as B+import qualified Data.Text       as T -newtype Markdown = Markdown String+newtype Markdown = Markdown { unMarkdown :: Text }     deriving (Eq, Ord, Show, Read, PersistField, IsString, Monoid) +instance ToMarkup Markdown where+    -- | Sanitized by default+    toMarkup = markdownToHtml+ instance ToField Markdown master where     toField = areq markdownField @@ -59,76 +70,57 @@  markdownField :: RenderMessage master FormMessage => Field sub master Markdown markdownField = Field-    { fieldParse = \values _ -> blank (Right . Markdown . unlines . lines' . T.unpack) values+    { fieldParse = parseHelper $ Right . Markdown     , fieldView  = \theId name attrs val _isReq -> toWidget         [hamlet|$newline never <textarea id="#{theId}" name="#{name}" *{attrs}>#{either id unMarkdown val} |]-     , fieldEnctype = UrlEncoded-     }--     where-        unMarkdown :: Markdown -> T.Text-        unMarkdown (Markdown s) = T.pack s--        lines' :: String -> [String]-        lines' = map go . lines--        go []        = []-        go ('\r':xs) = go xs-        go (x:xs)    = x : go xs--blank :: (Monad m, RenderMessage master FormMessage)-      => (T.Text -> Either FormMessage a)-      -> [T.Text]-      -> m (Either (SomeMessage master) (Maybe a))-blank _ []     = return $ Right Nothing-blank _ ("":_) = return $ Right Nothing-blank f (x:_)  = return $ either (Left . SomeMessage) (Right . Just) $ f x+    , fieldEnctype = UrlEncoded+    } --- | Converts markdown directly to html using the yesod default option ---   sets and sanitization. markdownToHtml :: Markdown -> Html markdownToHtml = writePandoc yesodDefaultWriterOptions                . parseMarkdown yesodDefaultReaderOptions --- | Same but with no sanitization run+-- | No HTML sanitization markdownToHtmlTrusted :: Markdown -> Html markdownToHtmlTrusted = writePandocTrusted yesodDefaultWriterOptions                       . parseMarkdown yesodDefaultReaderOptions --- | Reads markdown in from the specified file; returns the empty string ---   if the file does not exist+-- | Returns the empty string if the file does not exist markdownFromFile :: FilePath -> IO Markdown markdownFromFile f = do-    exists <- doesFileExist f-    content <- do+    exists  <- doesFileExist f+    content <-         if exists-            then readFile f+            then readFileUtf8 f             else return ""      return $ Markdown content --- | Converts the intermediate Pandoc type to Html. Sanitizes HTML.+    where+        readFileUtf8 :: FilePath -> IO Text+        readFileUtf8 fp = do+            bs <- B.readFile fp+            return $ decodeUtf8With lenientDecode bs+ writePandoc :: WriterOptions -> Pandoc -> Html writePandoc wo = preEscapedToMarkup . sanitizeBalance . T.pack . writeHtmlString wo --- | Skips the sanitization and its required conversion to Text writePandocTrusted :: WriterOptions -> Pandoc -> Html writePandocTrusted wo = preEscapedToMarkup . writeHtmlString wo --- | Parses Markdown into the intermediate Pandoc type parseMarkdown :: ReaderOptions -> Markdown -> Pandoc-parseMarkdown ro (Markdown m) = readMarkdown ro m+parseMarkdown ro = readMarkdown ro . T.unpack . unMarkdown --- | Pandoc defaults, plus Html5, minus WrapText+-- | Defaults plus Html5, minus WrapText yesodDefaultWriterOptions :: WriterOptions yesodDefaultWriterOptions = def   { writerHtml5    = True   , writerWrapText = False   } --- | Pandoc defaults, plus Smart, plus ParseRaw+-- | Defaults plus Smart and ParseRaw yesodDefaultReaderOptions :: ReaderOptions yesodDefaultReaderOptions = def     { readerSmart    = True
yesod-markdown.cabal view
@@ -1,5 +1,5 @@ name:                yesod-markdown-version:             0.6+version:             0.7 synopsis:            Tools for using markdown in a yesod application description:         A subset of pandoc functionality useful for markdown processing in yesod applications homepage:            http://github.com/pbrisbin/yesod-markdown@@ -16,8 +16,10 @@    build-depends: base            >= 4     && < 5                , text            >= 0.11  && < 0.12+               , bytestring      >= 0.9   && < 0.11                , pandoc          >= 1.10  && < 1.11                , blaze-html      >= 0.5   && < 0.6+               , blaze-markup    >= 0.5   && < 0.6                , xss-sanitize    >= 0.3.1 && < 0.4                , directory                , yesod-core      >= 1.1.5 && < 1.2