Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run inline parser within Inline HTML #100

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spec-results.json
Original file line number Diff line number Diff line change
Expand Up @@ -1309,12 +1309,18 @@
"auto_links": [
null
],
"backslash_escapes": [
null
],
"blockquotes_with_code_blocks": [
null
],
"code_blocks": [
null
],
"code_spans": [
null
],
"horizontal_rules": [
null
],
Expand Down
4 changes: 2 additions & 2 deletions spec-tests/OutputMarkdownHtml.elm
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ renderMarkdown markdown =
classes =
-- Only the first word is used in the class
case Maybe.map String.words language of
Just (actualLanguage::_) ->
Just (actualLanguage :: _) ->
[ Attr.class <| "language-" ++ actualLanguage ]

_ ->
Expand Down Expand Up @@ -282,7 +282,7 @@ passThroughNode nodeName =

{-| TODO come up with an API to provide a solution to do this sort of thing publicly
-}
passthrough : (String -> List Markdown.HtmlRenderer.Attribute -> List Block -> Result String view) -> Markdown.HtmlRenderer.HtmlRenderer view
passthrough : (String -> List Markdown.HtmlRenderer.Attribute -> Markdown.HtmlRenderer.InlinesOrBlocks -> Result String view) -> Markdown.HtmlRenderer.HtmlRenderer view
passthrough renderFn =
Markdown.HtmlRenderer.HtmlRenderer renderFn

Expand Down
45 changes: 12 additions & 33 deletions src/Markdown/Block.elm
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ type HeadingLevel
{-| An Inline block. Note that `HtmlInline`s can contain Blocks, not just nested `Inline`s.
-}
type Inline
= HtmlInline (Html Block)
= HtmlInline (Html Inline)
| Link String (Maybe String) (List Inline)
| Image String (Maybe String) (List Inline)
| Emphasis (List Inline)
Expand Down Expand Up @@ -217,13 +217,8 @@ extractTextHelp inline text =

HtmlInline html ->
case html of
HtmlElement _ _ blocks ->
blocks
|> foldl
(\block soFar ->
soFar ++ extractInlineBlockText block
)
text
HtmlElement _ _ inlines ->
text ++ extractInlineText inlines

_ ->
text
Expand Down Expand Up @@ -303,22 +298,6 @@ extractInlineBlockText block =
""



--BlockQuote blocks ->
--
--
--Heading headingLevel inlines ->
--
--
--Table list lists ->
--
--
--CodeBlock record ->
--
--
--ThematicBreak ->


{-| The way HTML is handled is one of the core ideas of this library.

You get the full HTML structure that you can use to process the Blocks before rendering them. Once you render them,
Expand Down Expand Up @@ -501,7 +480,7 @@ inlineParserWalk function inline =
HtmlInline html ->
case html of
HtmlElement string htmlAttributes children ->
HtmlElement string htmlAttributes (List.map (walkInlines function) children)
HtmlElement string htmlAttributes (List.map function children)
|> HtmlInline

_ ->
Expand Down Expand Up @@ -583,16 +562,16 @@ inlineParserValidateWalk function inline =

HtmlInline html ->
case html of
HtmlElement tagName htmlAttributes blocks ->
blocks
|> traverse (inlineParserValidateWalkBlock function)
HtmlElement tagName htmlAttributes inlines ->
inlines
|> traverse function
|> Result.andThen
(\transformedBlocks ->
HtmlElement tagName htmlAttributes transformedBlocks
(\transformedInlines ->
HtmlElement tagName htmlAttributes transformedInlines
|> HtmlInline
|> function
|> Result.mapError List.singleton
)
|> Result.mapError List.singleton

_ ->
function inline
Expand Down Expand Up @@ -978,8 +957,8 @@ inlineFoldl ifunction top_acc list =
ifn inline acc
in
case hblock of
HtmlElement _ _ blocks ->
inlineFoldl ifn hiacc blocks
HtmlElement _ _ inlines ->
List.foldl ifn hiacc inlines

HtmlComment _ ->
ifn inline hiacc
Expand Down
11 changes: 8 additions & 3 deletions src/Markdown/HtmlRenderer.elm
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module Markdown.HtmlRenderer exposing (Attribute, HtmlRenderer(..))
module Markdown.HtmlRenderer exposing (Attribute, HtmlRenderer(..), InlinesOrBlocks(..))

import Markdown.Block exposing (Block)
import Markdown.Block exposing (Block, Inline)


type alias Attribute =
{ name : String, value : String }


type InlinesOrBlocks
= Inlines (List Inline)
| Blocks (List Block)


type HtmlRenderer a
= HtmlRenderer (String -> List Attribute -> List Block -> Result String a)
= HtmlRenderer (String -> List Attribute -> InlinesOrBlocks -> Result String a)
40 changes: 39 additions & 1 deletion src/Markdown/Parser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mapInline inline =

Inline.HtmlInline node ->
node
|> nodeToRawBlock
|> nodeToRawInline
|> Block.HtmlInline

Inline.Emphasis level inlines ->
Expand Down Expand Up @@ -536,6 +536,11 @@ textNodeToBlocks textNodeValue =
|> Result.withDefault []


textNodeToInlines : String -> List Inline
textNodeToInlines textNodeValue =
inlineParseHelper [] (UnparsedInlines textNodeValue)


nodeToRawBlock : Node -> Block.Html Block
nodeToRawBlock node =
case node of
Expand Down Expand Up @@ -569,6 +574,39 @@ nodeToRawBlock node =
Block.HtmlDeclaration declarationType content


nodeToRawInline : Node -> Block.Html Inline
nodeToRawInline node =
case node of
HtmlParser.Text innerText ->
Block.HtmlComment "TODO this never happens, but use types to drop this case."

HtmlParser.Element tag attributes children ->
let
parseChild child =
case child of
HtmlParser.Text text ->
textNodeToInlines text

_ ->
[ nodeToRawInline child |> Block.HtmlInline ]
in
Block.HtmlElement tag
attributes
(List.concatMap parseChild children)

Comment string ->
Block.HtmlComment string

Cdata string ->
Block.Cdata string

ProcessingInstruction string ->
Block.ProcessingInstruction string

Declaration declarationType content ->
Block.HtmlDeclaration declarationType content


nodesToBlocks : List Node -> Result Parser.Problem (List Block)
nodesToBlocks children =
nodesToBlocksHelp children []
Expand Down
42 changes: 40 additions & 2 deletions src/Markdown/Renderer.elm
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ renderHtml tagName attributes children (Markdown.HtmlRenderer.HtmlRenderer htmlR
|> combineResults
|> Result.andThen
(\okChildren ->
htmlRenderer tagName attributes children
children
|> Markdown.HtmlRenderer.Blocks
|> htmlRenderer tagName attributes
|> Result.map
(\myRenderer -> myRenderer okChildren)
)
Expand Down Expand Up @@ -579,7 +581,7 @@ renderSingleInline renderer inline =
Block.HtmlInline html ->
case html of
Block.HtmlElement tag attributes children ->
renderHtmlNode renderer tag attributes children
renderInlineHtmlNode renderer tag attributes children
|> Just

_ ->
Expand All @@ -593,3 +595,39 @@ renderHtmlNode renderer tag attributes children =
children
renderer.html
(renderHelper renderer children)


renderInlineHtmlNode : Renderer view -> String -> List Attribute -> List Inline -> Result String view
renderInlineHtmlNode renderer tag attributes children =
renderInlineHtml tag
attributes
children
renderer.html
(renderHelperInline renderer children)


renderInlineHtml :
String
-> List Attribute
-> List Inline
-> Markdown.Html.Renderer (List view -> view)
-> Result String (List view)
-> Result String view
renderInlineHtml tagName attributes children (Markdown.HtmlRenderer.HtmlRenderer htmlRenderer) renderedChildren =
renderedChildren
|> Result.andThen
(\okChildren ->
children
|> Markdown.HtmlRenderer.Inlines
|> htmlRenderer tagName attributes
|> Result.map
(\myRenderer -> myRenderer okChildren)
)


renderHelperInline :
Renderer view
-> List Inline
-> Result String (List view)
renderHelperInline renderer blocks =
renderStyled renderer blocks
2 changes: 1 addition & 1 deletion test-results/failing/New/double_link.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ Should give output:
But instead was:

````````````html
<p><p>Already linked:</p><a href="http://example.com/"><p>http://example.com/</p></a><p>.</p></p><p>Already linked:<a href="http://example.com/">http://example.com/</a>.</p><p>Already linked:<a href="http://example.com/"><p><strong>http://example.com/</strong></p></a>.</p>
<p><p>Already linked:</p><a href="http://example.com/"><p>http://example.com/</p></a><p>.</p></p><p>Already linked:<a href="http://example.com/">http://example.com/</a>.</p><p>Already linked:<a href="http://example.com/"><strong>http://example.com/</strong></a>.</p>
````````````
Loading