git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: move combining logic to Reducible

Introduces a new function in Reducibles, concatR. The idea is that if we have two list of Reducibles (blocks or inlines), we can combine them and just perform the reduction on the joining parts (the last element of the first list, the first element of the second list). This is useful in cases where the two lists are already reduced, and we're only worried about the joining elements. This actually improves the efficiency a bit further, because concatR can be smart about empty lists.

commit9d0b390d48823e2fcbcbabf84fd105b595bcb60b authorJesse Rosenthal <jrosenthal@jhu.edu> dateTue 12 Aug 2014, 10:26 (12 years ago) parente4a8e4a
2 files changed+11−3
src/Text/Pandoc/Readers/Docx.hs+1−3 src/Text/Pandoc/Readers/Docx/Reducible.hs+10
src/Text/Pandoc/Readers/Docx.hs+1−3
@@ -462,9 +462,7 @@ bodyPartToBlocks (Paragraph pPr parparts)
462462bodyPartToBlocks (Paragraph pPr parparts) = do
463463 ils <- parPartsToInlines parparts >>= (return . normalizeSpaces)
464464 dropIls <- gets docxDropCap
465− let ils' = case ils of
466− (x:xs) -> reduceList (dropIls ++ [x]) ++ xs
467− [] -> dropIls
465+ let ils' = concatR dropIls ils
468466 if dropCap pPr
469467 then do modify $ \s -> s { docxDropCap = ils' }
470468 return []
src/Text/Pandoc/Readers/Docx/Reducible.hs+10
@@ -39,6 +39,7 @@ module Text.Pandoc.Readers.Docx.Reducible ((<++>),
3939 innards,
4040 reduceList,
4141 reduceListB,
42+ concatR,
4243 rebuild)
4344 where
4445
@@ -78,6 +79,15 @@ reduceList' as (x:xs) = reduceList' (init as ++ (last as <++> x) ) xs
7879reduceList :: (Reducible a) => [a] -> [a]
7980reduceList = reduceList' []
8081
82+concatR :: (Reducible a) => [a] -> [a] -> [a]
83+concatR [] [] = []
84+concatR [] ss = ss
85+concatR rs [] = rs
86+concatR rs ss = let (x:xs) = reverse rs
87+ (y:ys) = ss
88+ in
89+ reverse xs ++ ( x <++> y ) ++ ys
90+
8191combineReducibles :: (Reducible a, Eq a) => a -> a -> [a]
8292combineReducibles r s =
8393 let (conts, rs) = topLevelContainers r