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

markdown generator #791

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6ddcac7
add markup module
lubegasimon Dec 19, 2021
c40ab7c
add markdown generator
lubegasimon Dec 19, 2021
dc8604b
add simple test module for ease PR review
lubegasimon Dec 19, 2021
1a934c6
promote tests
lubegasimon Dec 19, 2021
508704e
some code improvements
lubegasimon Jan 5, 2022
06c6159
Fix path handling
Julow Jan 18, 2022
6021e82
Improve indentation of headings
Julow Jan 18, 2022
812289d
Don't render alternatives as code
Julow Jan 19, 2022
e80d5a0
Render 6-heading only for declarations
Julow Jan 19, 2022
46e8f80
Improve test case
Julow Jan 19, 2022
cba655d
Take code from inlined expansions
Julow Jan 19, 2022
91e7cb0
Render formatted code out of headings
Julow Jan 19, 2022
0d5e556
Render formatted code in quote blocks
Julow Jan 19, 2022
0c45ebb
Disable inlining of expansions
Julow Jan 28, 2022
749fead
Generate correct links to other pages
Julow Feb 2, 2022
d2c2dca
Enable generator tests for markdown
Julow Feb 2, 2022
0da2e62
Cleanup blocks concatenation
Julow Feb 2, 2022
1fc35ec
Cleanup iteration code
Julow Feb 3, 2022
654d4a0
Make sure noops are removed from concatenations
Julow Feb 3, 2022
69a08be
Document: Remove empty text nodes
Julow Feb 3, 2022
9bbc31a
Fix bits of source code incorrectly removed
Julow Feb 3, 2022
b559088
Fix printing of code blocks
Julow Feb 3, 2022
a8a859d
Fix printing of code spans
Julow Feb 3, 2022
c4922c8
Fix printing of documentation comments
Julow Feb 3, 2022
3c2a8d9
Fix rendering of nested and documented items
Julow Feb 4, 2022
7449b4a
Fix spacing after quote blocks
Julow Feb 7, 2022
27a972c
Fix rendering of spaces between inlines
Julow Feb 7, 2022
5726a9b
Remove inlines concatenation with spaces
Julow Feb 7, 2022
b4d5f6d
document: Remove extra space after class keyword
Julow Feb 7, 2022
0b10e6f
remove unnecessary block_separator
lubegasimon Feb 11, 2022
93c66f6
Escape backticks
Julow Feb 11, 2022
b38f9b6
Use Astring
Julow Feb 11, 2022
4389a34
Properly handle HTML entities
Julow Feb 11, 2022
6627327
block quote variant constructors
lubegasimon Feb 11, 2022
67077ec
add brackets back to record types and variants
lubegasimon Feb 11, 2022
be8a615
mark up record and variant closing brackets that are blocks
lubegasimon Feb 15, 2022
ab13bf8
remove redundant brackets
lubegasimon Feb 15, 2022
0968555
promote tests after a rebase
lubegasimon Feb 15, 2022
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
Prev Previous commit
Next Next commit
Make sure noops are removed from concatenations
Noops shouldn't be present in the tree to respect the contract:

    noop ++ x = x ++ noop = x

Remove the special handling of Text nodes that was a work around this.
  • Loading branch information
Julow committed Feb 3, 2022
commit 654d4a0e1ec31efbbac13aae812cbfe7be8ad9dd
52 changes: 22 additions & 30 deletions src/markdown/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ let style (style : style) =
| `Superscript -> superscript
| `Subscript -> subscript

let fold_inlines f elts : inlines =
List.fold_left (fun acc elt -> acc ++ f elt) noop elts

let fold_blocks f elts : blocks =
List.fold_left (fun acc elt -> acc +++ f elt) noop_block elts

Expand Down Expand Up @@ -68,36 +71,25 @@ let rec source_code (s : Source.t) args =
text "->" (* takes care of the Entity branch of Inline.t *)
| Tag (_, s) :: t -> source_code s args ++ source_code t args

and inline (l : Inline.t) args =
match l with
| [] -> noop
| i :: rest -> (
match i.desc with
| Text "" | Text " " -> inline rest args
| Text _ ->
let l, _, rest =
Doctree.Take.until l ~classify:(function
| { Inline.desc = Text s; _ } -> Accum [ s ]
| _ -> Stop_and_keep)
in
text String.(concat "" l |> trim) ++ inline rest args
| Entity _ -> noop
| Styled (styl, content) ->
style styl (inline content args) ++ inline rest args
| Linebreak -> line_break ++ inline rest args
| Link (href, content) ->
link ~href (inline content args) ++ inline rest args
| InternalLink (Resolved (url, content)) ->
if args.generate_links then
link
~href:(Link.href ~base_path:args.base_path url)
(inline content args)
++ inline rest args
else inline content args ++ inline rest args
| InternalLink (Unresolved content) ->
inline content args ++ inline rest args
| Source content -> source_code content args ++ inline rest args
| Raw_markup (_, s) -> text s ++ inline rest args)
and inline l args = fold_inlines (inline_one args) l

and inline_one args i =
match i.Inline.desc with
| Text ("" | " ") -> noop
| Text s -> text (String.trim s)
| Entity _ -> noop
| Styled (styl, content) -> style styl (inline content args)
| Linebreak -> line_break
| Link (href, content) -> link ~href (inline content args)
| InternalLink (Resolved (url, content)) ->
if args.generate_links then
link
~href:(Link.href ~base_path:args.base_path url)
(inline content args)
else inline content args
| InternalLink (Unresolved content) -> inline content args
| Source content -> source_code content args
| Raw_markup (_, s) -> text s

let rec block args l = fold_blocks (block_one args) l

Expand Down
5 changes: 4 additions & 1 deletion src/markdown/markup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ let ordered_list bs = List (Ordered, bs)

let unordered_list bs = List (Unordered, bs)

let ( ++ ) left right = ConcatI (left, right)
(* Make sure to never leave a [Noop] in the result, which would cause unwanted
spaces. *)
let ( ++ ) left right =
match (left, right) with Noop, x | x, Noop -> x | _ -> ConcatI (left, right)

let join left right = Join (left, right)

Expand Down