-
Notifications
You must be signed in to change notification settings - Fork 46
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
Make definitions block-level #257
base: master
Are you sure you want to change the base?
Changes from all commits
ec6c3a2
4ae638a
a8d88a8
34137b8
1ddf523
d50b1ee
18e7674
88d1133
d5ab6ca
a243c87
0d81673
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,12 @@ open Ast | |
module Sub = Parser.Sub | ||
|
||
module Pre = struct | ||
type container = | ||
type t = | ||
{ blocks : attributes Raw.block list | ||
; next : container | ||
} | ||
|
||
and container = | ||
| Rblockquote of t | ||
| Rlist of | ||
list_type | ||
|
@@ -21,12 +26,15 @@ module Pre = struct | |
* attributes | ||
| Rindented_code of string list | ||
| Rhtml of Parser.html_kind * string list | ||
| Rdef_list of string * string list | ||
| Rdef_list of def_list_container | ||
| Rempty | ||
|
||
and t = | ||
{ blocks : attributes Raw.block list | ||
; next : container | ||
and def_list_container = | ||
{ term : string | ||
; defs : attributes Raw.block list list | ||
; indent : int | ||
; empty_line_seen : bool | ||
; state : t | ||
} | ||
|
||
let concat l = String.concat "\n" (List.rev l) ^ "\n" | ||
|
@@ -69,13 +77,30 @@ module Pre = struct | |
Code_block (attr, label, "") :: blocks | ||
| Rfenced_code (_, _, _kind, (label, _other), l, attr) -> | ||
Code_block (attr, label, concat l) :: blocks | ||
| Rdef_list (term, defs) -> | ||
let l, blocks = | ||
| Rdef_list deflist -> | ||
let def = finish deflist.state in | ||
let defs = def :: deflist.defs in | ||
let this_sp = | ||
match def with | ||
| [ Paragraph _ ] -> Tight | ||
| _ -> Loose | ||
in | ||
let l, sp, blocks = | ||
match blocks with | ||
| Definition_list (_, l) :: b -> (l, b) | ||
| b -> ([], b) | ||
| Definition_list (_, sp, l) :: b -> | ||
let sp = | ||
match (sp, this_sp) with | ||
| Tight, Tight -> Tight | ||
| Loose, _ | ||
| _, Loose -> | ||
Loose | ||
in | ||
(l, sp, b) | ||
| b -> ([], this_sp, b) | ||
in | ||
Definition_list ([], l @ [ { term; defs = List.rev defs } ]) :: blocks | ||
Definition_list | ||
([], sp, l @ [ { term = deflist.term; defs = List.rev defs } ]) | ||
:: blocks | ||
| Rindented_code l -> | ||
(* TODO: trim from the right *) | ||
let rec loop = function | ||
|
@@ -116,10 +141,19 @@ module Pre = struct | |
} | ||
| Rempty, (Lsetext_heading _ | Lparagraph | Ldef_list _) -> | ||
{ blocks; next = Rparagraph [ Sub.to_string s ] } | ||
| Rparagraph [ h ], Ldef_list def -> | ||
{ blocks; next = Rdef_list (h, [ def ]) } | ||
| Rdef_list (term, defs), Ldef_list def -> | ||
{ blocks; next = Rdef_list (term, def :: defs) } | ||
(* | Rparagraph [ h ], Ldef_list (indent, def) -> *) | ||
| Rparagraph [ h ], Ldef_list (indent, def) -> | ||
{ blocks | ||
; next = | ||
(* Rdef_list (h, indent, false, [], process empty (Sub.of_string def)) *) | ||
Rdef_list | ||
{ term = h | ||
; indent | ||
; empty_line_seen = false | ||
; defs = [] | ||
; state = process empty (Sub.of_string def) | ||
} | ||
} | ||
| Rparagraph _, Llist_item ((Ordered (1, _) | Bullet _), _, s1) | ||
when not (Parser.is_empty (Parser.P.of_string (Sub.to_string s1))) -> | ||
process { blocks = close { blocks; next }; next = Rempty } s | ||
|
@@ -147,10 +181,34 @@ module Pre = struct | |
{ blocks | ||
; next = Rfenced_code (ind, num, q, info, Sub.to_string s :: lines, a) | ||
} | ||
| Rdef_list (term, d :: defs), Lparagraph -> | ||
| ( Rdef_list ({ empty_line_seen = false; _ } as deflist) | ||
, Ldef_list (indent, def) ) -> | ||
{ blocks | ||
; next = Rdef_list (term, (d ^ "\n" ^ Sub.to_string s) :: defs) | ||
; next = | ||
Rdef_list | ||
{ deflist with | ||
indent | ||
; defs = finish deflist.state :: deflist.defs | ||
; state = process empty (Sub.of_string def) | ||
} | ||
} | ||
| Rdef_list deflist, Lempty -> | ||
{ blocks | ||
; next = | ||
Rdef_list | ||
{ deflist with | ||
empty_line_seen = true | ||
; state = process deflist.state s | ||
} | ||
} | ||
| Rdef_list deflist, _ when Parser.indent s >= deflist.indent -> | ||
let s = Sub.offset deflist.indent s in | ||
let state = process deflist.state s in | ||
{ blocks; next = Rdef_list { deflist with state } } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this should set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does address the lazy wrapping, thanks for the tip! Unfortunately it also breaks the nested def-list parsing. I'll continue considering it, but if an immediate solution occurs to you, let me know ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reordering the clauses such that the one that checks for the indentation level comes first solves the problem with the nested def-list test. The only remaining problem on def_list-003 test then are missing |
||
| Rdef_list ({ empty_line_seen = false; _ } as deflist), Lparagraph -> | ||
(* Lazy wrapping *) | ||
let state = process deflist.state s in | ||
{ blocks; next = Rdef_list { deflist with state } } | ||
| Rdef_list _, _ -> | ||
process { blocks = close { blocks; next }; next = Rempty } s | ||
| Rindented_code lines, Lindented_code s -> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this a bit more, I don't think we need the
empty_line_seen = false
check here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing this (either by removing the match on the
empty_line_seen
or removing the case all together) leads to failures around theThis is not a correct definition list
line. This to will need some considering. The moral of the story seems to me that the handwritten parsing logic here is quite touchy and tough to reason about...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/jgm/commonmark-hs/blob/master/commonmark-extensions/test/definition_lists.md ("Multiple definitions, loose") gives this example:
So perhaps the
This is not a correct definition list
test is itself wrong?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks for the reference! I'm happy with deferring to commonmark on these things. I'll spend some time comparing their test suite with ours later this week or over the weekend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, duh. Now that I look at the test case, since you've introduced the loose spacing, of course that test is now incorrect! My bad for not giving it sufficient thought before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, if we remove this check, then it fixes the loose format for multiple definitions, but it breaks the nesting. Which I think means that, even if we were OK with the leaving the lazy line wrapping broken for multi-paragraph definitions, we're still a bit stuck on this point.
I'll investigate over the weekend, and try to get my head around properly around the parsing function. But just an FYI to explains why I'm not merging this straight away.