Skip to content

Commit

Permalink
feat (IO.Markdown): added warning if file could not be fully parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
smallhadroncollider committed Feb 19, 2018
1 parent fc35852 commit 87d4887
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
8 changes: 7 additions & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ main :: IO ()
main = do
config <- setup
(exists, path) <- T.exists config
when exists $ create path <$> T.readFile config path >>= go config

when exists $ do
content <- T.readFile config path

case content of
Right lists -> go config $ create path lists
Left err -> putStrLn $ path ++ ": " ++ err
2 changes: 1 addition & 1 deletion roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
## In Progress

- One bad config line stops all config from working - needs to merge with defaultConfig
- Display a warning if any line of the file could not be parsed - otherwise could lead to data loss

## Done

Expand Down Expand Up @@ -139,3 +138,4 @@
* ~Change sub-list: e.g. from " *" to "-"~
- Feels sluggish in sub-task view - cache main view?
- Leaving search only refreshes current list
- Display a warning if any line of the file could not be parsed - otherwise could lead to data loss
38 changes: 25 additions & 13 deletions src/IO/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module IO.Markdown (
trimListItem
) where

import Data.Text (Text, drop, append, null, lines, isPrefixOf, strip, dropAround, snoc)
import Data.Text as T (Text, drop, append, null, lines, isPrefixOf, strip, dropAround, snoc)
import Data.List (intercalate)
import Data.Text.Encoding (encodeUtf8, decodeUtf8With)

import Data.Taskell.Lists (Lists, newList, appendToLast)
Expand All @@ -20,16 +21,16 @@ import IO.Config (Config, MarkdownConfig, markdown, titleOutput, taskOutput, sub

-- parse code
trimListItem :: Text -> Text
trimListItem = strip . Data.Text.drop 1
trimListItem = strip . T.drop 1

trimTitle :: Text -> Text
trimTitle = strip . Data.Text.drop 2
trimTitle = strip . T.drop 2

trimTask :: Text -> Task
trimTask = new . trimListItem

trimTilde :: Text -> Text
trimTilde = strip . Data.Text.dropAround (== '~')
trimTilde = strip . T.dropAround (== '~')

addSubItem :: Text -> Lists -> Lists
addSubItem t ls = adjust' updateList i ls
Expand All @@ -39,21 +40,32 @@ addSubItem t ls = adjust' updateList i ls
updateList l = updateFn j (addSubTask st) l
where j = count l - 1

start :: MarkdownConfig -> Lists -> Text -> Lists
start config ls s | titleOutput config `snoc` ' ' `isPrefixOf` s = newList (trimTitle s) ls
| taskOutput config `snoc` ' ' `isPrefixOf` s = appendToLast (trimTask s) ls
| subtaskOutput config `snoc` ' ' `isPrefixOf` s = addSubItem (trimListItem $ strip s) ls
| otherwise = ls
start :: MarkdownConfig -> (Lists, [Int]) -> (Text, Int) -> (Lists, [Int])
start config (ls, errs) (s, li)
| titleOutput config `snoc` ' ' `isPrefixOf` s = (newList (trimTitle s) ls, errs)
| taskOutput config `snoc` ' ' `isPrefixOf` s = (appendToLast (trimTask s) ls, errs)
| subtaskOutput config `snoc` ' ' `isPrefixOf` s = (addSubItem (trimListItem $ strip s) ls, errs)
| not (T.null (strip s)) = (ls, errs ++ [li])
| otherwise = (ls, errs)

decodeError :: String -> Maybe Word8 -> Maybe Char
decodeError _ _ = Just '\65533'

parse :: Config -> ByteString -> Lists
parse config s = foldl' (start (markdown config)) empty $ Data.Text.lines $ decodeUtf8With decodeError s
parse :: Config -> ByteString -> Either String Lists
parse config s = do
let lns = T.lines $ decodeUtf8With decodeError s
let fn = start (markdown config)
let acc = (empty, [])
let (lists, errs) = foldl' fn acc $ zip lns [1..]

if Prelude.null errs
then Right lists
else Left $ "could not parse line(s) " ++ intercalate ", " (show <$> errs)


-- stringify code
join :: Text -> [Text] -> Text
join = foldl' Data.Text.append
join = foldl' T.append

subTaskToString :: MarkdownConfig -> Text -> SubTask -> Text
subTaskToString config t st = join t [
Expand All @@ -77,7 +89,7 @@ taskToString config s t = join s [

listToString :: MarkdownConfig -> Text -> List -> Text
listToString config s l = join s [
if Data.Text.null s then "" else "\n"
if T.null s then "" else "\n"
, titleOutput config
, " "
, title l
Expand Down
2 changes: 1 addition & 1 deletion src/IO/Taskell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ writeFile :: Config -> Lists -> FilePath -> IO ()
writeFile config tasks path = void (BS.writeFile path $ stringify config tasks)

-- reads json file
readFile :: Config -> FilePath -> IO Lists
readFile :: Config -> FilePath -> IO (Either String Lists)
readFile config path = do
content <- BS.readFile path
return $ parse config content

0 comments on commit 87d4887

Please sign in to comment.