From e9c7d4c3915214ad4813cc2cd34ba584d4c8b05e Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Sun, 29 Jun 2014 23:30:40 -0500 Subject: [PATCH] added regex for parsing conditions and toggles from hrefs --- Ficdown.Parser/Engine/BlockHandler.cs | 22 ++++++++++++++++++---- Ficdown.Parser/Engine/RegexLib.cs | 9 ++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Ficdown.Parser/Engine/BlockHandler.cs b/Ficdown.Parser/Engine/BlockHandler.cs index a0e7fbc..dced07c 100644 --- a/Ficdown.Parser/Engine/BlockHandler.cs +++ b/Ficdown.Parser/Engine/BlockHandler.cs @@ -64,6 +64,19 @@ return story; } + private void ParseHref(string href, out IList conditions, out IList toggles) + { + var match = RegexLib.Href.Match(href); + if (match.Success) + { + var cstr = match.Groups["conditions"].Value; + var tstr = match.Groups["toggles"].Value; + } + else throw new FormatException(string.Format("Invalid href: {0}", href)); + conditions = null; + toggles = null; + } + private Scene BlockToScene(Block block) { var scene = new Scene @@ -75,11 +88,12 @@ if (sceneName.Success) { scene.Name = sceneName.Groups["text"].Value; + IList conditions, toggles; + ParseHref(sceneName.Groups["href"].Value, out conditions, out toggles); + scene.Conditions = conditions; + scene.Toggles = toggles; } - else - { - scene.Name = block.Name; - } + else scene.Name = block.Name; return scene; } diff --git a/Ficdown.Parser/Engine/RegexLib.cs b/Ficdown.Parser/Engine/RegexLib.cs index d786840..0603ea0 100644 --- a/Ficdown.Parser/Engine/RegexLib.cs +++ b/Ficdown.Parser/Engine/RegexLib.cs @@ -5,9 +5,12 @@ internal static class RegexLib { - public static Regex Anchors = new Regex(string.Format(@"(?\[(?{0})\]\([ ]*(?{1})[ ]*\))", - GetNestedBracketsPattern(), GetNestedParensPattern()), - RegexOptions.Singleline | RegexOptions.Compiled); + public static Regex Anchors = + new Regex( + string.Format(@"(?\[(?{0})\]\([ ]*(?{1})[ ]*\))", GetNestedBracketsPattern(), + GetNestedParensPattern()), RegexOptions.Singleline | RegexOptions.Compiled); + + public static Regex Href = new Regex(@"^(\?(?[^#]+))?(#(?.+))?$", RegexOptions.Compiled); private const int _nestDepth = 6;