git @ Cat's Eye Technologies Falderal / 5da3475
Format based on coalesced lines, not blocks. catseye 10 years ago
2 changed file(s) with 23 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
4242 -- to show what we can do.
4343 --
4444
45 --
46 -- We format by (coalesced) lines, instead of by blocks, because by the time
47 -- the file has been parsed into blocks, some content has been dropped.
48 --
49
4550 formatFile fileName = do
4651 testText <- readFile fileName
4752 lines <- return $ transformLines $ lines testText
48 blocks <- return $ convertLinesToBlocks $ lines
49 outputText <- return $ formatBlocks (formatBlock) blocks
53 outputText <- return $ formatLines (formatLine) lines
5054 putStr outputText
5155
52 formatBlocks formatter blocks = foldl (++) "" (map (formatter) blocks)
56 formatLines formatter lines = foldl (++) "" (map (formatter) lines)
5357
54 formatBlock (Section sectionText) =
55 sectionText ++ "\n"
56 formatBlock (Test literalText inputText expected) =
57 literalText ++ "\n" ++ "| " ++ inputText ++ "\n> " ++ (show expected) ++ "\n"
58 formatLine (TestInput text) =
59 (prefixEachLine "| " text)
60 formatLine (ExpectedResult text) =
61 (prefixEachLine "= " text) ++ "\n"
62 formatLine (ExpectedError text) =
63 (prefixEachLine "? " text) ++ "\n"
64 formatLine (LiteralText text) =
65 (prefixEachLine "" text) ++ "\n"
66 formatLine (QuotedCode text) =
67 (prefixEachLine "> " text) ++ "\n"
68 formatLine (SectionHeading text) =
69 (prefixEachLine "# " text) ++ "\n" -- atx-style for now
70
71 prefixEachLine prefix text =
72 foldl (++) "" (map (\x -> prefix ++ x ++ "\n") (lines text))
134134 (last:coalesceLines lines line)
135135
136136 --
137 -- Convert lines to blocks.
137 -- Convert (coalesced) lines to blocks.
138138 --
139139
140140 convertLinesToBlocks ((LiteralText literalText):(TestInput testText):(ExpectedResult expected):rest) =