Skip to content

Asset references #1002

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

Closed
wants to merge 14 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Improve jump to implementation in rendered source code, and add a
`count-occurrences` flag and command to count occurrences of every identifiers
(@panglesd, #976)
- Add ability to reference assets (@panglesd, #1002)

# 2.4.0

Expand Down
2 changes: 2 additions & 0 deletions doc/ocamldoc_differences.mld
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The following describes the changes between what [odoc] understands and what’s
- [odoc] has a better mechanism for disambiguating references in comments. See 'reference syntax' later in this document.
- Built-in support for standalone [.mld] files. These are documents using the OCamldoc markup, but they’re rendered as distinct pages.
- Structured output: [odoc] can produce output in a structured directory tree rather a set of files.
- [odoc] support the inclusion of assets in the structured directory tree.
- A few extra tags are supported:
+ [@returns] is a synonym for [@return]
+ [@raises] is a synonym for [@raise]
Expand All @@ -56,6 +57,7 @@ Additionally we support extra annotations:
- [instance-variable] refers to instance variables
- [label] refers to labels introduced in anchors
- [page] refers to [.mld] pages as outlined above
- [asset] refers assets as outlined above
- [value] is recognised as [val]

{3 Referencing items containing hyphens or dots}
Expand Down
5 changes: 3 additions & 2 deletions doc/odoc_for_authors.mld
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,14 @@ The prefixes supported are:
- [instance-variable]
- [section] (and the equivalent deprecated prefix [label]) - for referring to headings
- [page] - for referring to [.mld] pages
- [asset] - for referring to assets

In some cases the element being referenced might have a hyphen, a dot or a space in the name,
e.g. if trying to refer to a page from a [.mld] file "1.2.3.mld". In this case, the
e.g. if trying to refer to a page from a [.mld] file "1.2.3.mld", or to an asset, like a [.txt] file "file.txt". In this case, the
element name should be quoted with double quote marks:

{v
{!page-"1.2.3"}
{!page-"1.2.3"}, {!asset-"file.txt"}
v}


Expand Down
8 changes: 4 additions & 4 deletions doc/parent_child_spec.mld
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,17 @@ installed and might be used by a different driver.
In order for drivers to build consistent documentation for a package, the
following convention should be followed.

- [.mld] pages are installed in a package's [share] directory, under the
[odoc-pages] sub-directory.
- A page is the parent of every installed pages. The driver can freely name this
- [.mld] pages and assets are installed in a package's [doc] directory, under the
[odoc-pages] sub-directory. This means that assets with the `.mld` extension are recognized as pages.
- A page is the parent of every installed pages and assets. The driver can freely name this
page, for example it can be named after the package. In what follows, we
refer to this page as the [pkg] page.
- If there is an installed [index.mld] file, the driver has to use it as
content for the [pkg] page.
- If there is no installed [index.mld] page, the driver has to generate some
content for the [pkg] page.

This convention is followed by the
This convention (excluding assets) is followed by the
{{:https://github.com/ocaml-doc/voodoo}driver for ocaml.org},
by the driver {{:https://erratique.ch/software/odig/doc/packaging.html}Odig}
and by the build system {{:https://github.com/ocaml/dune}Dune}.
1 change: 1 addition & 0 deletions src/document/comment.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module Reference = struct
| `InstanceVariable (p, f) ->
render_unresolved (p :> t) ^ "." ^ InstanceVariableName.to_string f
| `Label (p, f) -> render_unresolved (p :> t) ^ "." ^ LabelName.to_string f
| `Asset (p, f) -> render_unresolved (p :> t) ^ "." ^ AssetName.to_string f

(* This is the entry point. *)
let to_ir : ?text:Inline.t -> Reference.t -> Inline.t =
Expand Down
1 change: 1 addition & 0 deletions src/model/names.ml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ module LabelName = SimpleName
module PageName = SimpleName
module DefName = SimpleName
module LocalName = SimpleName
module AssetName = SimpleName
2 changes: 2 additions & 0 deletions src/model/names.mli
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ module PageName : SimpleName
module DefName : SimpleName

module LocalName : SimpleName

module AssetName : SimpleName
8 changes: 8 additions & 0 deletions src/model/paths.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,10 @@ module Reference = struct
module Page = struct
type t = Paths_types.Resolved_reference.page
end

module Asset = struct
type t = Paths_types.Resolved_reference.asset
end
end

type t = Paths_types.Reference.any
Expand Down Expand Up @@ -1143,4 +1147,8 @@ module Reference = struct
module Page = struct
type t = Paths_types.Reference.page
end

module Asset = struct
type t = Paths_types.Reference.asset
end
end
8 changes: 8 additions & 0 deletions src/model/paths.mli
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@ module rec Reference : sig
type t = Paths_types.Resolved_reference.page
end

module Asset : sig
type t = Paths_types.Resolved_reference.asset
end

type t = Paths_types.Resolved_reference.any

val identifier : t -> Identifier.t
Expand Down Expand Up @@ -631,6 +635,10 @@ module rec Reference : sig
type t = Paths_types.Reference.page
end

module Asset : sig
type t = Paths_types.Reference.asset
end

type t = Paths_types.Reference.any

type tag_any = Paths_types.Reference.tag_any
Expand Down
20 changes: 16 additions & 4 deletions src/model/paths_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ module Identifier = struct
type reference_label = label

type reference_page = page

type reference_asset = asset_file
end

module rec Path : sig
Expand Down Expand Up @@ -548,6 +550,7 @@ module rec Reference : sig
| `TInstanceVariable
| `TLabel
| `TPage
| `TAsset
| `TChildPage
| `TChildModule
| `TUnknown ]
Expand Down Expand Up @@ -720,9 +723,7 @@ module rec Reference : sig
(** @canonical Odoc_model.Paths.Reference.Label.t *)

type page =
[ `Resolved of Resolved_reference.page
| `Root of string * [ `TPage | `TUnknown ]
| `Dot of label_parent * string ]
[ `Root of string * [ `TPage | `TUnknown ] | `Dot of label_parent * string ]
(** @canonical Odoc_model.Paths.Reference.Page.t *)

type any =
Expand All @@ -742,8 +743,16 @@ module rec Reference : sig
| `ClassType of signature * ClassTypeName.t
| `Method of class_signature * MethodName.t
| `InstanceVariable of class_signature * InstanceVariableName.t
| `Label of label_parent * LabelName.t ]
| `Label of label_parent * LabelName.t
| `Asset of page * AssetName.t ]
(** @canonical Odoc_model.Paths.Reference.t *)

type asset =
[ `Resolved of Resolved_reference.asset
| `Root of string * [ `TAsset ]
| `Dot of label_parent * string
| `Asset of page * AssetName.t ]
(** @canonical Odoc_model.Paths.Reference.Asset.t *)
end =
Reference

Expand Down Expand Up @@ -908,5 +917,8 @@ and Resolved_reference : sig
| `InstanceVariable of class_signature * InstanceVariableName.t
| `Label of label_parent * LabelName.t ]
(** @canonical Odoc_model.Paths.Reference.Resolved.t *)

type asset = [ `Identifier of Identifier.reference_asset ]
(** @canonical Odoc_model.Paths.Reference.Resolved.Asset.t *)
end =
Resolved_reference
44 changes: 29 additions & 15 deletions src/model/reference.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ let should_not_be_empty : what:string -> Location_.span -> Error.t =
fun ~what ->
Error.make "%s should not be empty." (Astring.String.Ascii.capitalize what)

let not_allowed :
?suggestion:string ->
what:string ->
in_what:string ->
Location_.span ->
Error.t =
fun ?suggestion ~what ~in_what ->
Error.make ?suggestion "%s is not allowed in %s."
let should_be_first :
?suggestion:string -> what:string -> Location_.span -> Error.t =
fun ?suggestion ~what ->
Error.make ?suggestion "%s is not allowed to have a parent."
(Astring.String.Ascii.capitalize what)
in_what

let deprecated_reference_kind location kind replacement =
deprecated_reference_kind kind replacement location |> Error.raise_warning
Expand Down Expand Up @@ -76,6 +71,7 @@ let match_extra_odoc_reference_kind (_location as loc) s :
Some `TLabel
| Some "module-type" -> Some `TModuleType
| Some "page" -> Some `TPage
| Some "asset" -> Some `TAsset
| Some "value" ->
d loc "value" "val";
Some `TValue
Expand Down Expand Up @@ -298,6 +294,26 @@ let parse whole_reference_location s :
|> Error.raise_exception)
in

let page (kind, identifier, location) tokens : Page.t =
let kind = match_reference_kind location kind in
match tokens with
| [] -> (
match kind with
| (`TUnknown | `TPage) as kind -> `Root (identifier, kind)
| _ -> expected [ "page" ] location |> Error.raise_exception)
| _ :: _ -> (
match kind with
| (`TUnknown | `TPage) as k ->
let suggestion =
match k with
| `TUnknown -> Printf.sprintf "'%s' should be first." identifier
| `TPage -> Printf.sprintf "'page-%s' should be first." identifier
in
should_be_first ~what:"Page label" ~suggestion location
|> Error.raise_exception
| _ -> expected [ "page" ] location |> Error.raise_exception)
in

let start_from_last_component (kind, identifier, location) old_kind tokens =
let new_kind = match_reference_kind location kind in
let kind =
Expand Down Expand Up @@ -363,21 +379,19 @@ let parse whole_reference_location s :
| `TLabel ->
`Label
(label_parent next_token tokens, LabelName.make_std identifier)
| `TAsset ->
`Asset (page next_token tokens, AssetName.make_std identifier)
| `TChildPage | `TChildModule ->
let suggestion =
Printf.sprintf "'child-%s' should be first." identifier
in
not_allowed ~what:"Child label"
~in_what:"the last component of a reference path" ~suggestion
location
should_be_first ~what:"Child label" ~suggestion location
|> Error.raise_exception
| `TPage ->
let suggestion =
Printf.sprintf "'page-%s' should be first." identifier
in
not_allowed ~what:"Page label"
~in_what:"the last component of a reference path" ~suggestion
location
should_be_first ~what:"Page label" ~suggestion location
|> Error.raise_exception)
in

Expand Down
7 changes: 6 additions & 1 deletion src/model_desc/paths_desc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ module Names = struct

let labelname = To_string LabelName.to_string

let assetname = To_string AssetName.to_string

let pagename = To_string PageName.to_string

let parametername = To_string ModuleName.to_string
Expand Down Expand Up @@ -200,6 +202,7 @@ module General_paths = struct
| `TType -> C0 "`TType"
| `TUnknown -> C0 "`TUnknown"
| `TValue -> C0 "`TValue"
| `TAsset -> C0 "`TAsset"
| `TChildPage -> C0 "`TChildPage"
| `TChildModule -> C0 "`TChildModule")

Expand Down Expand Up @@ -329,7 +332,9 @@ module General_paths = struct
((x1 :> r), x2),
Pair (reference, Names.instancevariablename) )
| `Label (x1, x2) ->
C ("`Label", ((x1 :> r), x2), Pair (reference, Names.labelname)))
C ("`Label", ((x1 :> r), x2), Pair (reference, Names.labelname))
| `Asset (x1, x2) ->
C ("`Asset", ((x1 :> r), x2), Pair (reference, Names.assetname)))

and resolved_reference : rr t =
Variant
Expand Down
28 changes: 27 additions & 1 deletion src/xref2/component.ml
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ module Element = struct
(* No component for pages yet *)
type page = [ `Page of Identifier.Page.t * Odoc_model.Lang.Page.t ]

type asset = [ `Asset of Identifier.AssetFile.t ]

type label_parent = [ signature | type_ | page ]

type fragment_type_parent = [ signature | datatype ]
Expand All @@ -543,7 +545,8 @@ module Element = struct
| extension
| extension_decl
| field
| page ]
| page
| asset ]

let identifier : [< any ] -> Odoc_model.Paths.Identifier.t =
let open Odoc_model.Paths.Identifier in
Expand All @@ -561,6 +564,7 @@ module Element = struct
| `Extension (id, _, _) -> (id :> t)
| `ExtensionDecl (id, _) -> (id :> t)
| `Page (id, _) -> (id :> t)
| `Asset id -> (id :> t)
end

module Fmt = struct
Expand Down Expand Up @@ -1499,6 +1503,11 @@ module Fmt = struct
(parent :> t)
(LabelName.to_string name)

and model_resolved_asset_reference ppf
(`Identifier id : Odoc_model.Paths.Reference.Resolved.Asset.t) =
Format.fprintf ppf "%a" model_identifier
(id :> Odoc_model.Paths.Identifier.t)

and model_reference ppf (r : Odoc_model.Paths.Reference.t) =
let open Odoc_model.Paths.Reference in
match r with
Expand Down Expand Up @@ -1562,6 +1571,23 @@ module Fmt = struct
Format.fprintf ppf "%a.%s" model_reference
(parent :> t)
(LabelName.to_string name)
| `Asset (parent, name) ->
Format.fprintf ppf "%a.%s" model_reference
(parent :> t)
(AssetName.to_string name)

and model_asset_reference ppf (r : Odoc_model.Paths.Reference.Asset.t) =
let open Odoc_model.Paths.Reference in
match r with
| `Resolved r' ->
Format.fprintf ppf "r(%a)" model_resolved_asset_reference r'
| `Root (name, _) -> Format.fprintf ppf "unresolvedroot(%s)" name
| `Dot (parent, str) ->
Format.fprintf ppf "%a.%s" model_reference (parent :> t) str
| `Asset (parent, name) ->
Format.fprintf ppf "%a.%s" model_reference
(parent :> t)
(AssetName.to_string name)
end

module LocalIdents = struct
Expand Down
11 changes: 10 additions & 1 deletion src/xref2/component.mli
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ module Element : sig
(* No component for pages yet *)
type page = [ `Page of Identifier.Page.t * Odoc_model.Lang.Page.t ]

type asset = [ `Asset of Identifier.AssetFile.t ]

type label_parent = [ signature | type_ | page ]

type fragment_type_parent = [ signature | datatype ]
Expand All @@ -507,7 +509,8 @@ module Element : sig
| extension
| extension_decl
| field
| page ]
| page
| asset ]

val identifier : [< any ] -> Identifier.t
end
Expand Down Expand Up @@ -636,7 +639,13 @@ module Fmt : sig
val model_resolved_reference :
Format.formatter -> Odoc_model.Paths.Reference.Resolved.t -> unit

val model_resolved_asset_reference :
Format.formatter -> Odoc_model.Paths.Reference.Resolved.Asset.t -> unit

val model_reference : Format.formatter -> Odoc_model.Paths.Reference.t -> unit

val model_asset_reference :
Format.formatter -> Odoc_model.Paths.Reference.Asset.t -> unit
end

module Of_Lang : sig
Expand Down
Loading