2014-08-09 15:18:31 -05:00
|
|
|
|
|
2014-07-02 23:11:36 -05:00
|
|
|
|
namespace Ficdown.Parser.Parser
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using Model.Parser;
|
|
|
|
|
|
2015-07-19 15:51:10 -05:00
|
|
|
|
internal class Utilities
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
2015-07-19 15:51:10 -05:00
|
|
|
|
public static Utilities GetInstance(string blockName, int lineNumber)
|
|
|
|
|
{
|
|
|
|
|
return new Utilities
|
|
|
|
|
{
|
|
|
|
|
_blockName = blockName,
|
|
|
|
|
_lineNumber = lineNumber
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Utilities GetInstance(string blockName)
|
|
|
|
|
{
|
|
|
|
|
return new Utilities
|
|
|
|
|
{
|
|
|
|
|
_blockName = blockName
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string _blockName;
|
|
|
|
|
protected int? _lineNumber;
|
|
|
|
|
|
|
|
|
|
public string NormalizeString(string raw)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
|
|
|
|
return Regex.Replace(Regex.Replace(raw.ToLower(), @"^\W+|\W+$", string.Empty), @"\W+", "-");
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 15:51:10 -05:00
|
|
|
|
private Href ParseHref(string href)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
|
|
|
|
var match = RegexLib.Href.Match(href);
|
|
|
|
|
if (match.Success)
|
|
|
|
|
{
|
|
|
|
|
var ttstr = match.Groups["target"].Value;
|
|
|
|
|
var cstr = match.Groups["conditions"].Value;
|
|
|
|
|
var tstr = match.Groups["toggles"].Value;
|
|
|
|
|
return new Href
|
|
|
|
|
{
|
|
|
|
|
Original = href,
|
|
|
|
|
Target = !string.IsNullOrEmpty(ttstr) ? ttstr.TrimStart('/') : null,
|
|
|
|
|
Conditions =
|
|
|
|
|
!string.IsNullOrEmpty(cstr)
|
|
|
|
|
? new List<string>(cstr.TrimStart('?').Split('&').Select(c => c.Trim().ToLower()))
|
|
|
|
|
.ToDictionary(c => c.TrimStart('!'), c => !c.StartsWith("!"))
|
|
|
|
|
: null,
|
|
|
|
|
Toggles =
|
|
|
|
|
!string.IsNullOrEmpty(tstr)
|
2014-07-26 23:54:50 -05:00
|
|
|
|
? new List<string>(tstr.TrimStart('#').Split('+').Select(t => t.Trim().ToLower())).ToArray()
|
2014-07-02 23:11:36 -05:00
|
|
|
|
: null
|
|
|
|
|
};
|
|
|
|
|
}
|
2015-07-19 15:51:10 -05:00
|
|
|
|
throw new FicdownException(_blockName, _lineNumber, string.Format("Invalid href: {0}", href));
|
2014-07-02 23:11:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 15:51:10 -05:00
|
|
|
|
public Anchor ParseAnchor(string anchorText)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
|
|
|
|
var match = RegexLib.Anchors.Match(anchorText);
|
2015-07-19 15:51:10 -05:00
|
|
|
|
if (!match.Success) throw new FicdownException(_blockName, _lineNumber, string.Format("Invalid anchor: {0}", anchorText));
|
2014-07-26 23:54:50 -05:00
|
|
|
|
return MatchToAnchor(match);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 15:51:10 -05:00
|
|
|
|
public IList<Anchor> ParseAnchors(string text)
|
2014-07-26 23:54:50 -05:00
|
|
|
|
{
|
|
|
|
|
var matches = RegexLib.Anchors.Matches(text);
|
|
|
|
|
return matches.Cast<Match>().Select(MatchToAnchor).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 15:51:10 -05:00
|
|
|
|
private Anchor MatchToAnchor(Match match)
|
2014-07-26 23:54:50 -05:00
|
|
|
|
{
|
2014-07-02 23:11:36 -05:00
|
|
|
|
var astr = match.Groups["anchor"].Value;
|
|
|
|
|
var txstr = match.Groups["text"].Value;
|
|
|
|
|
var ttstr = match.Groups["title"].Value;
|
2014-07-26 23:54:50 -05:00
|
|
|
|
var hrefstr = match.Groups["href"].Value;
|
|
|
|
|
if (hrefstr.StartsWith(@""""))
|
|
|
|
|
{
|
|
|
|
|
ttstr = hrefstr.Trim('"');
|
|
|
|
|
hrefstr = string.Empty;
|
|
|
|
|
}
|
2014-07-02 23:11:36 -05:00
|
|
|
|
return new Anchor
|
|
|
|
|
{
|
|
|
|
|
Original = !string.IsNullOrEmpty(astr) ? astr : null,
|
|
|
|
|
Text = !string.IsNullOrEmpty(txstr) ? txstr : null,
|
2017-12-28 17:42:33 -06:00
|
|
|
|
Title = ttstr,
|
2014-07-26 23:54:50 -05:00
|
|
|
|
Href = ParseHref(hrefstr)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 15:51:10 -05:00
|
|
|
|
public IDictionary<bool, string> ParseConditionalText(string text)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
|
|
|
|
var match = RegexLib.ConditionalText.Match(text);
|
2015-07-19 15:51:10 -05:00
|
|
|
|
if (!match.Success) throw new FicdownException(_blockName, _lineNumber, string.Format(@"Invalid conditional text: {0}", text));
|
2014-07-02 23:11:36 -05:00
|
|
|
|
return new Dictionary<bool, string>
|
|
|
|
|
{
|
|
|
|
|
{true, match.Groups["true"].Value},
|
|
|
|
|
{false, match.Groups["false"].Value}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-19 15:51:10 -05:00
|
|
|
|
public bool ConditionsMet(IDictionary<string, bool> playerState, IDictionary<string, bool> conditions)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
conditions.All(
|
|
|
|
|
c =>
|
|
|
|
|
(!c.Value && (!playerState.ContainsKey(c.Key) || !playerState[c.Key])) ||
|
2014-08-10 17:32:13 -05:00
|
|
|
|
(c.Value && (playerState.ContainsKey(c.Key) && playerState[c.Key])));
|
2014-07-02 23:11:36 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|