packages feed

pandoc-include-code 1.0.1.0 → 1.1.0.0

raw patch · 2 files changed

+43/−31 lines, 2 filesdep +text

Dependencies added: text

Files

pandoc-include-code.cabal view
@@ -8,7 +8,7 @@ author:              Oskar Wickström maintainer:          Oskar Wickström homepage:	           https://github.com/owickstrom/pandoc-include-code-version:             1.0.1.0+version:             1.1.0.0 cabal-version:       >= 1.8 build-type:          Simple category:            Documentation@@ -27,6 +27,7 @@                    , unordered-containers >= 0.2      && < 0.3                    , process                    , filepath+                   , text                 >= 1.2      && < 1.3                    , mtl                  >= 2.2      && < 3                    , pandoc-types         >= 1.12     && < 1.18 
src/Text/Pandoc/Filter/IncludeCode.hs view
@@ -3,6 +3,7 @@ {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase                 #-} {-# LANGUAGE NamedFieldPuns             #-}+{-# LANGUAGE OverloadedStrings          #-} {-# LANGUAGE RecordWildCards            #-}  module Text.Pandoc.Filter.IncludeCode@@ -13,21 +14,20 @@ import           Control.Applicative import           Data.Monoid #endif-import           Control.Exception+ import           Control.Monad.Except import           Control.Monad.Reader import           Data.Char            (isSpace) import           Data.HashMap.Strict  (HashMap) import qualified Data.HashMap.Strict  as HM import           Data.List            (isInfixOf)-import           Data.Maybe           (fromMaybe)+import           Data.Text            (Text)+import qualified Data.Text            as Text+import qualified Data.Text.IO         as Text import           Text.Pandoc.JSON import           Text.Read            (readMaybe) -data Range = Range-  { startLine :: Int-  , endLine   :: Int-  }+data Range = Range Int Int  mkRange :: Int -> Int -> Maybe Range mkRange s e@@ -36,7 +36,7 @@  data InclusionSpec = InclusionSpec   { include :: FilePath-  , snippet :: Maybe String+  , snippet :: Maybe Text   , range   :: Maybe Range   , dedent  :: Maybe Int   }@@ -75,7 +75,7 @@     Nothing -> return Nothing   where     lookupInt name = HM.lookup name attrs >>= readMaybe-    snippet = HM.lookup "snippet" attrs+    snippet = Text.pack <$> HM.lookup "snippet" attrs     dedent = lookupInt "dedent"     getRange =       case (lookupInt "startLine", lookupInt "endLine") of@@ -88,10 +88,10 @@         (Just _, Nothing) -> throwError (IncompleteRange End)         (Nothing, Nothing) -> return Nothing -type Lines = [String]+type Lines = [Text] -readIncluded :: Inclusion String-readIncluded = liftIO . readFile =<< asks include+readIncluded :: Inclusion Text+readIncluded = liftIO . Text.readFile =<< asks include  filterLineRange :: Lines -> Inclusion Lines filterLineRange ls =@@ -101,6 +101,14 @@       where startIndex = pred start     Nothing -> return ls +isSnippetTag :: Text -> Text -> Text -> Bool+isSnippetTag tag name line =+  mconcat [tag, " snippet ", name] `Text.isSuffixOf` Text.strip line++isSnippetStart, isSnippetEnd :: Text -> Text -> Bool+isSnippetStart = isSnippetTag "start"+isSnippetEnd = isSnippetTag "end"+ onlySnippet :: Lines -> Inclusion Lines onlySnippet ls = do   s <- asks snippet@@ -108,11 +116,7 @@     Just name ->       return $       drop 1 $-      takeWhile (not . isSnippetEnd) $ dropWhile (not . isSnippetStart) ls-      where isSnippetTag tag line =-              (tag ++ " snippet " ++ name) `isInfixOf` line-            isSnippetStart = isSnippetTag "start"-            isSnippetEnd = isSnippetTag "end"+      takeWhile (not . isSnippetEnd name) $ dropWhile (not . isSnippetStart name) ls     Nothing -> return ls  dedentLines :: Lines -> Inclusion Lines@@ -123,10 +127,12 @@     Nothing -> return ls   where     dedentLine 0 line = line-    dedentLine _ "" = ""-    dedentLine n (c:cs)-      | isSpace c = dedentLine (pred n) cs-      | otherwise = c : cs+    dedentLine n line =+      case Text.uncons line of+        Just (c, cs)+          | isSpace c -> dedentLine (pred n) cs+          | otherwise -> Text.cons c cs+        Nothing -> ""  filterAttributes :: [(String, String)] -> [(String, String)] filterAttributes = filter nonFilterAttribute@@ -144,25 +150,30 @@         IncompleteRange Start -> "Incomplete range: \"startLine\" is missing"         IncompleteRange End -> "Incomplete range: \"endLine\" is missing" -splitLines :: String -> Inclusion Lines-splitLines = return . lines+splitLines :: Text -> Inclusion Lines+splitLines = return . Text.lines -joinLines :: Lines -> Inclusion String-joinLines = return . unlines+joinLines :: Lines -> Inclusion Text+joinLines = return . Text.unlines +allSteps :: Inclusion Text+allSteps =+  readIncluded+  >>= splitLines+  >>= filterLineRange+  >>= onlySnippet+  >>= dedentLines+  >>= joinLines+ -- | A Pandoc filter that includes code snippets from external files. includeCode :: Maybe Format -> Block -> IO Block includeCode _ cb@(CodeBlock (id', classes, attrs) _) =   case parseInclusion (HM.fromList attrs) of     Right (Just spec) ->-      runInclusion'-        spec-        (readIncluded >>= splitLines >>= filterLineRange >>= onlySnippet >>=-         dedentLines >>=-         joinLines) >>= \case+      runInclusion' spec allSteps >>= \case         Left err -> printAndFail err         Right contents ->-          return (CodeBlock (id', classes, filterAttributes attrs) contents)+          return (CodeBlock (id', classes, filterAttributes attrs) (Text.unpack contents))     Right Nothing -> return cb     Left err -> printAndFail err includeCode _ x = return x