Skip to content

Commit

Permalink
another approach to nested link references dillonkearns#60 (by using …
Browse files Browse the repository at this point in the history
…rawBlockParser when a container is completed/close)
  • Loading branch information
LutSa committed Jun 4, 2021
1 parent 1441e20 commit 83643f7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 66 deletions.
129 changes: 65 additions & 64 deletions src/Markdown/Parser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ parse input =

Ok v ->
-- then parse the inlines of each raw block
case parseAllInlines (parseContainers v) of
case parseAllInlines v of
Err e ->
-- NOTE these messages get an incorrect location,
-- because they are parsed outside of the main (raw block) parser context.
Expand All @@ -80,35 +80,6 @@ parse input =
Ok (List.filter isNotEmptyParagraph blocks)


parseContainers: State-> State
parseContainers state =
parseContainersHelp state.rawBlocks {linkReferenceDefinitions=state.linkReferenceDefinitions, rawBlocks = []}


parseContainersHelp : List RawBlock -> State -> State
parseContainersHelp unparsedRawBlocks parsedState =
case unparsedRawBlocks of
rawBlock :: rest ->
case rawBlock of
BlockQuote rawBlocks ->
case Advanced.run rawBlockParser rawBlocks of
Ok value ->
parseContainersHelp rest
{ linkReferenceDefinitions = parsedState.linkReferenceDefinitions ++ (parseContainers value).linkReferenceDefinitions
, rawBlocks = parsedState.rawBlocks ++[ (parseContainers value).rawBlocks|>ParsedBlockQuote]
}
Err e ->
-- TODO return this error
{linkReferenceDefinitions=[], rawBlocks= []}
_ ->
parseContainersHelp rest
{ linkReferenceDefinitions = parsedState.linkReferenceDefinitions
, rawBlocks = parsedState.rawBlocks ++ [rawBlock]
}
[] ->
parsedState


deadEndsToString : List (Advanced.DeadEnd String Parser.Problem) -> String
deadEndsToString deadEnds =
deadEnds
Expand Down Expand Up @@ -670,71 +641,88 @@ parseAllInlinesHelp state rawBlocks parsedBlocks =

completeOrMergeBlocks : State -> RawBlock -> State
completeOrMergeBlocks state newRawBlock =
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks =
case
( newRawBlock
, state.rawBlocks
)
of
( CodeBlock block1, (CodeBlock block2) :: rest ) ->
CodeBlock
{ body = joinStringsPreserveAll block2.body block1.body
, language = Nothing
}
:: rest
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks = CodeBlock
{ body = joinStringsPreserveAll block2.body block1.body
, language = Nothing
}
:: rest}

( IndentedCodeBlock block1, (IndentedCodeBlock block2) :: rest ) ->
IndentedCodeBlock (joinStringsPreserveAll block2 block1)
:: rest

( OpenBlockOrParagraph (UnparsedInlines body1), (BlockQuote body2) :: rest ) ->
BlockQuote (joinRawStringsWith "\n" body2 body1)
:: rest

( BlockQuote body1, (BlockQuote body2) :: rest ) ->
BlockQuote (joinStringsPreserveAll body2 body1)
:: rest
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks = IndentedCodeBlock (joinStringsPreserveAll block2 block1)
:: rest}


( _, (BlockQuote body2) :: rest ) ->
case newRawBlock of
BlockQuote body1 ->
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks = BlockQuote (joinStringsPreserveAll body2 body1)
:: rest}

OpenBlockOrParagraph (UnparsedInlines body1) ->
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks = BlockQuote (joinRawStringsWith "\n" body2 body1)
:: rest}

_ ->
case Advanced.run rawBlockParser body2 of
Ok value ->
{ linkReferenceDefinitions = state.linkReferenceDefinitions ++ value.linkReferenceDefinitions
, rawBlocks = newRawBlock :: (value.rawBlocks |> ParsedBlockQuote) :: rest
}
Err e ->
-- TODO return this error
{linkReferenceDefinitions=[], rawBlocks= []}

( OpenBlockOrParagraph (UnparsedInlines body1), (OpenBlockOrParagraph (UnparsedInlines body2)) :: rest ) ->
OpenBlockOrParagraph (UnparsedInlines (joinRawStringsWith "\n" body2 body1))
:: rest
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks = OpenBlockOrParagraph (UnparsedInlines (joinRawStringsWith "\n" body2 body1))
:: rest}

( SetextLine LevelOne _, (OpenBlockOrParagraph unparsedInlines) :: rest ) ->
Heading 1 unparsedInlines
:: rest
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks =Heading 1 unparsedInlines
:: rest}

( SetextLine LevelTwo _, (OpenBlockOrParagraph unparsedInlines) :: rest ) ->
Heading 2 unparsedInlines
:: rest
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks =Heading 2 unparsedInlines
:: rest}

( TableDelimiter (Markdown.Table.TableDelimiterRow text alignments), (OpenBlockOrParagraph (UnparsedInlines rawHeaders)) :: rest ) ->
case TableParser.parseHeader (Markdown.Table.TableDelimiterRow text alignments) rawHeaders of
Ok (Markdown.Table.TableHeader headers) ->
Table (Markdown.Table.Table headers []) :: rest
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks =Table (Markdown.Table.Table headers []) :: rest}

Err _ ->
OpenBlockOrParagraph (UnparsedInlines (joinRawStringsWith "\n" rawHeaders text.raw))
:: rest
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks =OpenBlockOrParagraph (UnparsedInlines (joinRawStringsWith "\n" rawHeaders text.raw))
:: rest}

( Table updatedTable, (Table _) :: rest ) ->
Table updatedTable :: rest
{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks = Table updatedTable :: rest}

_ ->
newRawBlock :: state.rawBlocks
}


{ linkReferenceDefinitions = state.linkReferenceDefinitions
, rawBlocks = newRawBlock :: state.rawBlocks}

-- RAW BLOCK PARSER


stepRawBlock : State -> Parser (Step State State)
stepRawBlock revStmts =
-- Some blocks can't immediately follow a body
oneOf
[ Helpers.endOfFile
|> map (\_ -> Done revStmts)
|> map (\_ -> Done (completeBlocks revStmts))
, LinkReferenceDefinition.parser
|> Advanced.backtrackable
|> map (\reference -> Loop (addReference revStmts reference))
Expand All @@ -757,6 +745,19 @@ stepRawBlock revStmts =
]


completeBlocks state=
case state.rawBlocks of
(BlockQuote body2) :: rest ->
case Advanced.run rawBlockParser body2 of
Ok value ->
{ linkReferenceDefinitions = state.linkReferenceDefinitions ++ value.linkReferenceDefinitions
, rawBlocks = (value.rawBlocks |> ParsedBlockQuote) :: rest
}
Err e ->
-- TODO return this error
state
_-> state


-- Note [Static Parser Structure]
--
Expand Down
2 changes: 1 addition & 1 deletion test-results/failing/CommonMark/HTML blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ Should give output:
But instead was:
````````````html
<p>bar</p>
````````````
## [Example 144](https://spec.commonmark.org/0.29/#example-144)
Expand Down
2 changes: 1 addition & 1 deletion test-results/failing/GFM/HTML blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ Should give output:
But instead was:
````````````html
<p>bar</p>
````````````
## [Example 144](https://spec.commonmark.org/0.29/#example-144)
Expand Down

0 comments on commit 83643f7

Please sign in to comment.