pandoc-crossref 0.1.0.1 → 0.1.0.2
raw patch · 3 files changed
+36/−41 lines, 3 files
Files
- pandoc-crossref.cabal +2/−2
- src/References/Refs.hs +22/−19
- test-pandoc-crossref.hs +12/−20
pandoc-crossref.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: pandoc-crossref-version: 0.1.0.1+version: 0.1.0.2 synopsis: Pandoc filter for cross-references description: pandoc-crossref is a pandoc filter for numbering figures, equations, tables and cross-references to them. license: GPL-2@@ -23,7 +23,7 @@ source-repository this type: git location: https://github.com/lierdakil/pandoc-crossref- tag: v0.1.0.0+ tag: v0.1.0.1 executable pandoc-crossref main-is: pandoc-crossref.hs
src/References/Refs.hs view
@@ -7,6 +7,7 @@ import Data.Maybe import Data.Function import qualified Data.Map as M+import Control.Arrow as A import Util.Accessor import References.Types@@ -63,9 +64,9 @@ texcit = RawInline (Format "tex") $ if useCleveref opts then- " \\cref{"++listLabels prefix "" "" cits++"}"+ "\\cref{"++listLabels prefix "" "" cits++"}" else- listLabels prefix " \\ref{" "}" cits+ listLabels prefix "\\ref{" "}" cits p | useCleveref opts = [] | otherwise = getRefPrefix opts prefix @@ -73,7 +74,7 @@ listLabels prefix p s = foldl' joinStr "" . mapMaybe (getLabel prefix) where joinStr acc i | null acc = p++i++s- | otherwise = acc++","++p++i++s+ | otherwise = acc++", "++p++i++s getLabel :: String -> Citation -> Maybe String getLabel prefix Citation{citationId=cid}@@ -84,32 +85,34 @@ replaceRefsOther prefix opts cits = do indices <- mapM (getRefIndex prefix) cits let- indices' = groupBy ((==) `on` fmap fst) (sort indices)+ indices' = groupBy ((==) `on` (fmap fst . fst)) (sort indices) return $ normalizeInlines $ getRefPrefix opts prefix ++ concatMap (makeIndices opts) indices' -getRefIndex :: String -> Citation -> WS (Maybe (Int, Int))-getRefIndex prefix Citation{citationId=cid}+getRefIndex :: String -> Citation -> WS (Maybe (Int, Int), [Inline])+getRefIndex prefix Citation{citationId=cid,citationSuffix=suf} | prefix `isPrefixOf` cid- = gets (fmap refIndex . M.lookup cid . getProp prop)- | otherwise = return Nothing+ = (\x -> (x,suf)) `fmap` gets (fmap refIndex . M.lookup cid . getProp prop)+ | otherwise = return (Nothing, suf) where prop = lookupUnsafe prefix accMap -makeIndices :: Options -> [Maybe (Int,Int)] -> [Inline]-makeIndices _ s | any isNothing s = [Str "??"]-makeIndices o s = intercalate sep $ reverse $ map f $ foldl' f2 [] $ catMaybes s+makeIndices :: Options -> [(Maybe (Int, Int), [Inline])] -> [Inline]+makeIndices _ s | any (isNothing . fst) s = [Strong [Str "??"]]+makeIndices o s = intercalate sep $ reverse $ map f $ foldl' f2 [] $ map (A.first fromJust) $ filter (isJust . fst) s where- f2 [] i = [[i]]- f2 ([]:xs) i = [i]:xs- f2 l@(x@((_,hx):_):xs) i@(_,ni)+ f2 [] (i,suf) = [[(i,suf)]]+ f2 ([]:xs) (i,suf) = [(i,suf)]:xs+ f2 l@(x@(((_,hx),sufp):_):xs) (i@(_,ni),suf)+ | not (null suf) || not (null sufp) = [(i,suf)]:l | ni-hx == 0 = l -- remove duplicates- | ni-hx == 1 = (i:x):xs -- group sequental- | otherwise = [i]:l -- new group+ | ni-hx == 1 = ((i,[]):x):xs -- group sequental+ | otherwise = [(i,[])]:l -- new group f [] = [] -- drop empty lists f [w] = show' w -- single value f [w1,w2] = show' w2 ++ sep ++ show' w1 -- two values f (x:xs) = show' (last xs) ++ rangeDelim o ++ show' x -- shorten more than two values sep = [Str ", "]- show' (c,n) = if sepChapters o && c>0- then [Str $ show c] ++ chapDelim o ++ [Str $ show n]- else [Str $ show n]+ show' ((c,n),suf) = (if sepChapters o && c>0+ then [Str $ show c] ++ chapDelim o ++ [Str $ show n]+ else [Str $ show n])+ ++ suf
test-pandoc-crossref.hs view
@@ -3,7 +3,6 @@ import Text.Pandoc.Builder import Text.Pandoc.Walk import Text.Pandoc.Generic-import Text.Pandoc.Shared (normalizeInlines) import Util.Options import Control.Monad.State import References.Types@@ -16,7 +15,6 @@ import qualified Util.Template import qualified Data.Map as M import Data.Monoid-import Data.List (intersperse) main :: IO () main = hspec $ do@@ -36,27 +34,27 @@ describe "References.Refs.replaceRefs" $ do it "References one image" $- testRefs' "fig:" [1] [4] imgRefs' "fig. 4"+ testRefs' "fig:" [1] [4] imgRefs' "fig.\160\&4" it "References multiple images" $- testRefs' "fig:" [1..3] [4..6] imgRefs' "fig. 4-6"+ testRefs' "fig:" [1..3] [4..6] imgRefs' "fig.\160\&4-6" it "References one equation" $- testRefs' "eq:" [1] [4] eqnRefs' "eq. 4"+ testRefs' "eq:" [1] [4] eqnRefs' "eq.\160\&4" it "References multiple equations" $- testRefs' "eq:" [1..3] [4..6] eqnRefs' "eq. 4-6"+ testRefs' "eq:" [1..3] [4..6] eqnRefs' "eq.\160\&4-6" it "References one table" $- testRefs' "tbl:" [1] [4] tblRefs' "tbl. 4"+ testRefs' "tbl:" [1] [4] tblRefs' "tbl.\160\&4" it "References multiple tables" $- testRefs' "tbl:" [1..3] [4..6] tblRefs' "tbl. 4-6"+ testRefs' "tbl:" [1..3] [4..6] tblRefs' "tbl.\160\&4-6" describe "References.List.listOf" $ do it "Generates list of tables" $ testList (para $ rawInline "tex" "\\listoftables") def{tblRefs=M.fromList $ refRec' "tbl:1" 4 "4" <> refRec' "tbl:2" 5 "5" <> refRec' "tbl:3" 6 "6"}- (header 1 (str' "List of Tables") <> orderedList ((plain . str . show) `map` [4..6 :: Int]))+ (header 1 (text "List of Tables") <> orderedList ((plain . str . show) `map` [4..6 :: Int])) it "Generates list of figures" $ testList (para $ rawInline "tex" "\\listoffigures") def{imgRefs=M.fromList $ refRec' "fig:1" 4 "4" <> refRec' "fig:2" 5 "5" <> refRec' "fig:3" 6 "6"}- (header 1 (str' "List of Figures") <> orderedList ((plain . str . show) `map` [4..6 :: Int]))+ (header 1 (text "List of Figures") <> orderedList ((plain . str . show) `map` [4..6 :: Int])) describe "Util.Template" $ it "Applies templates" $@@ -71,13 +69,13 @@ refGen p l1 l2 = M.fromList $ mconcat $ zipWith refRec'' (((p++) . show) `map` l1) l2 refRec' :: String -> Int -> String -> [(String, RefRec)]-refRec' ref i tit = [(ref, RefRec{refIndex=(0,i),refTitle=toList $ str' tit})]+refRec' ref i tit = [(ref, RefRec{refIndex=(0,i),refTitle=toList $ text tit})] refRec'' :: String -> Int -> [(String, RefRec)] refRec'' ref i = refRec' ref i [] testRefs' :: String -> [Int] -> [Int] -> Accessor References (M.Map String RefRec) -> String -> Expectation-testRefs' p l1 l2 prop res = testRefs (para $ citeGen p l1) (setProp prop (refGen p l1 l2) def) (para $ str'' res)+testRefs' p l1 l2 prop res = testRefs (para $ citeGen p l1) (setProp prop (refGen p l1 l2) def) (para $ text res) testBlocks :: Blocks -> (Blocks, References) -> Expectation testBlocks arg res = runState (walkM (f defaultOptions) arg) def `shouldBe` res@@ -90,25 +88,19 @@ testList bs st res = runState (bottomUpM (References.List.listOf defaultOptions) (toList bs)) st `shouldBe` (toList res,st) figure :: String -> String -> String -> String -> Blocks-figure src title alt ref = para (image src title (str' alt) <> ref' "fig" ref)+figure src title alt ref = para (image src title (text alt) <> ref' "fig" ref) equation :: String -> String -> Blocks equation eq ref = para (displayMath eq <> ref' "eq" ref) table' :: String -> String -> Blocks-table' title ref = table (str' title <> ref' "tbl" ref) []+table' title ref = table (text title <> ref' "tbl" ref) [] [para $ str "H1", para $ str "H2"] [[para $ str "C1", para $ str "C2"]] ref' :: String -> String -> Inlines ref' p n | null n = mempty | otherwise = space <> str ("{#"++p++":"++n++"}")--str' :: String -> Inlines-str' s = fromList $ intersperse Space $ Str `map` words s--str'' :: String -> Inlines-str'' s = fromList $ normalizeInlines $ intersperse (Str "\160") $ Str `map` words s defaultOptions :: Options defaultOptions = getOptions defaultMeta Nothing