Commit 6dce9d63 authored by Camil Staps's avatar Camil Staps 🚀

Add tool in util to generate HTML documentation of ABC

parent 8a7b07d8
Pipeline #17113 failed with stage
in 6 minutes and 39 seconds
definition module Clean.Doc.Markup
from Text.HTML import :: HtmlTag
documentationToHTML :: !(String -> HtmlTag) !String -> [HtmlTag]
implementation module Clean.Doc.Markup
import _SystemArray
import StdBool
import StdChar
import StdClass
import StdInt
import StdMaybe
import StdString
import Text.HTML
documentationToHTML :: !(String -> HtmlTag) !String -> [HtmlTag]
documentationToHTML link s = walk 0 0 (size s-1) link (moveBackticksOutsideCurlyBraces s)
moveBackticksOutsideCurlyBraces :: !String -> String
moveBackticksOutsideCurlyBraces s = edit (size s-1) {c \\ c <-: s}
where
edit :: !Int !*String -> *String
edit -1 s = s
edit i s
| s.[i]=='`' && s.[i-1]=='{' && s.[i-2]=='{' = edit (i-3) {s & [i]='{', [i-2]='`'}
| s.[i]=='}' && s.[i-1]=='}' && s.[i-2]=='`' = edit (i-3) {s & [i]='`', [i-2]='}'}
| otherwise = edit (i-1) s
walk :: !Int !Int !Int !(String -> HtmlTag) !String -> [HtmlTag]
walk last i end link s
| i>end = [Text (s%(last,end))]
| otherwise = case s.[i] of
'`'
| s.[i+1]=='`' && s.[i+2]==' ' -> case find (i+1) end " ``" s of
Nothing -> walk last (i+1) end link s
Just r -> [Text (s%(last,i-1)),CodeTag [] (walk (i+3) (i+3) (r-1) link s):walk (r+3) (r+3) end link s]
| otherwise -> case find (i+1) end "`" s of
Nothing -> walk last (i+1) end link s
Just r -> [Text (s%(last,i-1)),CodeTag [] (walk (i+1) (i+1) (r-1) link s):walk (r+1) (r+1) end link s]
'\n'
| s.[i+1]=='\n'
-> [Text (s%(last,i-1)),BrTag []:walk (i+1) (i+1) end link s]
| s.[i+1]=='`' && s.[i+2]=='`' && s.[i+3]=='`' -> case find (i+4) end "\n" s of
Nothing -> walk last (i+1) end link s
Just nl -> case find nl end "\n```" s of
Nothing -> walk last (i+1) end link s
Just r -> [Text (s%(last,i-1)),PreTag [] [Text (s%(nl,r-1))]:walk (r+4) (r+4) end link s]
# li=first_after_whitespace i s
| s.[li]=='-' || s.[li]=='*'
-> [Text (s%(last,i-1)),BrTag [],Text {s.[li]}:walk (li+1) (li+1) end link s]
'*'
| s.[i+1]=='*'
| s.[i+2]=='*' -> case find (i+3) end "***" s of
Nothing -> walk last (i+1) end link s
Just r -> [Text (s%(last,i-1)),StrongTag [] [EmTag [] (walk (i+3) (i+3) (r-1) link s)]:walk (r+3) (r+3) end link s]
| otherwise -> case find (i+2) end "**" s of
Nothing -> walk last (i+1) end link s
Just r -> [Text (s%(last,i-1)),StrongTag [] (walk (i+2) (i+2) (r-1) link s):walk (r+2) (r+2) end link s]
| otherwise -> case find (i+1) end "*" s of
Nothing -> walk last (i+1) end link s
Just r -> [Text (s%(last,i-1)),EmTag [] (walk (i+1) (i+1) (r-1) link s):walk (r+1) (r+1) end link s]
'{' | s.[i+1]=='{' -> case find (i+2) end "}}" s of
Nothing -> walk last (i+1) end link s
Just r -> [Text (s%(last,i-1)),link (s%(i+2,r-1)):walk (r+2) (r+2) end link s]
_
-> walk last (i+1) end link s
find :: !Int !Int !String !String -> Maybe Int
find i end q s
# sq = size q
| i+sq-1>end
= Nothing
| startsWithAt (i+sq-1) (sq-1) q s
= Just i
= find (i+1) end q s
where
startsWithAt :: !Int !Int !String !String -> Bool
startsWithAt _ -1 _ _ = True
startsWithAt i j q s = s.[i]==q.[j] && startsWithAt (i-1) (j-1) q s
first_after_whitespace :: !Int !String -> Int
first_after_whitespace i s
| i >= size s = i-1
| isSpace s.[i] = first_after_whitespace (i+1) s
| otherwise = i
module genabcdoc
import StdFunctions
import StdList
import StdOrdList
import StdString
import Clean.Doc.Markup
import Data.Either
from Data.Func import on, `on`
import Data.List
import Text
import Text.HTML
import Cloogle.API
import Cloogle.DB
import Builtin.ABC
Start = toString (documentation builtin_abc_instructions)
documentation :: ![Either String ABCInstructionEntry] -> HtmlTag
documentation instrs = HtmlTag []
[ HeadTag []
[ TitleTag [] [Text "ABC documentation"]
, StyleTag [TypeAttr "text/css"] [Text ".instruction{border:1px solid black;margin:.5em;padding:.5em;}"]
]
, BodyTag []
[ H1Tag [] [Text "ABC documentation"]
: concatMap entry instrs ++
[H2Tag [] [Text "Index"], index instrs]
]
]
index :: ![Either String ABCInstructionEntry] -> HtmlTag
index instrs = UlTag []
[LiTag [] [ATag [HrefAttr ("#"+++i.aie_instruction)] [Text i.aie_instruction]] \\ i <- sorted]
where
sorted = sortBy ((<) `on` \e->e.aie_instruction) [i \\ Right i <- instrs]
entry :: !(Either String ABCInstructionEntry) -> [HtmlTag]
entry (Left title) = [H2Tag [] [Text title]]
entry (Right aie)
| aie.aie_description=="There is no documentation for this ABC instruction yet."
= [CodeTag [] [Text aie.aie_instruction], Text " "]
= [DivTag [IdAttr aie.aie_instruction, ClassAttr "instruction"]
[ CodeTag [] (intersperse (Text " ") [Text aie.aie_instruction:[EmTag [] [Text (toString arg)] \\ arg <- aie.aie_arguments]])
, DivTag [] (documentationToHTML link aie.aie_description)
]]
where
link :: !String -> HtmlTag
link s = ATag [HrefAttr ("#"+++s)] [Text s]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment