2014-07-02 23:11:36 -05:00
|
|
|
|
namespace Ficdown.Parser.Parser
|
2014-06-29 23:04:51 -05:00
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using Model.Parser;
|
|
|
|
|
using Model.Story;
|
|
|
|
|
using Action = Model.Story.Action;
|
|
|
|
|
|
2014-08-09 15:18:31 -05:00
|
|
|
|
internal class BlockHandler : IBlockHandler
|
2014-06-29 23:04:51 -05:00
|
|
|
|
{
|
|
|
|
|
public IEnumerable<Block> ExtractBlocks(IEnumerable<string> lines)
|
|
|
|
|
{
|
|
|
|
|
var blocks = new List<Block>();
|
|
|
|
|
Block currentBlock = null;
|
2015-07-19 15:51:10 -05:00
|
|
|
|
var lineNum = 1;
|
2014-06-29 23:04:51 -05:00
|
|
|
|
foreach (var line in lines)
|
|
|
|
|
{
|
|
|
|
|
var match = Regex.Match(line, @"^(?<level>#{1,3})\s+(?<name>[^#].*)$");
|
|
|
|
|
if (match.Success)
|
|
|
|
|
{
|
|
|
|
|
if (currentBlock != null) blocks.Add(currentBlock);
|
|
|
|
|
currentBlock = new Block()
|
|
|
|
|
{
|
|
|
|
|
Type = (BlockType) match.Groups["level"].Length,
|
|
|
|
|
Name = match.Groups["name"].Value,
|
2015-07-19 15:51:10 -05:00
|
|
|
|
Lines = new List<Line>(),
|
|
|
|
|
LineNumber = lineNum++
|
2014-06-29 23:04:51 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-07-19 15:51:10 -05:00
|
|
|
|
if (currentBlock != null) currentBlock.Lines.Add(new Line
|
|
|
|
|
{
|
|
|
|
|
Number = lineNum++,
|
|
|
|
|
Text = line
|
|
|
|
|
});
|
2014-06-29 23:04:51 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (currentBlock != null) blocks.Add(currentBlock);
|
|
|
|
|
return blocks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Story ParseBlocks(IEnumerable<Block> blocks)
|
|
|
|
|
{
|
|
|
|
|
// get the story
|
2014-08-10 17:32:13 -05:00
|
|
|
|
var storyBlock = blocks.SingleOrDefault(b => b.Type == BlockType.Story);
|
2015-07-19 15:51:10 -05:00
|
|
|
|
if(storyBlock == null) throw new FicdownException("No story block found");
|
2014-08-10 17:32:13 -05:00
|
|
|
|
|
2015-07-24 00:56:53 -05:00
|
|
|
|
Anchor storyAnchor;
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-09-25 16:29:53 -05:00
|
|
|
|
storyAnchor = Utilities.GetInstance(storyBlock.Name, storyBlock.LineNumber).ParseAnchor(storyBlock.Name, storyBlock.LineNumber, 1);
|
2015-07-24 00:56:53 -05:00
|
|
|
|
}
|
|
|
|
|
catch(FicdownException ex)
|
|
|
|
|
{
|
2018-09-25 16:29:53 -05:00
|
|
|
|
throw new FicdownException(ex.BlockName, "Story name must be an anchor pointing to the first scene", ex.LineNumber);
|
2015-07-24 00:56:53 -05:00
|
|
|
|
}
|
2014-06-29 23:04:51 -05:00
|
|
|
|
|
2014-07-02 23:11:36 -05:00
|
|
|
|
if (storyAnchor.Href.Target == null || storyAnchor.Href.Conditions != null ||
|
|
|
|
|
storyAnchor.Href.Toggles != null)
|
2018-09-25 16:29:53 -05:00
|
|
|
|
throw new FicdownException(storyBlock.Name, "Story href should only have a target", storyBlock.LineNumber);
|
2014-06-29 23:04:51 -05:00
|
|
|
|
|
|
|
|
|
var story = new Story
|
|
|
|
|
{
|
2014-07-02 23:11:36 -05:00
|
|
|
|
Name = storyAnchor.Text,
|
2015-07-29 18:11:07 -05:00
|
|
|
|
Description = string.Join("\n", storyBlock.Lines.Select(l => l.Text)).Trim(),
|
2014-06-29 23:04:51 -05:00
|
|
|
|
Scenes = new Dictionary<string, IList<Scene>>(),
|
2014-07-02 23:11:36 -05:00
|
|
|
|
Actions = new Dictionary<string, Action>()
|
2014-06-29 23:04:51 -05:00
|
|
|
|
};
|
|
|
|
|
|
2014-07-26 23:54:50 -05:00
|
|
|
|
var sid = 1;
|
|
|
|
|
var scenes = blocks.Where(b => b.Type == BlockType.Scene).Select(b => BlockToScene(b, sid++));
|
2014-06-29 23:04:51 -05:00
|
|
|
|
foreach (var scene in scenes)
|
|
|
|
|
{
|
2014-07-02 23:11:36 -05:00
|
|
|
|
if (!story.Scenes.ContainsKey(scene.Key)) story.Scenes.Add(scene.Key, new List<Scene>());
|
|
|
|
|
story.Scenes[scene.Key].Add(scene);
|
2014-06-29 23:04:51 -05:00
|
|
|
|
}
|
2014-07-26 23:54:50 -05:00
|
|
|
|
var aid = 1;
|
2015-07-24 00:56:53 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
story.Actions =
|
|
|
|
|
blocks.Where(b => b.Type == BlockType.Action).Select(b => BlockToAction(b, aid++)).ToDictionary(a => a.Toggle, a => a);
|
|
|
|
|
}
|
|
|
|
|
catch(ArgumentException)
|
|
|
|
|
{
|
|
|
|
|
var a = blocks.First(b => b.Type == BlockType.Action && blocks.Any(d => b != d && BlockToAction(b, 0).Toggle == BlockToAction(d, 0).Toggle));
|
|
|
|
|
var actionA = BlockToAction(a, a.LineNumber);
|
|
|
|
|
var dupe = blocks.First(b => b.Type == BlockType.Action && b != a && BlockToAction(b, 0).Toggle == actionA.Toggle);
|
2018-09-25 16:29:53 -05:00
|
|
|
|
throw new FicdownException(actionA.Toggle, string.Format("Action is defined again on line {0}", dupe.LineNumber), actionA.LineNumber);
|
2015-07-24 00:56:53 -05:00
|
|
|
|
}
|
2014-06-29 23:04:51 -05:00
|
|
|
|
|
2014-07-02 23:11:36 -05:00
|
|
|
|
if (!story.Scenes.ContainsKey(storyAnchor.Href.Target))
|
2018-09-25 16:29:53 -05:00
|
|
|
|
throw new FicdownException(storyBlock.Name, string.Format("Story links to undefined scene: {0}", storyAnchor.Href.Target), storyBlock.LineNumber);
|
2014-07-02 23:11:36 -05:00
|
|
|
|
story.FirstScene = storyAnchor.Href.Target;
|
2014-06-30 11:59:56 -05:00
|
|
|
|
|
2014-06-29 23:04:51 -05:00
|
|
|
|
return story;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-29 23:30:40 -05:00
|
|
|
|
|
2014-07-26 23:54:50 -05:00
|
|
|
|
private Scene BlockToScene(Block block, int id)
|
2014-06-29 23:04:51 -05:00
|
|
|
|
{
|
|
|
|
|
var scene = new Scene
|
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
Id = id,
|
2015-07-19 15:51:10 -05:00
|
|
|
|
LineNumber = block.LineNumber,
|
2018-09-25 16:29:53 -05:00
|
|
|
|
RawDescription = string.Join("\n", block.Lines.Select(l => l.Text)),
|
2015-07-19 15:51:10 -05:00
|
|
|
|
Description = string.Join("\n", block.Lines.Select(l => l.Text)).Trim()
|
2014-06-29 23:04:51 -05:00
|
|
|
|
};
|
|
|
|
|
|
2018-09-25 16:29:53 -05:00
|
|
|
|
if(RegexLib.Anchors.IsMatch(block.Name))
|
2014-06-29 23:04:51 -05:00
|
|
|
|
{
|
2018-09-25 16:29:53 -05:00
|
|
|
|
var sceneName = Utilities.GetInstance(block.Name, block.LineNumber).ParseAnchor(block.Name, block.LineNumber, 1);
|
2014-07-02 23:11:36 -05:00
|
|
|
|
scene.Name = sceneName.Title != null ? sceneName.Title.Trim() : sceneName.Text.Trim();
|
2015-07-19 15:51:10 -05:00
|
|
|
|
scene.Key = Utilities.GetInstance(block.Name, block.LineNumber).NormalizeString(sceneName.Text);
|
2014-07-02 23:11:36 -05:00
|
|
|
|
if(sceneName.Href.Target != null || sceneName.Href.Toggles != null)
|
2018-09-25 16:29:53 -05:00
|
|
|
|
throw new FicdownException(block.Name, "Scene href should only have conditions", block.LineNumber);
|
2014-07-02 23:11:36 -05:00
|
|
|
|
scene.Conditions = sceneName.Href.Conditions;
|
|
|
|
|
}
|
2018-09-25 16:29:53 -05:00
|
|
|
|
else
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
|
|
|
|
scene.Name = block.Name.Trim();
|
2015-07-19 15:51:10 -05:00
|
|
|
|
scene.Key = Utilities.GetInstance(block.Name, block.LineNumber).NormalizeString(block.Name);
|
2014-06-29 23:04:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return scene;
|
|
|
|
|
}
|
2014-07-02 23:11:36 -05:00
|
|
|
|
|
2014-07-26 23:54:50 -05:00
|
|
|
|
private Action BlockToAction(Block block, int id)
|
2014-07-02 23:11:36 -05:00
|
|
|
|
{
|
|
|
|
|
return new Action
|
|
|
|
|
{
|
2014-07-26 23:54:50 -05:00
|
|
|
|
Id = id,
|
2015-07-19 15:51:10 -05:00
|
|
|
|
Toggle = Utilities.GetInstance(block.Name, block.LineNumber).NormalizeString(block.Name),
|
2018-09-25 16:29:53 -05:00
|
|
|
|
RawDescription = string.Join("\n", block.Lines.Select(l => l.Text)),
|
2015-07-19 15:51:10 -05:00
|
|
|
|
Description = string.Join("\n", block.Lines.Select(l => l.Text)).Trim(),
|
|
|
|
|
LineNumber = block.LineNumber
|
2014-07-02 23:11:36 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
2014-06-29 23:04:51 -05:00
|
|
|
|
}
|
2015-07-19 15:51:10 -05:00
|
|
|
|
}
|