You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.5 KiB

2 years ago
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Main where
  3. import qualified Data.Text.Lazy.IO as Text
  4. import qualified Data.Text.Lazy as Text
  5. import Data.Text.Lazy (Text)
  6. import Text.HTML.TagSoup
  7. import System.Environment
  8. import Control.Exception
  9. import Data.Maybe
  10. main :: IO ()
  11. main = do
  12. [file] <- getArgs
  13. tags <- parseTags <$> Text.readFile file
  14. evaluate (length tags)
  15. Text.writeFile file (renderTags (hideSteps False tags))
  16. hideSteps :: Bool -> [Tag Text] -> [Tag Text]
  17. hideSteps has_eqn (to@(TagOpen "a" attrs):tt@(TagText t):tc@(TagClose "a"):rest)
  18. | Text.length t >= 1, Text.last t == '⟨', Just href <- lookup "href" attrs =
  19. [ TagOpen "span" [("class", "reasoning-step")]
  20. , TagOpen "span" [("class", "as-written " <> fromMaybe "" (lookup "class" attrs))]
  21. , to, tt, tc ] ++ go href rest
  22. where
  23. alternate = Text.init t
  24. go href (to@(TagOpen "a" attrs):tt@(TagText t):tc@(TagClose "a"):cs)
  25. | Text.length t >= 1
  26. , Text.head t == '⟩'
  27. , Just href' <- lookup "href" attrs
  28. , href' == href
  29. = [ to, tt, tc, TagClose "span"
  30. , TagOpen "span" [("class", "alternate " <> fromMaybe "" (lookup "class" attrs))]
  31. , TagText alternate
  32. , TagClose "span"
  33. , TagClose "span"
  34. ] ++ hideSteps True cs
  35. go href (c:cs) = c:go href cs
  36. go _ [] = []
  37. hideSteps False (TagClose "html":cs) =
  38. [TagOpen "style" [], TagText ".equations { display: none !important; }", TagClose "style", TagClose "html"]
  39. ++ hideSteps True cs
  40. hideSteps has_eqn (c:cs) = c:hideSteps has_eqn cs
  41. hideSteps _ [] = []