From eeb7f1badb0519c80e0699f80fa4270ca824c760 Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Sun, 10 Aug 2014 17:32:13 -0500 Subject: [PATCH] html renderer generates index page; fixed more traversal/state resolution bugs --- .../Ficdown.Parser.Tests.csproj | 1 + Ficdown.Parser.Tests/IntegrationTests.cs | 2 +- .../TestStories/CloakOfDarkness.md | 61 +++++++++++++++++++ .../TestStories/Resources.Designer.cs | 10 +++ .../TestStories/Resources.resx | 3 + Ficdown.Parser/FicDownParser.cs | 2 +- Ficdown.Parser/Ficdown.Parser.csproj | 1 + Ficdown.Parser/Model/Parser/ResolvedStory.cs | 12 ++++ Ficdown.Parser/Parser/BlockHandler.cs | 4 +- Ficdown.Parser/Parser/IStateResolver.cs | 2 +- Ficdown.Parser/Parser/StateResolver.cs | 19 ++++-- Ficdown.Parser/Parser/Utilities.cs | 2 +- Ficdown.Parser/Player/GameTraverser.cs | 7 +++ Ficdown.Parser/Player/StateManager.cs | 2 +- Ficdown.Parser/Render/HtmlRenderer.cs | 10 ++- Ficdown.Parser/Render/IRenderer.cs | 3 +- 16 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 Ficdown.Parser.Tests/TestStories/CloakOfDarkness.md create mode 100644 Ficdown.Parser/Model/Parser/ResolvedStory.cs diff --git a/Ficdown.Parser.Tests/Ficdown.Parser.Tests.csproj b/Ficdown.Parser.Tests/Ficdown.Parser.Tests.csproj index 8d4a2aa..df79b6a 100644 --- a/Ficdown.Parser.Tests/Ficdown.Parser.Tests.csproj +++ b/Ficdown.Parser.Tests/Ficdown.Parser.Tests.csproj @@ -61,6 +61,7 @@ + diff --git a/Ficdown.Parser.Tests/IntegrationTests.cs b/Ficdown.Parser.Tests/IntegrationTests.cs index 083aac1..abf4676 100644 --- a/Ficdown.Parser.Tests/IntegrationTests.cs +++ b/Ficdown.Parser.Tests/IntegrationTests.cs @@ -13,7 +13,7 @@ public void CanParseValidStoryFile() { var parser = new FicdownParser(); - var storyText = Encoding.UTF8.GetString(Resources.TheRobotKing); + var storyText = Encoding.UTF8.GetString(Resources.CloakOfDarkness); var story = parser.ParseStory(storyText); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "itest_output"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); diff --git a/Ficdown.Parser.Tests/TestStories/CloakOfDarkness.md b/Ficdown.Parser.Tests/TestStories/CloakOfDarkness.md new file mode 100644 index 0000000..fd3706f --- /dev/null +++ b/Ficdown.Parser.Tests/TestStories/CloakOfDarkness.md @@ -0,0 +1,61 @@ +# [Cloak of Darkness](/foyer) + +A basic IF demonstration. + +Hurrying through the rainswept November night, you're glad to see the bright lights of the Opera House. It's surprising that there aren't more people about but, hey, what do you expect in a cheap demo game...? + +## [Foyer]("Foyer of the Opera House") + +[You](#examined-self) are standing in a spacious hall, splendidly decorated in red and gold, with glittering chandeliers overhead. The entrance from the street is to the [north](#tried-to-leave), and there are doorways [south](/bar) and [west](/cloakroom). + +### Tried to Leave + +[You've](#examined-self) only just arrived, and besides, the weather outside seems to be getting worse. + +### Examined Self + +[You aren't carrying anything.|You are wearing a handsome cloak, of velvet trimmed with satin, and slightly splattered with raindrops. Its blackness is so deep that it almost seems to suck light from the room.](?lost-cloak) + +## Cloakroom + +The walls of this small room were clearly once lined with hooks, though now only one remains. The exit is a door to the [east](/foyer). + +[Your cloak is on the floor here.](?dropped-cloak) +[Your cloak is hanging on the hook.](?hung-cloak) + +- [Examine the hook.](#examined-hook) +- [Hang your cloak on the hook.](?examined-self&!lost-cloak#lost-cloak+hung-cloak) +- [Drop your cloak on the floor.](?examined-self&!lost-cloak#lost-cloak+dropped-cloak) + +### Examined Hook + +It's just a small brass hook, [with your cloak hanging on it|screwed to the wall](?hung-cloak). + +## [Bar]("Foyer Bar") + +You walk to the bar, but it's so dark here you can't really make anything out. The foyer is back to the [north](/foyer). + +- [Feel around for a light switch.](?!scuffled1#scuffled1+not-in-the-dark) +- [Sit on a bar stool.](?!scuffled2#scuffled2+not-in-the-dark) + +### Not in the Dark + +In the dark? You could easily disturb something. + +## [Bar](?lost-cloak "Foyer Bar") + +The bar, much rougher than you'd have guessed after the opulence of the foyer to the north, is completely empty. There seems to be some sort of message scrawled in the sawdust on the floor. The foyer is back to the [north](/foyer). + +[Examine the message.](/message) + +## [Message]("Foyer Bar") + +The message, neatly marked in the sawdust, reads... + +**You have won!** + +## [Message](?scuffled1&scuffled2 "Foyer Bar") + +The message has been carelessly trampled, making it difficult to read. You can just distinguish the words... + +**You have lost.** diff --git a/Ficdown.Parser.Tests/TestStories/Resources.Designer.cs b/Ficdown.Parser.Tests/TestStories/Resources.Designer.cs index f8b1859..fd2cb80 100644 --- a/Ficdown.Parser.Tests/TestStories/Resources.Designer.cs +++ b/Ficdown.Parser.Tests/TestStories/Resources.Designer.cs @@ -60,6 +60,16 @@ namespace Ficdown.Parser.Tests.TestStories { } } + /// + /// Looks up a localized resource of type System.Byte[]. + /// + internal static byte[] CloakOfDarkness { + get { + object obj = ResourceManager.GetObject("CloakOfDarkness", resourceCulture); + return ((byte[])(obj)); + } + } + /// /// Looks up a localized resource of type System.Byte[]. /// diff --git a/Ficdown.Parser.Tests/TestStories/Resources.resx b/Ficdown.Parser.Tests/TestStories/Resources.resx index b979b7c..48da9cb 100644 --- a/Ficdown.Parser.Tests/TestStories/Resources.resx +++ b/Ficdown.Parser.Tests/TestStories/Resources.resx @@ -118,6 +118,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + cloakofdarkness.md;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + therobotking.md;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 diff --git a/Ficdown.Parser/FicDownParser.cs b/Ficdown.Parser/FicDownParser.cs index a88645a..fb67fc9 100644 --- a/Ficdown.Parser/FicDownParser.cs +++ b/Ficdown.Parser/FicDownParser.cs @@ -32,7 +32,7 @@ namespace Ficdown.Parser set { _stateResolver = value; } } - public IEnumerable ParseStory(string storyText) + public ResolvedStory ParseStory(string storyText) { var lines = storyText.Split(new[] {"\n", "\r\n"}, StringSplitOptions.None); var blocks = BlockHandler.ExtractBlocks(lines); diff --git a/Ficdown.Parser/Ficdown.Parser.csproj b/Ficdown.Parser/Ficdown.Parser.csproj index de2e11f..bb548a1 100644 --- a/Ficdown.Parser/Ficdown.Parser.csproj +++ b/Ficdown.Parser/Ficdown.Parser.csproj @@ -43,6 +43,7 @@ + diff --git a/Ficdown.Parser/Model/Parser/ResolvedStory.cs b/Ficdown.Parser/Model/Parser/ResolvedStory.cs new file mode 100644 index 0000000..1f34845 --- /dev/null +++ b/Ficdown.Parser/Model/Parser/ResolvedStory.cs @@ -0,0 +1,12 @@ +namespace Ficdown.Parser.Model.Parser +{ + using System.Collections.Generic; + + public class ResolvedStory + { + public string Name { get; set; } + public string Description { get; set; } + public string FirstPage { get; set; } + public IEnumerable Pages { get; set; } + } +} diff --git a/Ficdown.Parser/Parser/BlockHandler.cs b/Ficdown.Parser/Parser/BlockHandler.cs index 8cf71d6..693dec0 100644 --- a/Ficdown.Parser/Parser/BlockHandler.cs +++ b/Ficdown.Parser/Parser/BlockHandler.cs @@ -39,7 +39,9 @@ public Story ParseBlocks(IEnumerable blocks) { // get the story - var storyBlock = blocks.Single(b => b.Type == BlockType.Story); + var storyBlock = blocks.SingleOrDefault(b => b.Type == BlockType.Story); + if(storyBlock == null) throw new FormatException("No story block found"); + var storyAnchor = Utilities.ParseAnchor(storyBlock.Name); if (storyAnchor.Href.Target == null || storyAnchor.Href.Conditions != null || diff --git a/Ficdown.Parser/Parser/IStateResolver.cs b/Ficdown.Parser/Parser/IStateResolver.cs index b5a5b26..13c694e 100644 --- a/Ficdown.Parser/Parser/IStateResolver.cs +++ b/Ficdown.Parser/Parser/IStateResolver.cs @@ -7,6 +7,6 @@ internal interface IStateResolver { - IEnumerable Resolve(IEnumerable pages, Story story); + ResolvedStory Resolve(IEnumerable pages, Story story); } } diff --git a/Ficdown.Parser/Parser/StateResolver.cs b/Ficdown.Parser/Parser/StateResolver.cs index 502a0cc..a270f55 100644 --- a/Ficdown.Parser/Parser/StateResolver.cs +++ b/Ficdown.Parser/Parser/StateResolver.cs @@ -21,18 +21,23 @@ _usedNames = new HashSet(); } - public IEnumerable Resolve(IEnumerable pages, Story story) + public ResolvedStory Resolve(IEnumerable pages, Story story) { _story = story; - return - pages.Select( + return new ResolvedStory + { + Name = story.Name, + Description = story.Description, + FirstPage = GetPageNameForHash(pages.Single(p => p.Id.Equals(Guid.Empty)).CompressedHash), + Pages = pages.Select( page => new ResolvedPage { Name = GetPageNameForHash(page.CompressedHash), Content = ResolveDescription(page), ActiveToggles = GetStateDictionary(page).Where(t => t.Value).Select(t => t.Key) - }).ToList(); + }).ToList() + }; } private string ResolveAnchor(Anchor anchor, IDictionary playerState, string targetHash) @@ -63,7 +68,9 @@ var actionTuple = _story.Actions.Single(a => a.Value.Id == i + 1); var actionAnchors = Utilities.ParseAnchors(actionTuple.Value.Description); var anchorDict = GetStateDictionary(page); - if (actionAnchors.Any(a => a.Href.Conditions.ContainsKey(actionTuple.Key))) + if ( + actionAnchors.Any( + a => a.Href.Conditions != null && a.Href.Conditions.ContainsKey(actionTuple.Key))) { if (page.State.ActionFirstToggles[firstToggleCounter++]) { @@ -95,7 +102,7 @@ return resolved.ToString(); } - private IDictionary GetStateDictionary(PageState page) + public static IDictionary GetStateDictionary(PageState page) { return page.StateMatrix.Where(matrix => page.State.PlayerState[matrix.Value]) .ToDictionary(m => m.Key, m => true); diff --git a/Ficdown.Parser/Parser/Utilities.cs b/Ficdown.Parser/Parser/Utilities.cs index c145577..ed83b6c 100644 --- a/Ficdown.Parser/Parser/Utilities.cs +++ b/Ficdown.Parser/Parser/Utilities.cs @@ -105,7 +105,7 @@ namespace Ficdown.Parser.Parser conditions.All( c => (!c.Value && (!playerState.ContainsKey(c.Key) || !playerState[c.Key])) || - (playerState.ContainsKey(c.Key) && playerState[c.Key])); + (c.Value && (playerState.ContainsKey(c.Key) && playerState[c.Key]))); } } } diff --git a/Ficdown.Parser/Player/GameTraverser.cs b/Ficdown.Parser/Player/GameTraverser.cs index ee9e672..f13d84d 100644 --- a/Ficdown.Parser/Player/GameTraverser.cs +++ b/Ficdown.Parser/Player/GameTraverser.cs @@ -123,6 +123,13 @@ foreach (var anchor in anchors.Where(a => a.Href.Target != null || a.Href.Toggles != null)) { + // don't follow links that would be hidden + if (anchor.Href.Conditions != null && + string.IsNullOrEmpty( + Utilities.ParseConditionalText(anchor.Text)[ + Utilities.ConditionsMet(StateResolver.GetStateDictionary(currentState.Page), + anchor.Href.Conditions)])) continue; + var newState = _manager.ResolveNewState(anchor, currentState.Page); if (!currentState.Page.Links.ContainsKey(anchor.Original)) currentState.Page.Links.Add(anchor.Original, newState.UniqueHash); diff --git a/Ficdown.Parser/Player/StateManager.cs b/Ficdown.Parser/Player/StateManager.cs index ea5f52d..ef60404 100644 --- a/Ficdown.Parser/Player/StateManager.cs +++ b/Ficdown.Parser/Player/StateManager.cs @@ -84,7 +84,7 @@ newState.State.ActionsToShow[_story.Actions[toggle].Id - 1] = true; if ( Utilities.ParseAnchors(_story.Actions[toggle].Description) - .Any(a => a.Href.Conditions.ContainsKey(toggle))) + .Any(a => a.Href.Conditions != null && a.Href.Conditions.ContainsKey(toggle))) actionFirstToggles.Add(!current.State.PlayerState[_stateMatrix[toggle]]); } newState.State.PlayerState[_stateMatrix[toggle]] = true; diff --git a/Ficdown.Parser/Render/HtmlRenderer.cs b/Ficdown.Parser/Render/HtmlRenderer.cs index b316566..18e87e1 100644 --- a/Ficdown.Parser/Render/HtmlRenderer.cs +++ b/Ficdown.Parser/Render/HtmlRenderer.cs @@ -1,6 +1,5 @@ namespace Ficdown.Parser.Render { - using System.Collections.Generic; using System.IO; using System.Linq; using MarkdownSharp; @@ -16,9 +15,14 @@ _md = new Markdown(); } - public void Render(IEnumerable pages, string outPath, bool debug = false) + public void Render(ResolvedStory story, string outPath, bool debug = false) { - foreach (var page in pages) + var index = string.Format("# {0}\n\n{1}\n\n[Click here]({2}.html) to start!", story.Name, story.Description, + story.FirstPage); + + File.WriteAllText(Path.Combine(outPath, "index.html"), _md.Transform(index)); + + foreach (var page in story.Pages) { var content = page.Content; foreach (var anchor in Utilities.ParseAnchors(page.Content)) diff --git a/Ficdown.Parser/Render/IRenderer.cs b/Ficdown.Parser/Render/IRenderer.cs index 0bfe5da..e9ea151 100644 --- a/Ficdown.Parser/Render/IRenderer.cs +++ b/Ficdown.Parser/Render/IRenderer.cs @@ -1,10 +1,9 @@ namespace Ficdown.Parser.Render { - using System.Collections.Generic; using Model.Parser; internal interface IRenderer { - void Render(IEnumerable pages, string outPath, bool debug); + void Render(ResolvedStory story, string outPath, bool debug); } }