git.iko.soy/typeable/pandoc
on master help sign in
Improvements/bug fixes to Text.Pandoc.Blocks
8d776e1fiddlosopher
src/Text/Pandoc/Blocks.hs+32−3
@@ -1,11 +1,15 @@
1−module Text.Blocks
1+module Text.Pandoc.Blocks
2 (
2 (
3+ TextBlock (..),
3 docToBlock,
4 docToBlock,
4 blockToDoc,
5 blockToDoc,
5 widthOfBlock,
6 widthOfBlock,
6 heightOfBlock,
7 heightOfBlock,
7 hcatBlocks,
8 hcatBlocks,
8− hsepBlocks
9+ hsepBlocks,
10+ centerAlignBlock,
11+ leftAlignBlock,
12+ rightAlignBlock
9 )
13 )
10where
14where
11
15
@@ -32,7 +36,7 @@ blockToDoc :: TextBlock -> Doc
32blockToDoc (TextBlock _ _ lns) =
36blockToDoc (TextBlock _ _ lns) =
33 if null lns
37 if null lns
34 then empty
38 then empty
35− else text $ unlines (init lns) ++ last lns -- to avoid trailing \n
39+ else vcat $ map text lns
36
40
37widthOfBlock :: TextBlock -> Int
41widthOfBlock :: TextBlock -> Int
38widthOfBlock (TextBlock width _ _) = width
42widthOfBlock (TextBlock width _ _) = width
@@ -61,3 +65,28 @@ hcatBlocks ((TextBlock width1 height1 lns1):xs) =
61
65
62hsepBlocks = hcatBlocks . (intersperse (TextBlock 1 1 [" "]))
66hsepBlocks = hcatBlocks . (intersperse (TextBlock 1 1 [" "]))
63
67
68+isWhitespace x = x `elem` " \t"
69+
70+leftAlignBlock :: TextBlock -> TextBlock
71+leftAlignBlock (TextBlock width height lns) =
72+ TextBlock width height $
73+ map (dropWhile isWhitespace) lns
74+
75+rightAlignBlock :: TextBlock -> TextBlock
76+rightAlignBlock (TextBlock width height lns) =
77+ let rightAlignLine ln =
78+ let (spaces, rest) = span isWhitespace $ reverse $ hPad width ln
79+ in reverse (rest ++ spaces)
80+ in TextBlock width height $ map rightAlignLine lns
81+
82+centerAlignBlock :: TextBlock -> TextBlock
83+centerAlignBlock (TextBlock width height lns) =
84+ let centerAlignLine ln =
85+ let ln' = hPad width ln
86+ (startSpaces, rest) = span isWhitespace ln'
87+ endSpaces = takeWhile isWhitespace (reverse ln')
88+ numSpaces = length (startSpaces ++ endSpaces)
89+ startSpaces' = replicate (quot numSpaces 2) ' '
90+ in startSpaces' ++ rest
91+ in TextBlock width height $ map centerAlignLine lns
92+