#if USE_ARTICY
// Copyright (c) Pixel Crushers. All rights reserved.
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using UnityEngine.UI;
namespace PixelCrushers.DialogueSystem.Articy
{
///
/// This static utility class contains tools for working with Articy data.
///
public static class ArticyTools
{
///
/// InitializeLuaSubtables() converts fields whose titles start with this string into subtables.
///
public const string SubtableFieldPrefix = "SUBTABLE__";
///
/// Convert articy markup codes to rich text codes that Unity can display.
///
public static bool convertMarkupToRichText = true;
///
/// Checks the first few lines of a string in articy:draft XML format for a schema identifier.
///
///
/// true if it contains the schema identifier.
///
///
/// XML data to check.
///
///
/// Schema identifier to check for.
///
public static bool DataContainsSchemaId(string xmlData, string schemaId)
{
StringReader xmlStream = new StringReader(xmlData);
if (xmlStream != null)
{
for (int i = 0; i < 5; i++)
{
string s = xmlStream.ReadLine();
if (!string.IsNullOrEmpty(s) && s.Contains(schemaId)) return true;
}
//--- Not compatible with UWP10: xmlStream.Close();
}
return false;
}
private static string[] htmlTags = new string[] { "", "
", "", "", "", "", "",
"", "
", "", "" };
///
/// Removes HTML tags from a string.
///
///
/// The string without HTML.
///
///
/// The HTML-filled string.
///
public static string RemoveHtml(string s)
{
// This is a rather inefficient first pass, but it gets the job done.
// On the roadmap: Replace with http://www.codeproject.com/Articles/298519/Fast-Token-Replacement-in-Csharp
if (!string.IsNullOrEmpty(s))
{
if (convertMarkupToRichText) s = ReplaceMarkup(s);
foreach (string htmlTag in htmlTags)
{
s = s.Replace(htmlTag, string.Empty);
}
if (s.Contains("")) s = ReplaceHtmlCharacterCodes(s);
s = s.Replace(""", "\"");
s = s.Replace("&", "&");
s = s.Replace("<", "<");
s = s.Replace(">", ">");
s = s.Replace(" ", " ");
s = s.Trim();
}
return s;
}
///
/// Selectively replaces HTML character codes (numeric character references) that articy uses.
///
public static string ReplaceHtmlCharacterCodes(string s)
{
var text = s;
Regex regex = new Regex(@"[0-9]+;");
text = regex.Replace(text, delegate (Match match)
{
string codeString = match.Value.Substring(2, match.Value.Length - 3);
int numericCode;
if (!int.TryParse(codeString, out numericCode)) return match.Value;
return char.ConvertFromUtf32(numericCode).ToString();
});
return text;
//return s.Replace("!", "!")
// .Replace(""", "\"")
// .Replace("#", "#")
// .Replace("$", "$")
// .Replace("%", "%")
// .Replace("&", "&")
// .Replace("'", "'")
// .Replace("`", "`")
// .Replace(" ", " ")
// .Replace("¢", "¢")
// .Replace("£", "£")
// .Replace("¤", "¤")
// .Replace("¥", "¥")
// .Replace("¦", "¦")
// .Replace("§", "§")
// .Replace("¨", "¨")
// .Replace("©", "©")
// .Replace("±", "±")
// .Replace("²", "²")
// .Replace("³", "³")
// .Replace("´", "´")
// .Replace("¼", "¼")
// .Replace("½", "½")
// .Replace("¾", "¾")
// .Replace("¿", "¿");
}
//==================================================================
// Code contributed by Racoon7:
const RegexOptions Options = RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase;
static readonly Regex StylesRegex = new Regex(@"", Options); // Get the part of text dealing with styles
static readonly Regex StyleRegex = new Regex(@"#(?s[1-9]\d*) {(?