minor correction to IR and a lot of work towards generating the HTML, just need to handle some inline elements

This commit is contained in:
Pagwin 2025-11-02 23:35:11 -05:00
parent d61fd86b76
commit bab574a9db
No known key found for this signature in database
GPG key ID: 81137023740CA260
2 changed files with 39 additions and 8 deletions

View file

@ -2,8 +2,43 @@
module HTML (compileToHTML) where module HTML (compileToHTML) where
import Data.Text import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import IR import IR
compileToHTML :: Document -> Text tshow :: (Show s) => s -> T.Text
compileToHTML = const "" tshow = T.pack . show
compileToHTML :: Document -> T.Text
compileToHTML (Doc elements) = T.concat $ map elementToHTML elements
elementToHTML :: Element -> T.Text
elementToHTML (Heading (H {level, text})) = T.concat ["<h", tshow level, ">", serializeInlineToHTML text, "</h", tshow level, ">"]
--
elementToHTML (Code (C {language = m_language, code})) = T.concat ["<pre class=\"sourceCode ", language, "\"><code class=\"sourceCode ", language, "\">"]
where
language = fromMaybe "" m_language
elementToHTML (BlockQuote (Q elems)) = T.concat ["<blockquote>", serializeInlineToHTML elems, "</blockquote>"]
elementToHTML (List (L {list_type = Ordered, items})) = T.concat ["<ol>", generateLiElems items, "</ol>"]
elementToHTML (List (L {list_type = Unordered, items})) = T.concat ["<ul>", generateLiElems items, "</ul>"]
elementToHTML (HTML (HTMLTag {tagName, attributes, html_content})) = T.concat ["<", tagName, T.concat $ map (\(name, value) -> T.concat [name, "=", "\"", fromMaybe "" value, "\""]) attributes, ">", html_content, "</", tagName, ">"]
elementToHTML (Paragraph (P snippets)) = serializeInlineToHTML snippets
elementToHTML HorizontalRule = "<hr>"
generateLiElems :: [ListItem] -> T.Text
generateLiElems [] = ""
generateLiElems (LI {content, children} : remainder) =
T.concat
[ "<li>",
-- We assume child lists are stricly after our contents
-- if they aren't this is fucked
serializeInlineToHTML content,
T.concat $ map (elementToHTML . List) children,
"</li>"
]
serializeInlineToHTML :: [InlineText] -> T.Text
serializeInlineToHTML [] = ""
serializeInlineToHTML (Text t : rem) = t <> serializeInlineToHTML rem
serializeInlineToHTML (Bold elems : rem) = T.concat ["<b>", serializeInlineToHTML elems, "</b>", serializeInlineToHTML rem]
serializeInlineToHTML (Italic elems : rem) = T.concat ["<i>", serializeInlineToHTML elems, "</i>", serializeInlineToHTML rem]

View file

@ -39,8 +39,6 @@ data List = L
items :: [ListItem] items :: [ListItem]
} }
-- Table: keep as-is or simplify based on your needs
data HTML data HTML
= HTMLTag = HTMLTag
{ tagName :: Text, { tagName :: Text,
@ -48,8 +46,6 @@ data HTML
html_content :: Text html_content :: Text
} }
-- Optionally skip: HTMLComment, HTMLDeclaration
newtype Paragraph = P [InlineText] newtype Paragraph = P [InlineText]
data InlineText data InlineText
@ -63,7 +59,7 @@ data InlineText
title :: Maybe Text title :: Maybe Text
} }
| Image | Image
{ altText :: [InlineText], { altText :: Text,
url :: Text, url :: Text,
title :: Maybe Text title :: Maybe Text
} }