Skip to content

Commit

Permalink
Merge pull request fsprojects#724 from dsyme/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
dsyme authored Nov 10, 2021
2 parents 3003767 + f19addd commit e254d7f
Show file tree
Hide file tree
Showing 19 changed files with 327 additions and 186 deletions.
6 changes: 6 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 13.0.0

* Remove unused TransformAndOutputDocument from API
* Fixes Can't yet format InlineHtmlBlock #723
* Fixes `<code>` blocks are emitting <pre> blocks with escapes no longer escaped #712

## 12.0.2

* Remove front-matter output from notebooks
Expand Down
18 changes: 13 additions & 5 deletions src/FSharp.Formatting.ApiDocs/GenerateModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open System.Reflection
open System.Collections.Generic
open System.Text
open System.IO
open System.Web
open System.Xml
open System.Xml.Linq

Expand Down Expand Up @@ -1953,9 +1954,10 @@ module internal SymbolReader =
html.Append("</p>") |> ignore
| "paramref" ->
let name = elem.Attribute(XName.Get "name")
let nameAsHtml = HttpUtility.HtmlEncode name.Value

if name <> null then
html.AppendFormat("<span class=\"fsdocs-param-name\">{0}</span>", name.Value)
html.AppendFormat("<span class=\"fsdocs-param-name\">{0}</span>", nameAsHtml)
|> ignore
| "see"
| "seealso" ->
Expand All @@ -1980,25 +1982,31 @@ module internal SymbolReader =
|> ignore
| _ ->
urlMap.ResolveCref cname |> ignore
html.AppendFormat("{0}", cref.Value) |> ignore
//let crefAsHtml = HttpUtility.HtmlEncode cref.Value
html.Append(cref.Value) |> ignore
| "c" ->
html.Append("<code>") |> ignore

html.Append(elem.Value.TrimEnd('\r', '\n', ' ')) |> ignore
let code = elem.Value.TrimEnd('\r', '\n', ' ')
let codeAsHtml = HttpUtility.HtmlEncode code
html.Append(codeAsHtml) |> ignore

html.Append("</code>") |> ignore
| "code" ->
html.Append("<pre>") |> ignore

html.Append(elem.Value.TrimEnd('\r', '\n', ' ')) |> ignore
let code = elem.Value.TrimEnd('\r', '\n', ' ')
let codeAsHtml = HttpUtility.HtmlEncode code
html.Append(codeAsHtml) |> ignore

html.Append("</pre>") |> ignore
// 'a' is not part of the XML doc standard but is widely used
| "a" -> html.Append(elem.ToString()) |> ignore
// This allows any HTML to be transferred through
| _ ->
if anyTagsOK then
html.Append(elem.ToString()) |> ignore
let elemAsXml = elem.ToString()
html.Append(elemAsXml) |> ignore

let readXmlCommentAsHtmlAux
summaryExpected
Expand Down
21 changes: 15 additions & 6 deletions src/FSharp.Formatting.CodeFormat/CodeFormat.fs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type CodeFormat =
prefix,
?openTag,
?closeTag,
?addLines,
?lineNumbers,
?openLinesTag,
?closeLinesTag,
?addErrors,
Expand All @@ -90,26 +90,35 @@ type CodeFormat =
let closeTag = defaultArg closeTag "</pre>"
let openLinesTag = defaultArg openLinesTag openTag
let closeLinesTag = defaultArg closeLinesTag closeTag
let addLines = defaultArg addLines true
let lineNumbers = defaultArg lineNumbers true
let addErrors = defaultArg addErrors false

let tokenKindToCss = defaultArg tokenKindToCss CodeFormatHelper.defaultTokenMap

let snip, tip =
Html.format addLines addErrors prefix openTag closeTag openLinesTag closeLinesTag snippets tokenKindToCss
Html.formatSnippetsAsHtml
lineNumbers
addErrors
prefix
openTag
closeTag
openLinesTag
closeLinesTag
snippets
tokenKindToCss

let snip = [| for key, h in snip -> FormattedSnippet(key, h) |]

FormattedContent(snip, tip)

/// Formats the .fsx snippets as LaTeX. The parameters specify prefix for LaTeX tags, whether lines should
/// be added to outputs.
static member FormatLatex(snippets, ?openTag, ?closeTag, ?addLines) =
let addLines = defaultArg addLines true
static member FormatLatex(snippets, ?openTag, ?closeTag, ?lineNumbers) =
let lineNumbers = defaultArg lineNumbers true
let openTag = defaultArg openTag @"\begin{Verbatim}"
let closeTag = defaultArg closeTag @"\end{Verbatim}"

let snips = Latex.format addLines openTag closeTag snippets
let snips = Latex.formatSnippetsAsLatex lineNumbers openTag closeTag snippets

let snips = Array.map FormattedSnippet snips
FormattedContent(snips, "")
Expand Down
20 changes: 15 additions & 5 deletions src/FSharp.Formatting.CodeFormat/HtmlFormatting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type ToolTipFormatter(prefix) =

/// Represents context used by the formatter
type FormattingContext =
{ AddLines: bool
{ GenerateLineNumbers: bool
GenerateErrors: bool
Writer: TextWriter
OpenTag: string
Expand Down Expand Up @@ -175,7 +175,7 @@ let formatSnippets (ctx: FormattingContext) (snippets: Snippet []) =

// If we're adding lines, then generate two column table
// (so that the body can be easily copied)
if ctx.AddLines then
if ctx.GenerateLineNumbers then
ctx.Writer.Write("<table class=\"pre\">")
ctx.Writer.Write("<tr>")
ctx.Writer.Write("<td class=\"lines\">")
Expand Down Expand Up @@ -204,7 +204,7 @@ let formatSnippets (ctx: FormattingContext) (snippets: Snippet []) =

emitTag ctx.CloseTag

if ctx.AddLines then
if ctx.GenerateLineNumbers then
// Close the table if we are adding lines
ctx.Writer.WriteLine("</td>")
ctx.Writer.WriteLine("</tr>")
Expand All @@ -215,11 +215,21 @@ let formatSnippets (ctx: FormattingContext) (snippets: Snippet []) =

/// Format snippets and return HTML for <pre> tags together
/// wtih HTML for ToolTips (to be added to the end of document)
let format addLines addErrors prefix openTag closeTag openLinesTag closeLinesTag (snippets: Snippet []) tokenKindToCss =
let formatSnippetsAsHtml
lineNumbers
addErrors
prefix
openTag
closeTag
openLinesTag
closeLinesTag
(snippets: Snippet [])
tokenKindToCss
=
let tipf = ToolTipFormatter prefix

let ctx =
{ AddLines = addLines
{ GenerateLineNumbers = lineNumbers
GenerateErrors = addErrors
Writer = null
FormatTip = tipf.FormatTip
Expand Down
12 changes: 6 additions & 6 deletions src/FSharp.Formatting.CodeFormat/LatexFormatting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let latexEncode s =

/// Represents context used by the formatter
type FormattingContext =
{ AddLines: bool
{ GenerateLineNumbers: bool
Writer: TextWriter
OpenTag: string
CloseTag: string }
Expand Down Expand Up @@ -97,10 +97,10 @@ let formatSnippets (ctx: FormattingContext) (snippets: Snippet []) =
ctx.Writer.Write(ctx.OpenTag)

// Line numbers belong to the tag
if ctx.AddLines then
ctx.Writer.WriteLine(@"[commandchars=\\\{\}, numbers=left]")
if ctx.GenerateLineNumbers then
ctx.Writer.WriteLine(@"[escapeinside=\\\{\}, numbers=left]")
else
ctx.Writer.WriteLine(@"[commandchars=\\\{\}]")
ctx.Writer.WriteLine(@"[escapeinside=\\\{\}]")

// Print all lines of the snippet
lines
Expand All @@ -119,9 +119,9 @@ let formatSnippets (ctx: FormattingContext) (snippets: Snippet []) =

/// Format snippets and return LaTEX for <pre> tags together
/// (to be added to the end of document)
let format addLines openTag closeTag (snippets: Snippet []) =
let formatSnippetsAsLatex lineNumbers openTag closeTag (snippets: Snippet []) =
let ctx =
{ AddLines = addLines
{ GenerateLineNumbers = lineNumbers
Writer = null
OpenTag = openTag
CloseTag = closeTag }
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Formatting.CodeFormat/PynbFormatting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open System
open FSharp.Formatting.CodeFormat

/// Generate Pynb code cell text with the specified snippets
let format (snippets: Snippet []) =
let formatSnippetsAsPynb (snippets: Snippet []) =
[| for (Snippet (key, lines)) in snippets do
let str =
[| for (Line (originalLine, _spans)) in lines -> originalLine |]
Expand Down
5 changes: 2 additions & 3 deletions src/FSharp.Formatting.Literate/Contexts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ type OutputKind =
| Fsx

/// Requests Markdown output
| Md
| Markdown
member x.Extension =
match x with
| Fsx -> "fsx"
| Latex -> "tex"
| Md -> "md"
| Markdown -> "md"
| Html -> "html"
| Pynb -> "ipynb"


/// Defines the output of processing a literate doc
type internal LiterateDocModel =
{
Expand Down
4 changes: 2 additions & 2 deletions src/FSharp.Formatting.Literate/Formatting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module internal Formatting =
crefResolver = crefResolver,
mdlinkResolver = mdlinkResolver
)
| OutputKind.Md ->
| OutputKind.Markdown ->
Markdown.ToMd(
doc,
substitutions = substitutions,
Expand Down Expand Up @@ -75,7 +75,7 @@ module internal Formatting =
/// entire source code of the specified document (with possible fsx formatting)
let getSourceDocument (doc: LiterateDocument) =
match doc.Source with
| LiterateSource.Markdown text -> doc.With(paragraphs = [ CodeBlock(text, None, "", "", None) ])
| LiterateSource.Markdown text -> doc.With(paragraphs = [ CodeBlock(text, None, None, "", "", None) ])
| LiterateSource.Script snippets ->
let mutable count = 0

Expand Down
47 changes: 14 additions & 33 deletions src/FSharp.Formatting.Literate/Literate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,12 @@ type Literate private () =
mdlinkResolver

let doc = Transformations.replaceLiterateParagraphs ctx doc
Markdown.ToLatex(MarkdownDocument(doc.Paragraphs, doc.DefinedLinks), ?substitutions = substitutions)

Markdown.ToLatex(
MarkdownDocument(doc.Paragraphs, doc.DefinedLinks),
?substitutions = substitutions,
?lineNumbers = lineNumbers
)

/// Write the literate document as Latex without using a template
static member WriteLatex
Expand Down Expand Up @@ -382,7 +387,13 @@ type Literate private () =
mdlinkResolver

let doc = Transformations.replaceLiterateParagraphs ctx doc
Markdown.WriteLatex(MarkdownDocument(doc.Paragraphs, doc.DefinedLinks), writer, ?substitutions = substitutions)

Markdown.WriteLatex(
MarkdownDocument(doc.Paragraphs, doc.DefinedLinks),
writer,
?substitutions = substitutions,
?lineNumbers = lineNumbers
)

/// Formate the literate document as an iPython notebook
static member ToPynb(doc: LiterateDocument, ?substitutions, ?crefResolver, ?mdlinkResolver) =
Expand Down Expand Up @@ -459,6 +470,7 @@ type Literate private () =

let parseOptions =
match outputKind with
| OutputKind.Markdown
| OutputKind.Fsx
| OutputKind.Pynb -> parseOptions ||| MarkdownParseOptions.ParseCodeAsOther
//||| MarkdownParseOptions.ParseNonCodeAsOther
Expand Down Expand Up @@ -549,37 +561,6 @@ type Literate private () =
let outputPath = defaultOutput output input outputKind
Formatting.transformDocument doc outputPath ctx

static member TransformAndOutputDocument
(
doc,
output,
?template,
?outputKind,
?prefix,
?lineNumbers,
?generateAnchors,
?substitutions,
?crefResolver,
?mdlinkResolver
) =
let crefResolver = defaultArg crefResolver (fun _ -> None)
let mdlinkResolver = defaultArg mdlinkResolver (fun _ -> None)
let outputKind = defaultArg outputKind OutputKind.Html

let ctx =
makeFormattingContext
outputKind
prefix
lineNumbers
generateAnchors
substitutions
None
crefResolver
mdlinkResolver

let res = Formatting.transformDocument doc output ctx
SimpleTemplating.UseFileAsSimpleTemplate(res.Substitutions, template, output)

/// Convert a markdown file into HTML or another output kind
static member ConvertMarkdownFile
(
Expand Down
Loading

0 comments on commit e254d7f

Please sign in to comment.