
import System.File
import Data.Strings
import Data.List1
import Data.List

selfClosing : List String
selfClosing = ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "param", "meta", "track", "wbr"]

idrisReserved : List String 
idrisReserved = ["data"]

tagType : String
tagType = "List Attribute -> List (DOM V) -> DOM V"

voidTagType : String
voidTagType = "List Attribute -> DOM V"

getTags : IO (Maybe (List String))
getTags = do 
  input <- readFile ("tags.txt")
  case input of 
       Right contents => pure . Just . filter (\line => line /= "") . forget . lines $ contents
       Left err => pure Nothing

header : String 
header = """
  -- This file is automatically generated, do not edit
  module TagList
  import Attributes
  import VNode
  """

makeTag : String -> String 
makeTag tag = let
  tag = trim tag
  tagName = if elem tag idrisReserved 
               then tag ++ "'" 
               else tag
  tagString = "\"" ++ tag ++ "\""

  (tagFunc, tagType) = if elem tag selfClosing 
                          then ("defineVoidTag", voidTagType)
                          else ("defineTag", tagType)
  in 
  unlines ["export", tagName ++ " : " ++ tagType, tagName ++ " = " ++ tagFunc ++ " " ++ tagString, ""]


main : IO () 
main = do 
  Just tags <- getTags
    | Nothing => putStrLn "Failed to read tags"
  let contents = header ++ "\n\n" ++ (unlines . map makeTag $ tags)
  _ <- writeFile "TagList.idr" contents
  pure ()
