summaryrefslogtreecommitdiff
path: root/Pancake.hs
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2017-10-28 03:16:26 +0300
committerdefanor <defanor@uberspace.net>2017-10-28 03:16:26 +0300
commitd2da10d36b857e399e10388ddc6f66850211ec77 (patch)
tree40e98bcc17276200f98c5af7c0de2b64263b9bc9 /Pancake.hs
parentb52d2a395814158319a781d38a49bb0f132c221d (diff)
Improve plaintext rendering
Reuse lineToInlines previously used for Gopher, and adjust fitLines.
Diffstat (limited to 'Pancake.hs')
-rw-r--r--Pancake.hs17
1 files changed, 11 insertions, 6 deletions
diff --git a/Pancake.hs b/Pancake.hs
index e80fa2e..62ef0dd 100644
--- a/Pancake.hs
+++ b/Pancake.hs
@@ -237,12 +237,17 @@ fitLines maxLen inlineBits = map mconcat $ map reverse $ fitWords [] 0 inlineBit
-- a new line
fitWords _ 0 (w:ws) = fitWords [w] (length $ asString w) ws
-- add a word to a line
- fitWords curLine curLen (w:ws) = let wLen = length (asString w) in
- if curLen + wLen <= maxLen
- then fitWords (w:curLine) (curLen + wLen) ws
- else curLine : fitWords [] 0 (case w of
- " " -> ws
- _ -> (w:ws))
+ fitWords curLine curLen (w:ws) = let wLen = length (asString w)
+ spaceAhead = case ws of
+ (" " : _) -> True
+ _ -> False
+ in if curLen + wLen <= maxLen
+ then fitWords (w:curLine) (curLen + wLen) $
+ -- if there's an unnecessary space ahead, skip it
+ if (curLen + wLen + 1 > maxLen && spaceAhead)
+ then tail ws
+ else ws
+ else curLine : fitWords [] 0 (w:ws)
-- end, no words pending
fitWords _ 0 [] = []
-- end, with words pending