Skip to content

Instantly share code, notes, and snippets.

@StephenWakely
Created January 31, 2019 11:27
Show Gist options
  • Save StephenWakely/e46d78db1b5f5fb13135c42c1bd9f6fa to your computer and use it in GitHub Desktop.
Save StephenWakely/e46d78db1b5f5fb13135c42c1bd9f6fa to your computer and use it in GitHub Desktop.
Generate purescript file from fontawesome
{-# LANGUAGE OverloadedStrings #-}
module Lib
( someFunc
) where
import System.IO
import Data.Char
import Data.Text (Text)
import qualified Data.Text as T
import Data.List
functionName :: Text -> Text
functionName class_ = join $ T.splitOn "-" class_
where
join (x:xs) = x <> foldMap T.toTitle xs
appendHeader :: [Text] -> [Text]
appendHeader body = [ "-- | Font awesome icons"
, "-- | Generated by a tool"
, "module Control.FontAwesome where"
, ""
, "import Prelude"
, "import Halogen as H"
, "import Halogen.HTML as HH"
, "import Halogen.HTML.Properties as HP"
, "" ] <> body
makeClass :: Text -> Text
makeClass class_ =
let
name = functionName class_
in
name <> " ∷ ∀ p i. H.HTML p i\n" <>
name <> " = HH.i [ HP.class_ $ HH.ClassName \"fa " <> class_ <> "\" ] []\n"
isClassLine :: Text -> Bool
isClassLine line =
".fa-" `T.isPrefixOf` line &&
":before" `T.isInfixOf` line
extractClassNames :: [Text] -> [Text]
extractClassNames lines =
getName <$> filter isClassLine lines
where
getName :: Text -> Text
getName class_ = T.takeWhile (/= ':') $ T.drop 1 class_
main :: IO ()
main = do
contents <- readFile "./font-awesome.css"
writeFile "./FontAwesome.purs" $
T.unpack $
T.unlines $
appendHeader $
map makeClass $
extractClassNames $
T.lines $
T.pack $
contents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment