Skip to content

Commit

Permalink
add --no-link option, and a small improvement
Browse files Browse the repository at this point in the history
Signed-off-by: lubegasimon <[email protected]>
  • Loading branch information
lubegasimon committed Sep 14, 2021
1 parent cd51f81 commit 1704036
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 32 deletions.
81 changes: 53 additions & 28 deletions src/markdown/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,32 @@ and inline (l : Inline.t) =
| Styled (sty, content) -> style sty (inline content) ++ inline rest
| Linebreak -> break ++ inline rest
| Link (href, content) ->
(let rec f (content : Inline.t) =
match content with
| [] -> noop
| i :: rest -> (
match i.desc with
| Text s ->
command "" (str "[%s]" s ++ str "(%s)" href) ++ f rest
| _ -> noop ++ f rest)
in
f content)
++ inline rest
| InternalLink (Resolved (link, content)) -> (
match link.page.parent with
| Some _ -> inline content ++ inline rest
| None ->
open_sq_bracket ++ inline content ++ close_sq_bracket
++ open_parenthesis
++ String ("#" ^ link.anchor)
++ close_parenthesis ++ inline rest)
if !Link.no_link then
(let rec f (content : Inline.t) =
match content with
| [] -> noop
| i :: rest -> (
match i.desc with
| Text s ->
command "" (str "[%s]" s ++ str "(%s)" href) ++ f rest
| _ -> noop ++ f rest)
in
f content)
++ inline rest
else inline content ++ inline rest
| InternalLink (Resolved (link, content)) ->
if !Link.no_link then
match link.page.parent with
| Some _ -> inline content ++ inline rest
| None ->
open_sq_bracket ++ inline content ++ close_sq_bracket
++ open_parenthesis
++ String ("#" ^ link.anchor)
++ close_parenthesis ++ inline rest
else inline content ++ inline rest
| InternalLink (Unresolved content) -> inline content ++ inline rest
| Source content -> env Any "`" "`" (source_code content) ++ inline rest
| Source content ->
env Any "`` " "`` " (source_code content) ++ inline rest
| Raw_markup t -> raw_markup t ++ inline rest)

let rec block (l : Block.t) =
Expand Down Expand Up @@ -287,24 +292,44 @@ and item nbsp (l : Item.t list) : Markup.t =
in
d ++ continue rest)

(* TODO: This is a duplicate, worthy to be abstracted *)
let rec list_concat_map ?sep ~f = function
| [] -> []
| [ x ] -> f x
| x :: xs -> (
let hd = f x in
let tl = list_concat_map ?sep ~f xs in
match sep with None -> hd @ tl | Some sep -> hd @ sep :: tl)

let on_sub subp =
match subp with
| `Page p -> if Link.should_inline p.Subpage.content.url then Some 1 else None
| `Include incl -> if inline_subpage incl.Include.status then Some 0 else None

let page { Page.header; items = i; url; _ } =
(** TODO: Rename the function. *)
let rec calc_subpages (no_link : bool) { Subpage.content; _ } =
[ page no_link content ]

and subpages (no_link : bool) i =
list_concat_map ~f:(calc_subpages no_link) @@ Doctree.Subpages.compute i

and page (no_link : bool) ({ Page.header; items; url; _ } as p) =
let header = Shift.compute ~on_sub header in
let i = Shift.compute ~on_sub i in
let items = Shift.compute ~on_sub items in
let subpages = subpages no_link p in
Block
([ Inline (Link.for_printing url) ]
@ [ item "&nbsp; " header ++ item "&nbsp; " i ])
@ [ item "&nbsp; " header ++ item "&nbsp; " items ]
@ subpages)

let rec subpage subp =
let rec subpage subp ~no_link =
let p = subp.Subpage.content in
if Link.should_inline p.url then [] else [ render p ]
if Link.should_inline p.url then [] else [ render p ~no_link ]

and render (p : Page.t) =
let content fmt = Format.fprintf fmt "%a" Markup.pp (page p) in
let children = Utils.flatmap ~f:subpage @@ Subpages.compute p in
and render (p : Page.t) ~no_link =
let content fmt = Format.fprintf fmt "%a" Markup.pp (page no_link p) in
let children =
Utils.flatmap ~f:(fun subp -> subpage subp ~no_link) (Subpages.compute p)
in
let filename = Link.as_filename p.url in
{ Odoc_document.Renderer.filename; content; children }
3 changes: 2 additions & 1 deletion src/markdown/generator.mli
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
val render : Odoc_document.Types.Page.t -> Odoc_document.Renderer.page
val render :
Odoc_document.Types.Page.t -> no_link:bool -> Odoc_document.Renderer.page
2 changes: 2 additions & 0 deletions src/markdown/link.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
open Odoc_document

let no_link = ref true

let for_printing url = List.map snd @@ Url.Path.to_list url

let segment_to_string (kind, name) =
Expand Down
13 changes: 11 additions & 2 deletions src/odoc/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,20 @@ module Odoc_latex = Make_renderer (struct
end)

module Odoc_markdown = Make_renderer (struct
type args = unit
type args = Markdown.args

let renderer = Markdown.renderer

let extra_args = Term.const ()
let no_link =
let doc =
"If no-link flag is passed, the rendered markdown\n\
\ output should not contain links, rather plain text."
in
Arg.(value & flag (info ~doc [ "no-link" ]))

let extra_args =
let f no_link = { Markdown.no_link } in
Term.(const f $ no_link)
end)

module Depends = struct
Expand Down
5 changes: 4 additions & 1 deletion src/odoc/markdown.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
open Odoc_document

let render _ page = Odoc_markdown.Generator.render page
type args = { no_link : bool }

let render args (page : Odoc_document.Types.Page.t) =
Odoc_markdown.Generator.render ~no_link:args.no_link page

let files_of_url url = Odoc_markdown.Link.files_of_url url

Expand Down

0 comments on commit 1704036

Please sign in to comment.