Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Jul 10, 2023
1 parent ed2a6b1 commit 649f3da
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/Text/Markdown/Unlit.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE CPP, OverloadedStrings #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
module Text.Markdown.Unlit (
run
, unlit
Expand Down Expand Up @@ -41,17 +43,20 @@ run args =
-- #line 1 "label"
--
case break (== "-h") args of
(xs, ["-h", fileName, infile, outfile]) ->
readFileUtf8 infile >>= writeFileUtf8 outfile . unlit fileName (mkSelector xs)

(xs, ["-h", infile]) ->
readFileUtf8 infile >>= writeUtf8 stdout . unlit infile (mkSelector xs)

_ -> do
name <- getProgName
hPutStrLn stderr ("usage: " ++ name ++ " [selector] -h SRC CUR DST")
exitFailure
(mkSelector -> selector, "-h" : foo) -> case foo of
[src, cur, dst] -> do
readFileUtf8 cur >>= writeFileUtf8 dst . unlit src selector
[src] -> do
readFileUtf8 src >>= writeUtf8 stdout . unlit src selector
_ -> usage
_ -> usage
where
usage :: IO ()
usage = do
name <- getProgName
hPutStrLn stderr ("usage: " ++ name ++ " [selector] -h SRC CUR DST")
exitFailure

mkSelector :: [String] -> Selector
mkSelector = fromMaybe ("haskell" :&: Not "ignore") . parseSelector . unwords

Expand All @@ -65,10 +70,10 @@ run args =
writeUtf8 handle str = hSetEncoding handle utf8 >> hPutStr handle str

unlit :: FilePath -> Selector -> String -> String
unlit fileName selector = unlines . concatMap formatCB . filter (toP selector . codeBlockClasses) . parse
unlit src selector = unlines . concatMap formatCB . filter (toP selector . codeBlockClasses) . parse
where
formatCB :: CodeBlock -> [String]
formatCB cb = ("#line " ++ show (codeBlockStartLine cb) ++ " " ++ show fileName) : codeBlockContent cb
formatCB cb = ("#line " ++ show (codeBlockStartLine cb) ++ " " ++ show src) : codeBlockContent cb

toP :: Selector -> [String] -> Bool
toP = go
Expand Down

0 comments on commit 649f3da

Please sign in to comment.