2014-10-19 21:50:23 +02:00
|
|
|
|
// XAML Map Control - http://xamlmapcontrol.codeplex.com/
|
|
|
|
|
|
// Copyright © 2014 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
#if WINDOWS_RUNTIME
|
2014-12-17 21:38:28 +01:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2014-10-19 21:50:23 +02:00
|
|
|
|
using Windows.UI.Xaml.Documents;
|
|
|
|
|
|
#else
|
2014-12-17 21:38:28 +01:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
2014-10-19 21:50:23 +02:00
|
|
|
|
using System.Windows.Documents;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class HyperlinkText
|
|
|
|
|
|
{
|
|
|
|
|
|
private static Regex regex = new Regex(@"\[([^\]]+)\]\(([^\)]+)\)");
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Converts text containing hyperlinks in markdown syntax [text](url)
|
|
|
|
|
|
/// to a collection of Run and Hyperlink inlines.
|
|
|
|
|
|
/// </summary>
|
2014-12-17 21:38:28 +01:00
|
|
|
|
public static IEnumerable<Inline> TextToInlines(this string text)
|
2014-10-19 21:50:23 +02:00
|
|
|
|
{
|
|
|
|
|
|
var inlines = new List<Inline>();
|
|
|
|
|
|
|
|
|
|
|
|
while (!string.IsNullOrEmpty(text))
|
|
|
|
|
|
{
|
|
|
|
|
|
var match = regex.Match(text);
|
|
|
|
|
|
Uri uri;
|
|
|
|
|
|
|
|
|
|
|
|
if (match.Success &&
|
|
|
|
|
|
match.Groups.Count == 3 &&
|
|
|
|
|
|
Uri.TryCreate(match.Groups[2].Value, UriKind.Absolute, out uri))
|
|
|
|
|
|
{
|
|
|
|
|
|
inlines.Add(new Run { Text = text.Substring(0, match.Index) });
|
|
|
|
|
|
text = text.Substring(match.Index + match.Length);
|
|
|
|
|
|
|
|
|
|
|
|
var link = new Hyperlink { NavigateUri = uri };
|
|
|
|
|
|
link.Inlines.Add(new Run { Text = match.Groups[1].Value });
|
|
|
|
|
|
#if SILVERLIGHT
|
|
|
|
|
|
link.TargetName = "_blank";
|
|
|
|
|
|
#elif !WINDOWS_RUNTIME
|
|
|
|
|
|
link.ToolTip = uri.ToString();
|
|
|
|
|
|
link.RequestNavigate += (s, e) => System.Diagnostics.Process.Start(e.Uri.ToString());
|
|
|
|
|
|
#endif
|
|
|
|
|
|
inlines.Add(link);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
inlines.Add(new Run { Text = text });
|
|
|
|
|
|
text = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return inlines;
|
|
|
|
|
|
}
|
2014-12-17 21:38:28 +01:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty InlinesSourceProperty = DependencyProperty.RegisterAttached(
|
|
|
|
|
|
"InlinesSource", typeof(string), typeof(HyperlinkText), new PropertyMetadata(null, InlinesSourcePropertyChanged));
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetInlinesSource(UIElement element)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (string)element.GetValue(InlinesSourceProperty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void SetInlinesSource(UIElement element, string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
element.SetValue(InlinesSourceProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void InlinesSourcePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
InlineCollection inlines = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (obj is TextBlock)
|
|
|
|
|
|
{
|
|
|
|
|
|
inlines = ((TextBlock)obj).Inlines;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (obj is Paragraph)
|
|
|
|
|
|
{
|
|
|
|
|
|
inlines = ((Paragraph)obj).Inlines;
|
|
|
|
|
|
}
|
|
|
|
|
|
#if WINDOWS_RUNTIME || SILVERLIGHT
|
|
|
|
|
|
else if (obj is RichTextBlock)
|
|
|
|
|
|
{
|
|
|
|
|
|
var paragraph = new Paragraph();
|
|
|
|
|
|
inlines = paragraph.Inlines;
|
|
|
|
|
|
var richTextBlock = (RichTextBlock)obj;
|
|
|
|
|
|
richTextBlock.Blocks.Clear();
|
|
|
|
|
|
richTextBlock.Blocks.Add(paragraph);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
if (inlines != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
inlines.Clear();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var inline in TextToInlines((string)e.NewValue))
|
|
|
|
|
|
{
|
|
|
|
|
|
inlines.Add(inline);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-10-19 21:50:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|