-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptolemy.hs
72 lines (59 loc) · 1.88 KB
/
ptolemy.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Prelude hiding (getContents)
import Data.ByteString (getContents)
import Data.Text (unpack)
import Data.Text.Encoding (decodeUtf8With)
import Data.Text.Encoding.Error (lenientDecode)
import System.Exit (die)
import System.Random (randomRIO)
main :: IO ()
main = do
contents <- getContents
let entries = process $ unpack $ decodeUtf8With lenientDecode contents
if null entries
then die "no entries found"
else do
index <- randomRIO (0, pred $ length entries)
let [quote, meta, title] = entries !! index
putStrLn $
concat
[ nl
, wrap quote
, nl
, nl
, wrap $
concat
[title, "p", takeWhile (/= ' ') $ drop highlightPrefixLen meta]
, nl
]
folder :: String -> ([[String]], [String]) -> ([[String]], [String])
folder line (blocks, currentBlock)
| line == lineEnd = (blocks, currentBlock)
| line == splitter = (currentBlock : blocks, [])
| otherwise = (blocks, currentBlock ++ [line])
predicate :: [String] -> Bool
predicate [] = False
predicate (x:_) = take prefixLen x /= bookmarkPrefix
process :: String -> [[String]]
process = filter predicate . fst . foldr folder ([], []) . lines
wrapper :: (String, String) -> String -> (String, String)
wrapper (acc, currentLine) word =
let newLine = concat [currentLine, word, " "]
in if length newLine > wrapWidth
then (concat [acc, currentLine, nl], word ++ " ")
else (acc, newLine)
wrap :: String -> String
wrap = uncurry (++) . foldl wrapper ([], []) . words
wrapWidth :: Int
wrapWidth = 80
lineEnd :: String
lineEnd = "\r"
splitter :: String
splitter = (take 10 $ repeat '=') ++ lineEnd
bookmarkPrefix :: String
bookmarkPrefix = "- Your Bookmark"
prefixLen :: Int
prefixLen = length bookmarkPrefix
highlightPrefixLen :: Int
highlightPrefixLen = length "- Your Highlight on Page "
nl :: String
nl = "\n"