public override IReaderMode Read(int indention, string text) { if (text.StartsWith("*")) { var mode = new GrammarMode(_fixture, g => _paragraph.AddChild(g)); mode.Read(indention, text); return(mode); } // It's a new grammar, so get on out of here if (text.IsHeaderTwo() || text.IsHeaderThree()) { return(null); } return(this); }
public void with_sentence_children() { theParagraph.AddChild(new Sentence { format = "Go left" }); theParagraph.AddChild(new Sentence { format = "Go right" }); var code = theParagraph.ToMissingCode(); code.ShouldContain("[StoryTeller.Hidden]"); code.ShouldContain("public void TheParagraph_1()"); code.ShouldContain("public void TheParagraph_2()"); code.ShouldContain("public StoryTeller.IGrammar TheParagraph()"); code.ShouldContain("return Paragraph(\"The Paragraph\", _ => {"); code.ShouldContain("_ += this[\"TheParagraph_1\"];"); code.ShouldContain("_ += this[\"TheParagraph_2\"];"); }
public IReaderMode Read(int indention, string line) { if (_hasAdded && _paragraph == null) { return(null); } if (line.IsHeaderTwo()) { // Get out of here, this is a new grammar! if (_hasAdded) { return(null); } var value = line.Trim().TrimStart('#', ' '); if (value.Contains(' ')) { _title = value; _key = _title.Replace(" ", " ").Split(' ').Select(x => x.Capitalize()).Join(""); } else { _key = value; // Default title _title = _key.SplitPascalCase(); } return(this); } if (line.IsHeaderThree()) { _title = line.Trim().TrimStart('#', ' '); return(this); } if (line.StartsWith("*")) { if (WithinParagraph) { var title = line.TrimStart('*').Trim(); if (_title.IsEmpty()) { _title = title; return(this); } // There's some internal clumsiness about reading children // within a Paragraph if (_title == title) { return(this); } if (!_hasAdded) { addSentence(); } return(null); } var childGrammar = new GrammarMode(_fixture, g => _paragraph.AddChild(g)) { WithinParagraph = true }; if (_paragraph == null) { _hasAdded = true; _paragraph = new Paragraph { key = _key, title = _title }; _adder(_paragraph); } else { childGrammar.Read(indention, line); } return(childGrammar); } if (line.StartsWith("fact", StringComparison.OrdinalIgnoreCase)) { if (!_hasAdded) { var sentence = addSentence(); sentence.fact = true; return(this); } } if (line.StartsWith("embeds", StringComparison.OrdinalIgnoreCase)) { return(_paragraph == null?buildEmbed(line) : this); } if (line.IsTableLine()) { var values = line.ToTableValues(); if (values.Any()) { switch (values[0].ToLower()) { case "sentence": var sentence = addSentence(); return(new SentenceMode(sentence)); case "table": return(buildTable()); case "set": return(buildSet(false)); case "ordered-set": return(buildSet(true)); default: throw new ArgumentOutOfRangeException($"'{values[0]}' is not a valid option here. Valid options are: 'sentence', 'table', 'set', or 'ordered-set'"); } } addSentence(); } if (line.IsEmpty() && !_hasAdded && _title.IsNotEmpty()) { addSentence(); } return(null); }