forked from CommunityToolkit/WindowsCommunityToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageInline.cs
More file actions
259 lines (217 loc) · 8.52 KB
/
Copy pathImageInline.cs
File metadata and controls
259 lines (217 loc) · 8.52 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using Microsoft.Toolkit.Parsers.Core;
using Microsoft.Toolkit.Parsers.Markdown.Helpers;
namespace Microsoft.Toolkit.Parsers.Markdown.Inlines
{
/// <summary>
/// Represents an embedded image.
/// </summary>
public class ImageInline : MarkdownInline, IInlineLeaf
{
/// <summary>
/// Initializes a new instance of the <see cref="ImageInline"/> class.
/// </summary>
public ImageInline()
: base(MarkdownInlineType.Image)
{
}
/// <summary>
/// Gets or sets the image URL.
/// </summary>
public string Url { get; set; }
/// <summary>
/// Gets or sets the image Render URL.
/// </summary>
public string RenderUrl { get; set; }
/// <summary>
/// Gets or sets a text to display on hover.
/// </summary>
public string Tooltip { get; set; }
/// <inheritdoc/>
public string Text { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the ID of a reference, if this is a reference-style link.
/// </summary>
public string ReferenceId { get; set; }
/// <summary>
/// Gets image width
/// If value is greater than 0, ImageStretch is set to UniformToFill
/// If both ImageWidth and ImageHeight are greater than 0, ImageStretch is set to Fill
/// </summary>
public int ImageWidth { get; internal set; }
/// <summary>
/// Gets image height
/// If value is greater than 0, ImageStretch is set to UniformToFill
/// If both ImageWidth and ImageHeight are greater than 0, ImageStretch is set to Fill
/// </summary>
public int ImageHeight { get; internal set; }
internal static void AddTripChars(List<InlineTripCharHelper> tripCharHelpers)
{
tripCharHelpers.Add(new InlineTripCharHelper() { FirstChar = '!', Method = InlineParseMethod.Image });
}
/// <summary>
/// Attempts to parse an image e.g. "".
/// </summary>
/// <param name="markdown"> The markdown text. </param>
/// <param name="start"> The location to start parsing. </param>
/// <param name="end"> The location to stop parsing. </param>
/// <returns> A parsed markdown image, or <c>null</c> if this is not a markdown image. </returns>
internal static InlineParseResult Parse(string markdown, int start, int end)
{
// Expect a '!' character.
if (start >= end || markdown[start] != '!')
{
return null;
}
int pos = start + 1;
// Then a '[' character
if (pos >= end || markdown[pos] != '[')
{
return null;
}
pos++;
// Find the ']' character
while (pos < end)
{
if (markdown[pos] == ']')
{
break;
}
pos++;
}
if (pos == end)
{
return null;
}
// Extract the alt.
string tooltip = markdown.Substring(start + 2, pos - (start + 2));
// Expect the '(' character.
pos++;
string reference = string.Empty;
string url = string.Empty;
int imageWidth = 0;
int imageHeight = 0;
if (pos < end && markdown[pos] == '[')
{
int refstart = pos;
// Find the reference ']' character
while (pos < end)
{
if (markdown[pos] == ']')
{
break;
}
pos++;
}
reference = markdown.Substring(refstart + 1, pos - refstart - 1);
}
else if (pos < end && markdown[pos] == '(')
{
while (pos < end && ParseHelpers.IsMarkdownWhiteSpace(markdown[pos]))
{
pos++;
}
// Extract the URL.
int urlStart = pos;
while (pos < end && markdown[pos] != ')')
{
pos++;
}
var imageDimensionsPos = markdown.IndexOf(" =", urlStart, pos - urlStart, StringComparison.Ordinal);
url = imageDimensionsPos > 0
? TextRunInline.ResolveEscapeSequences(markdown, urlStart + 1, imageDimensionsPos)
: TextRunInline.ResolveEscapeSequences(markdown, urlStart + 1, pos);
if (imageDimensionsPos > 0)
{
// trying to find 'x' which separates image width and height
var dimensionsSepatorPos = markdown.IndexOf("x", imageDimensionsPos + 2, pos - imageDimensionsPos - 1, StringComparison.Ordinal);
// didn't find separator, trying to parse value as imageWidth
if (dimensionsSepatorPos == -1)
{
var imageWidthStr = markdown.Substring(imageDimensionsPos + 2, pos - imageDimensionsPos - 2);
int.TryParse(imageWidthStr, out imageWidth);
}
else
{
var dimensions = markdown.Substring(imageDimensionsPos + 2, pos - imageDimensionsPos - 2).Split('x');
// got width and height
if (dimensions.Length == 2)
{
int.TryParse(dimensions[0], out imageWidth);
int.TryParse(dimensions[1], out imageHeight);
}
}
}
}
if (pos == end)
{
return null;
}
// We found something!
var result = new ImageInline
{
Tooltip = tooltip,
RenderUrl = url,
ReferenceId = reference,
Url = url,
Text = markdown.Substring(start, pos + 1 - start),
ImageWidth = imageWidth,
ImageHeight = imageHeight
};
return new InlineParseResult(result, start, pos + 1);
}
/// <summary>
/// If this is a reference-style link, attempts to converts it to a regular link.
/// </summary>
/// <param name="document"> The document containing the list of references. </param>
internal void ResolveReference(MarkdownDocument document)
{
if (document == null)
{
throw new ArgumentNullException("document");
}
if (ReferenceId == null)
{
return;
}
// Look up the reference ID.
var reference = document.LookUpReference(ReferenceId);
if (reference == null)
{
return;
}
// The reference was found. Check the URL is valid.
if (!Common.IsUrlValid(reference.Url))
{
return;
}
// Everything is cool when you're part of a team.
RenderUrl = reference.Url;
ReferenceId = null;
}
/// <summary>
/// Converts the object into it's textual representation.
/// </summary>
/// <returns> The textual representation of this object. </returns>
public override string ToString()
{
if (ImageWidth > 0 && ImageHeight > 0)
{
return string.Format("![{0}]: {1} (Width: {2}, Height: {3})", Tooltip, Url, ImageWidth, ImageHeight);
}
if (ImageWidth > 0)
{
return string.Format("![{0}]: {1} (Width: {2})", Tooltip, Url, ImageWidth);
}
if (ImageHeight > 0)
{
return string.Format("![{0}]: {1} (Height: {2})", Tooltip, Url, ImageHeight);
}
return string.Format("![{0}]: {1}", Tooltip, Url);
}
}
}