@@ -16,7 +16,7 @@ import Data.Monoid ((<>))
1616import Data.Text hiding (cons )
1717import GHC.IO.Handle (Handle )
1818import System.Console.ANSI
19- import Tldr.Types (ConsoleSetting (.. ))
19+ import Tldr.Types (ConsoleSetting (.. ), ColorSetting ( .. ) )
2020import qualified Data.Text as T
2121import qualified Data.Text.IO as TIO
2222
@@ -35,14 +35,17 @@ defConsoleSetting =
3535headingSetting :: ConsoleSetting
3636headingSetting = defConsoleSetting {consoleIntensity = BoldIntensity }
3737
38- toSGR :: ConsoleSetting -> [SGR ]
39- toSGR cons =
40- [ SetItalicized (italic cons)
41- , SetConsoleIntensity (consoleIntensity cons)
42- , SetUnderlining (underline cons)
43- , SetBlinkSpeed (blink cons)
44- , SetColor Foreground (fgIntensity cons) (fgColor cons)
45- ]
38+ toSGR :: ColorSetting -> ConsoleSetting -> [SGR ]
39+ toSGR color cons = case color of
40+ NoColor -> def
41+ UseColor -> SetColor Foreground (fgIntensity cons) (fgColor cons) : def
42+ where
43+ def =
44+ [ SetItalicized (italic cons)
45+ , SetConsoleIntensity (consoleIntensity cons)
46+ , SetUnderlining (underline cons)
47+ , SetBlinkSpeed (blink cons)
48+ ]
4649
4750renderNode :: NodeType -> Handle -> IO ()
4851renderNode (TEXT txt) handle = TIO. hPutStrLn handle (txt <> " \n " )
@@ -54,13 +57,13 @@ renderNode LINEBREAK handle = TIO.hPutStrLn handle ""
5457renderNode (LIST _) handle = TIO. hPutStrLn handle " " >> TIO. hPutStr handle " - "
5558renderNode _ _ = return ()
5659
57- changeConsoleSetting :: NodeType -> IO ()
58- changeConsoleSetting (HEADING _) = setSGR $ toSGR headingSetting
59- changeConsoleSetting BLOCK_QUOTE = setSGR $ toSGR headingSetting
60- changeConsoleSetting ITEM = setSGR $ toSGR $ defConsoleSetting {fgColor = Green }
61- changeConsoleSetting (CODE _) =
62- setSGR $ toSGR $ defConsoleSetting {fgColor = Yellow }
63- changeConsoleSetting _ = return ()
60+ changeConsoleSetting :: ColorSetting -> NodeType -> IO ()
61+ changeConsoleSetting color (HEADING _) = setSGR $ toSGR color headingSetting
62+ changeConsoleSetting color BLOCK_QUOTE = setSGR $ toSGR color headingSetting
63+ changeConsoleSetting color ITEM = setSGR $ toSGR color $ defConsoleSetting {fgColor = Green }
64+ changeConsoleSetting color (CODE _) =
65+ setSGR $ toSGR color $ defConsoleSetting {fgColor = Yellow }
66+ changeConsoleSetting _ _ = return ()
6467
6568handleSubsetNodeType :: NodeType -> Text
6669handleSubsetNodeType (HTML_BLOCK txt) = txt
@@ -79,16 +82,16 @@ handleParagraph :: [Node] -> Handle -> IO ()
7982handleParagraph xs handle =
8083 TIO. hPutStrLn handle $ T. concat $ Prelude. map handleSubsetNode xs
8184
82- handleNode :: Node -> Handle -> IO ()
83- handleNode (Node _ PARAGRAPH xs) handle = handleParagraph xs handle
84- handleNode (Node _ ITEM xs) handle =
85- changeConsoleSetting ITEM >> handleParagraph xs handle
86- handleNode (Node _ ntype xs) handle = do
87- changeConsoleSetting ntype
85+ handleNode :: Node -> Handle -> ColorSetting -> IO ()
86+ handleNode (Node _ PARAGRAPH xs) handle _ = handleParagraph xs handle
87+ handleNode (Node _ ITEM xs) handle color =
88+ changeConsoleSetting color ITEM >> handleParagraph xs handle
89+ handleNode (Node _ ntype xs) handle color = do
90+ changeConsoleSetting color ntype
8891 renderNode ntype handle
8992 mapM_
9093 (\ (Node _ ntype' ns) ->
91- renderNode ntype' handle >> mapM_ (` handleNode` handle) ns)
94+ renderNode ntype' handle >> mapM_ (\ n -> handleNode n handle color ) ns)
9295 xs
9396 setSGR [Reset ]
9497
@@ -98,7 +101,7 @@ parsePage fname = do
98101 let node = commonmarkToNode [] page
99102 return node
100103
101- renderPage :: FilePath -> Handle -> IO ()
102- renderPage fname handle = do
104+ renderPage :: FilePath -> Handle -> ColorSetting -> IO ()
105+ renderPage fname handle color = do
103106 node <- parsePage fname
104- handleNode node handle
107+ handleNode node handle color
0 commit comments