Skip to content

Commit

Permalink
Div-wrapping for better compatibility with other filters
Browse files Browse the repository at this point in the history
  • Loading branch information
lierdakil committed Aug 19, 2015
1 parent 56c3cd8 commit 43f904e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pandoc-crossref.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ go fmt (Pandoc meta bs) = do
dtv <- getSettings meta
let
doWalk =
bottomUpM (codeBlockCaptions opts) bs
bottomUpM (codeBlockCaptions opts) (walk divBlocks bs)
>>= walkM (replaceBlocks opts)
>>= bottomUpM (replaceRefs opts)
>>= bottomUpM (listOf opts)
Expand Down
27 changes: 20 additions & 7 deletions src/References/Blocks.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module References.Blocks (replaceBlocks) where
module References.Blocks (divBlocks, replaceBlocks) where

import Text.Pandoc.Definition
import Text.Pandoc.Builder (text, toList)
Expand Down Expand Up @@ -30,17 +30,17 @@ replaceBlocks opts (Header n (label, cls, attrs) text')
in r{curChap=cc'}
when ("sec:" `isPrefixOf` label') $ replaceAttrSec label' text' secRefs'
return $ Header n (label', cls, attrs) text'
replaceBlocks opts (Para (Image alt img:c))
| Just label <- getRefLabel "fig" c
replaceBlocks opts (Div (label,_,_) [Plain [Image alt img]])
| "fig:" `isPrefixOf` label
= do
idxStr <- replaceAttr opts label alt imgRefs'
let alt' = case outFormat opts of
f | isFormat "latex" f ->
RawInline (Format "tex") ("\\label{"++label++"}") : alt
_ -> applyTemplate idxStr alt $ figureTemplate opts
return $ Para [Image alt' (fst img,"fig:")]
replaceBlocks opts (Para (Math DisplayMath eq:c))
| Just label <- getRefLabel "eq" c
replaceBlocks opts (Div (label,_,_) [Plain [Math DisplayMath eq]])
| "eq:" `isPrefixOf` label
= case outFormat opts of
f | isFormat "latex" f ->
let eqn = "\\begin{equation}"++eq++"\\label{"++label++"}\\end{equation}"
Expand All @@ -49,9 +49,9 @@ replaceBlocks opts (Para (Math DisplayMath eq:c))
idxStr <- replaceAttr opts label [] eqnRefs'
let eq' = eq++"\\qquad("++stringify idxStr++")"
return $ Para [Math DisplayMath eq']
replaceBlocks opts (Table title align widths header cells)
replaceBlocks opts (Div (label,_,_) [Table title align widths header cells])
| not $ null title
, Just label <- getRefLabel "tbl" [last title]
, "tbl:" `isPrefixOf` label
= do
idxStr <- replaceAttr opts label (init title) tblRefs'
let title' =
Expand Down Expand Up @@ -111,6 +111,19 @@ replaceBlocks opts
]
replaceBlocks _ x = return x

divBlocks :: Block -> Block
divBlocks (Para (Image alt img:c))
| Just label <- getRefLabel "fig" c
= Div (label,[],[]) [Plain [Image alt (fst img, "fig:")]]
divBlocks (Para (math@(Math DisplayMath _eq):c))
| Just label <- getRefLabel "eq" c
= Div (label,[],[]) [Plain [math]]
divBlocks (table@(Table title _align _widths _header _cells))
| not $ null title
, Just label <- getRefLabel "tbl" [last title]
= Div (label,[],[]) [table]
divBlocks x = x

getRefLabel :: String -> [Inline] -> Maybe String
getRefLabel _ [] = Nothing
getRefLabel tag ils
Expand Down
2 changes: 1 addition & 1 deletion test-pandoc-crossref.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ testRefs'' p l1 l2 prop res = testRefs (para $ citeGen p l1) (setProp prop (refG

testBlocks :: Blocks -> (Blocks, References) -> Expectation
testBlocks arg res = runState (walkM (f defaultOptions) arg) def `shouldBe` res
where f = References.Blocks.replaceBlocks
where f = (. References.Blocks.divBlocks) . References.Blocks.replaceBlocks

testRefs :: Blocks -> References -> Blocks -> Expectation
testRefs bs st res = runState (bottomUpM (References.Refs.replaceRefs defaultOptions) (toList bs)) st `shouldBe` (toList res,st)
Expand Down

0 comments on commit 43f904e

Please sign in to comment.