Repository navigation
Expand file tree
/
Copy pathCodeFormat.fs
More file actions
96 lines (73 loc) · 3.46 KB
/
Copy pathCodeFormat.fs
File metadata and controls
96 lines (73 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// --------------------------------------------------------------------------------------
// F# CodeFormat (Main.fs)
// (c) Tomas Petricek, 2012, Available under Apache 2.0 license.
// --------------------------------------------------------------------------------------
namespace FSharp.Formatting.CodeFormat
open System
/// <summary>
/// Represents an individual formatted snippet with title as key
/// </summary>
///
/// <namespacedoc>
/// <summary>Functionality relating to formatting F# scripts and code snippets</summary>
/// </namespacedoc>
type FormattedSnippet(key: string, content: string) =
/// Returns the title of the snippet
member x.Title = key
/// Returns the key of the snippet
member x.Key = key
/// Returns the formatted content code for the snipet
member x.Content = content
/// Represents formatted snippets
type FormattedContent(snippets: FormattedSnippet array, tips: string) =
/// Returns the processed snippets as an array
member x.Snippets = snippets
/// Returns string with ToolTip elements for all the snippets
member x.ToolTip = tips
/// Exposes functionality of the F# code formatter with a nice interface
type CodeFormat =
/// Formats the .fsx snippets as HTML. The parameters specify prefix for HTML tags, whether lines should
/// be added to outputs and whether errors should be printed.
static member FormatHtml
(snippets, prefix, ?openTag, ?closeTag, ?lineNumbers, ?openLinesTag, ?closeLinesTag, ?addErrors, ?tokenKindToCss) =
let openTag = defaultArg openTag "<pre class=\"fssnip\">"
let closeTag = defaultArg closeTag "</pre>"
let openLinesTag = defaultArg openLinesTag "<pre class=\"fsdocs-snippet-lines\" aria-hidden=\"true\">"
let closeLinesTag = defaultArg closeLinesTag "</pre>"
let lineNumbers = defaultArg lineNumbers true
let addErrors = defaultArg addErrors false
let tokenKindToCss = defaultArg tokenKindToCss CodeFormatHelper.defaultTokenMap
let snip, tip =
Html.formatSnippetsAsHtml
lineNumbers
addErrors
prefix
openTag
closeTag
openLinesTag
closeLinesTag
tokenKindToCss
snippets
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, ?lineNumbers) =
let lineNumbers = defaultArg lineNumbers true
let openTag = defaultArg openTag @"\begin{Verbatim}"
let closeTag = defaultArg closeTag @"\end{Verbatim}"
let snips = Latex.formatSnippetsAsLatex lineNumbers openTag closeTag snippets
let snips = Array.map FormattedSnippet snips
FormattedContent(snips, "")
/// Formats the .fsx snippets as iPython notebook using the default settings.
static member FormatFsx(snippets) =
let snips =
[|
for (Snippet(key, lines)) in snippets do
let str =
[| for (Line(originalLine, _spans)) in lines -> originalLine |]
|> String.concat Environment.NewLine
yield key, str
|]
let snips = Array.map FormattedSnippet snips
FormattedContent(snips, "")