@@ -1,11 +1,15 @@
1−module Text.Blocks
1+module Text.Pandoc.Blocks
3 docToBlock,
4 docToBlock,
4 blockToDoc,
5 blockToDoc,
5 widthOfBlock,
6 widthOfBlock,
6 heightOfBlock,
7 heightOfBlock,
7 hcatBlocks,
8 hcatBlocks,
8− hsepBlocks
9+ hsepBlocks,
@@ -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
37widthOfBlock :: TextBlock -> Int
41widthOfBlock :: TextBlock -> Int
38widthOfBlock (TextBlock width _ _) = width
42widthOfBlock (TextBlock width _ _) = width
@@ -61,3 +65,28 @@ hcatBlocks ((TextBlock width1 height1 lns1):xs) =
62hsepBlocks = hcatBlocks . (intersperse (TextBlock 1 1 [" "]))
66hsepBlocks = hcatBlocks . (intersperse (TextBlock 1 1 [" "]))
68+isWhitespace x = x `elem` " \t"
70+leftAlignBlock :: TextBlock -> TextBlock
71+leftAlignBlock (TextBlock width height lns) =
72+ TextBlock width height $
73+ map (dropWhile isWhitespace) lns
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
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