Skip to content

Commit c7b85f0

Browse files
Repo AssistCopilot
authored andcommitted
Add /// doc comments to batch 2 modules (#1035)
Add XML doc comments to 5 key source files: - PynbModel.fs: Jupyter notebook model types - HtmlModel.fs: HTML element DSL types and Html builder module - CrossReferenceResolver.fs: cref resolution logic - GenerateMarkdown.fs: Markdown API doc renderer - GenerateHtml.fs: HTML API doc renderer Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent cacabf3 commit c7b85f0

5 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/FSharp.Formatting.ApiDocs/CrossReferenceResolver.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/// Resolves XML documentation cross-references (cref attributes) to hyperlinks pointing either to
2+
/// internal API doc pages or to external documentation (fsharp.github.io, learn.microsoft.com).
3+
/// Also manages URL base-name registration for all FSharp entities so internal links remain stable.
14
namespace rec FSharp.Formatting.ApiDocs
25

36
open System
@@ -24,7 +27,9 @@ open FSharp.Patterns
2427
open FSharp.Compiler.Syntax
2528

2629
[<AutoOpen>]
30+
/// Helpers for computing XML doc signature strings (the T:/M:/P:... identifiers) for FSharp symbols.
2731
module internal CrossReferences =
32+
/// Returns the XML doc signature for a type entity (e.g. "T:FSharp.Formatting.ApiDocs.ApiDocModel").
2833
let getXmlDocSigForType (typ: FSharpEntity) =
2934
if not (String.IsNullOrWhiteSpace typ.XmlDocSig) then
3035
typ.XmlDocSig
@@ -34,11 +39,14 @@ module internal CrossReferences =
3439
with _ ->
3540
""
3641

42+
/// Returns the single-letter XML doc sig prefix for a member: "E" for events, "P" for properties, "M" for other members.
3743
let getMemberXmlDocsSigPrefix (memb: FSharpMemberOrFunctionOrValue) =
3844
if memb.IsEvent then "E"
3945
elif memb.IsProperty then "P"
4046
else "M"
4147

48+
/// Returns the XML doc signature for a member (e.g. "M:MyNs.MyType.MyMethod(System.String)").
49+
/// Falls back to constructing one from the member's compiled name and type parameters when the compiler hasn't filled it in.
4250
let getXmlDocSigForMember (memb: FSharpMemberOrFunctionOrValue) =
4351
if not (String.IsNullOrWhiteSpace memb.XmlDocSig) then
4452
memb.XmlDocSig
@@ -122,11 +130,14 @@ module internal CrossReferences =
122130
| None -> ""
123131
| Some fullName -> sprintf "%s:%s.%s" (getMemberXmlDocsSigPrefix memb) fullName memberName
124132

133+
/// The result of resolving a cross-reference: a hyperlink URL plus a display name, and whether the target is internal.
125134
type internal CrefReference =
126135
{ IsInternal: bool
127136
ReferenceLink: string
128137
NiceName: string }
129138

139+
/// Resolves cref XML doc cross-references to URLs for the documentation site.
140+
/// Entities must be registered via <c>RegisterEntity</c> before resolution so that internal links can be built.
130141
type internal CrossReferenceResolver(root, collectionName, qualify, extensions) =
131142
let toReplace =
132143
([ ("Microsoft.", ""); (".", "-"); ("`", "-"); ("<", "_"); (">", "_"); (" ", "_"); ("#", "_") ]

src/FSharp.Formatting.ApiDocs/GenerateHtml.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// Generates HTML-format API documentation pages from an <c>ApiDocModel</c>.
2+
/// Produces one HTML file per entity and per namespace, suitable for embedding in an fsdocs template.
13
module internal FSharp.Formatting.ApiDocs.GenerateHtml
24

35
open System
@@ -10,16 +12,18 @@ open FSharp.Formatting.Templating
1012
open FSharp.Formatting.HtmlModel
1113
open FSharp.Formatting.HtmlModel.Html
1214

13-
/// Embed some HTML generated in GenerateModel
15+
/// Wraps pre-rendered API doc HTML as a raw <c>HtmlElement</c> text node.
1416
let embed (x: ApiDocHtml) = !!x.HtmlText
1517

18+
/// Wraps an <c>ApiDocHtml</c> summary in a styled paragraph, unless it already starts with a <pre> block.
1619
let fsdocsSummary (x: ApiDocHtml) =
1720
// the <pre> tag is not allowed inside a <p> tag.
1821
if x.HtmlText.StartsWith("<pre>", StringComparison.Ordinal) then
1922
embed x
2023
else
2124
div [ Class "fsdocs-summary-contents" ] [ p [ Class "fsdocs-summary" ] [ embed x ] ]
2225

26+
/// Renders API documentation pages in HTML format from the given <c>ApiDocModel</c>.
2327
type HtmlRender(model: ApiDocModel, ?menuTemplateFolder: string) =
2428
let root = model.Root
2529
let collectionName = model.Collection.CollectionName
@@ -694,6 +698,7 @@ type HtmlRender(model: ApiDocModel, ?menuTemplateFolder: string) =
694698

695699
[ yield (ParamKeys.``fsdocs-list-of-namespaces``, toc); yield ParamKeys.``fsdocs-body-class``, "api-docs" ]
696700

701+
/// Generates all HTML API doc pages into <paramref name="outDir"/>, applying the given template and global parameters.
697702
member _.Generate(outDir: string, templateOpt, collectionName, globalParameters) =
698703

699704
let getSubstitutons parameters toc (content: HtmlElement) pageTitle =

src/FSharp.Formatting.ApiDocs/GenerateMarkdown.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/// Generates Markdown-format API documentation pages from an <c>ApiDocModel</c>.
2+
/// Produces one Markdown file per entity and per namespace, suitable for rendering with a Markdown template.
13
module internal FSharp.Formatting.ApiDocs.GenerateMarkdown
24

35
open System
@@ -8,19 +10,26 @@ open FSharp.Formatting.Markdown
810
open FSharp.Formatting.Markdown.Dsl
911
open FSharp.Formatting.Templating
1012

13+
/// HTML-encodes a string and also escapes pipe characters (for use inside Markdown table cells).
1114
let encode (x: string) =
1215
HttpUtility.HtmlEncode(x).Replace("|", "&#124;")
1316

17+
/// URL-encodes a string for use inside Markdown links.
1418
let urlEncode (x: string) = HttpUtility.UrlEncode x
19+
/// Extracts the plain HTML text from an <c>ApiDocHtml</c> value (trimmed).
1520
let htmlString (x: ApiDocHtml) = (x.HtmlText.Trim())
1621

22+
/// Like <c>htmlString</c> but also replaces newlines with <br /> and escapes pipe characters (safe for table cells).
1723
let htmlStringSafe (x: ApiDocHtml) =
1824
(x.HtmlText.Trim()).Replace("\n", "<br />").Replace("|", "&#124;")
1925

26+
/// Wraps <c>ApiDocHtml</c> as an inline Markdown text node.
2027
let embed (x: ApiDocHtml) = !!(htmlString x)
28+
/// Like <c>embed</c> but uses the table-safe encoding.
2129
let embedSafe (x: ApiDocHtml) = !!(htmlStringSafe x)
2230
let br = !!"<br />"
2331

32+
/// Renders API documentation pages in Markdown format from the given <c>ApiDocModel</c>.
2433
type MarkdownRender(model: ApiDocModel, ?menuTemplateFolder: string) =
2534
let root = model.Root
2635
let collectionName = model.Collection.CollectionName
@@ -393,6 +402,7 @@ type MarkdownRender(model: ApiDocModel, ?menuTemplateFolder: string) =
393402
let toc = listOfNamespaces true true None
394403
[ yield (ParamKeys.``fsdocs-list-of-namespaces``, toc) ]
395404

405+
/// Generates all Markdown API doc pages into <paramref name="outDir"/>, applying the given template and global parameters.
396406
member _.Generate(outDir: string, templateOpt, collectionName, globalParameters) =
397407

398408
let getSubstitutons parameters toc (content: MarkdownDocument) pageTitle =

src/FSharp.Formatting.Common/HtmlModel.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
/// Internal DSL for building and rendering HTML element trees, used by the API docs and template rendering pipeline.
2+
/// Provides typed representations of HTML properties and elements, and a fluent builder API in the <c>Html</c> module.
13
namespace FSharp.Formatting.HtmlModel
24

35

6+
/// A discriminated union of all standard HTML attribute/property types.
7+
/// Each case represents one HTML attribute and carries its typed value.
48
type internal HtmlProperties =
59
| DefaultChecked of bool
610
| DefaultValue of string
@@ -298,6 +302,9 @@ type internal HtmlProperties =
298302
| Unselectable s -> sprintf "unselectable=\"%s\"" (if s then "true" else "false")
299303
| Custom(k, v) -> sprintf "%s=\"%s\"" k v
300304

305+
/// A typed representation of an HTML element tree node.
306+
/// Each case corresponds to an HTML tag. Leaf nodes hold <c>HtmlProperties</c> (attributes); branch nodes also hold child elements.
307+
/// The <c>String</c> case represents a raw text node; <c>EncodeString</c> HTML-encodes the text before output.
301308
type internal HtmlElement =
302309
private
303310
| A of props: HtmlProperties list * children: HtmlElement list
@@ -605,6 +612,8 @@ type internal HtmlElement =
605612

606613
helper 1 tag
607614

615+
/// Builder functions for creating <c>HtmlElement</c> values, one per HTML tag.
616+
/// Import this module (<c>open FSharp.Formatting.HtmlModel.Html</c>) to build element trees with a concise DSL.
608617
module internal Html =
609618
let a (props: HtmlProperties list) (children: HtmlElement list) = HtmlElement.A(props, children)
610619
let abbr (props: HtmlProperties list) (children: HtmlElement list) = HtmlElement.Abbr(props, children)

src/FSharp.Formatting.Common/PynbModel.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
/// Internal model for generating Jupyter notebook (.ipynb) files from literate scripts and F# Interactive sessions.
2+
/// Types in this module mirror the JSON structure of the Jupyter nbformat 4 spec.
13
module internal FSharp.Formatting.PynbModel
24

35
open System.Web
46

7+
/// Escapes a string for use as a JSON string value (with surrounding quotes).
58
let escapeAndQuote (txt: string) =
69
HttpUtility.JavaScriptStringEncode(txt, true)
710

11+
/// Ensures a string ends with a newline character, as required by the nbformat source line convention.
812
let addLineEnd (s: string) = if s.EndsWith '\n' then s else s + "\n"
913

14+
/// Represents the output data payload of a notebook cell execution result, keyed by MIME type.
1015
type OutputData =
1116
| OutputData of kind: string * lines: string array
1217

@@ -20,6 +25,7 @@ type OutputData =
2025
kind
2126
(String.concat ",\n" (Array.map escapeAndQuote lines))
2227

28+
/// Represents a single output produced by executing a notebook code cell.
2329
type Output =
2430
{ data: OutputData
2531
execution_count: int option
@@ -42,6 +48,7 @@ type Output =
4248
this.metadata
4349
this.output_type
4450

51+
/// Represents a single cell in a Jupyter notebook (code, markdown, or raw).
4552
type Cell =
4653
{ cell_type: string
4754
execution_count: int option
@@ -91,6 +98,7 @@ type Cell =
9198
|> Array.map (addLineEnd >> escapeAndQuote)
9299
|> String.concat ",\n ")
93100

101+
/// Metadata describing the kernel (language runtime) used to execute the notebook.
94102
type Kernelspec =
95103
{ display_name: string
96104
language: string
@@ -112,6 +120,7 @@ type Kernelspec =
112120
(escapeAndQuote this.language)
113121
(escapeAndQuote this.name)
114122

123+
/// Metadata describing the programming language used in the notebook (for syntax highlighting and MIME types).
115124
type LanguageInfo =
116125
{ file_extension: string
117126
mimetype: string
@@ -137,6 +146,7 @@ type LanguageInfo =
137146
(escapeAndQuote this.name)
138147
(escapeAndQuote this.pygments_lexer)
139148

149+
/// Polyglot Notebooks kernel info, identifying the default kernel for the notebook.
140150
type DefaultKernelInfo =
141151
{ defaultKernelName: string
142152
languageName: string
@@ -165,6 +175,7 @@ type DefaultKernelInfo =
165175
(escapeAndQuote this.languageName)
166176
(escapeAndQuote this.name)
167177

178+
/// Top-level notebook metadata combining kernel spec, language info, and Polyglot Notebooks extension data.
168179
type Metadata =
169180
{ kernelspec: Kernelspec
170181
language_info: LanguageInfo
@@ -186,6 +197,7 @@ type Metadata =
186197
this.language_info
187198
this.defaultKernelInfo
188199

200+
/// The root Jupyter notebook document, containing all cells and metadata.
189201
type Notebook =
190202
{ nbformat: int
191203
nbformat_minor: int
@@ -216,6 +228,7 @@ type Notebook =
216228
let internal splitLines (s: string) =
217229
s.Replace("\r\n", "\n").Split([| '\n' |])
218230

231+
/// Creates a code cell with the given source lines, execution count, and outputs.
219232
let codeCell (lines: string array) executionCount outputs =
220233
let lines = lines |> Array.collect splitLines |> Array.map addLineEnd
221234

@@ -228,11 +241,13 @@ let codeCell (lines: string array) executionCount outputs =
228241

229242
cell
230243

244+
/// Creates a raw (non-executed) cell with the given content string.
231245
let rawCell (s: string) =
232246
{ Cell.Default with
233247
cell_type = "raw"
234248
source = splitLines s }
235249

250+
/// Creates a markdown cell with the given source lines.
236251
let markdownCell (lines: string array) =
237252
let lines = lines |> Array.collect splitLines |> Array.map addLineEnd
238253

0 commit comments

Comments
 (0)