public void TestTemplateConditional()
 {
     var document = new SimpleDocument("{if value = 1:foo|else:{if value = 2:bar|else:baz}}");
     var store = new BuiltinStore();
     store["value"] = 1;
     var result = document.Render(store);
     Assert.AreEqual("foo", result);
     store["value"] = 2;
     result = document.Render(store);
     Assert.AreEqual("bar", result);
     store["value"] = 3;
     result = document.Render(store);
     Assert.AreEqual("baz", result);
 }
        public void TestTemplateOneOf()
        {
            Random random   = new Random();
            var    document = new SimpleDocument("The letter is {OneOf(\"a\", \"b\", \"c\", \"d\", null)}.");
            var    store    = new BuiltinStore();

            store["OneOf"] = new NativeFunction((values) =>
            {
                return(values[random.Next(values.Count)]);
            });
            store["system"] = "Alrai";
            List <string> results = new List <string>();

            for (int i = 0; i < 1000; i++)
            {
                results.Add(document.Render(store));
            }
            Assert.IsTrue(results.Contains(@"The letter is a."));
            results.RemoveAll(result => result == @"The letter is a.");
            Assert.IsTrue(results.Contains(@"The letter is b."));
            results.RemoveAll(result => result == @"The letter is b.");
            Assert.IsTrue(results.Contains(@"The letter is c."));
            results.RemoveAll(result => result == @"The letter is c.");
            Assert.IsTrue(results.Contains(@"The letter is d."));
            results.RemoveAll(result => result == @"The letter is d.");
            Assert.IsTrue(results.Contains(@"The letter is ."));
            results.RemoveAll(result => result == @"The letter is .");
            Assert.IsTrue(results.Count == 0);
        }
Esempio n. 3
0
        public void TooLongStringsTest(string variable)
        {
            var    sb = new StringBuilder();
            string input;

            // Add some variables in the text to test a use case where we don't have only text
            if (!string.IsNullOrEmpty(variable))
            {
                sb.Append(variable);
            }

            sb.Append('a', 45000);

            // Add some variables in the text to test a use case where we don't have only text
            if (!string.IsNullOrEmpty(variable))
            {
                sb.Append(variable);
            }

            input = sb.ToString();
            IStore store = new SimpleStore();

            store["foo"] = "{foo}";
            var doc = new SimpleDocument(input);
            var res = doc.Render(store);

            Assert.AreEqual(input, res);
        }
Esempio n. 4
0
        private static void AssertPrint(string expression, string expected)
        {
            IDocument document = new SimpleDocument("{echo " + expression + "}");
            IStore    store    = new BuiltinStore();

            Assert.AreEqual(expected, document.Render(store), "'{0}' doesn't render to '{1}'", expression, expected);
        }
Esempio n. 5
0
        private static void AssertEqual(string expression, string expected)
        {
            IDocument document = new SimpleDocument("{eq(" + expression + ", " + expected + ")}");
            IStore    store    = new BuiltinStore();

            Assert.AreEqual("true", document.Render(store), "'{0}' doesn't evaluate to '{1}'", expression, expected);
        }
Esempio n. 6
0
        private static void AssertResult(string expression, bool expected)
        {
            IDocument document = new SimpleDocument("{" + (expected ? "" : "!") + "(" + expression + ")}");
            IStore    store    = new BuiltinStore();

            Assert.AreEqual("true", document.Render(store));
        }
Esempio n. 7
0
        private void buttonEvaluate_Click(object sender, EventArgs e)
        {
            SimpleDocument document;
            ISetting       setting;
            IStore         store;

            setting = this.SettingCreate();

            try
            {
                document = new SimpleDocument(this.textBoxInput.Text, setting);
                store    = new BuiltinStore();

                foreach (TreeNode root in this.treeViewValue.Nodes)
                {
                    foreach (KeyValuePair <Value, Value> pair in this.ValuesBuild(root.Nodes))
                    {
                        store.Set(pair.Key, pair.Value, StoreMode.Global);
                    }
                }

                this.DisplayText(document.Render(store));
            }
            catch (ParseException exception)
            {
                this.DisplayError(exception);
            }
        }
Esempio n. 8
0
        public virtual string GetHtml()
        {
            if (HtmlTemplate == null)
            {
                return(null);
            }

            var document = new SimpleDocument(HtmlTemplate);
            var store    = new BuiltinStore();

            foreach (var prop in TemplateModel.GetType().GetProperties())
            {
                store[prop.Name] = prop.GetValue(TemplateModel).ToString();
            }

            var body         = document.Render(store);
            var baseDocument = new SimpleDocument(BaseHtmlTemplate);

            store = new BuiltinStore
            {
                ["Body"] = body
            };
            var result = baseDocument.Render(store);

            return(result);
        }
Esempio n. 9
0
        public void TextTooLong(string variable, int characters)
        {
            var    builder = new StringBuilder();
            var    store   = new SimpleStore();
            string input;

            // Add some variables in the text to test a use case where we don't have only text
            if (!string.IsNullOrEmpty(variable))
            {
                builder.Append(variable);
            }

            builder.Append('a', characters);

            // Add some variables in the text to test a use case where we don't have only text
            if (!string.IsNullOrEmpty(variable))
            {
                builder.Append(variable);
            }

            input = builder.ToString();

            store["foo"] = "{foo}";

            var document = new SimpleDocument(input);
            var output   = document.Render(store);

            Assert.AreEqual(input, output);
        }
        public void TestTemplateConditional()
        {
            var document = new SimpleDocument("{if value = 1:foo|else:{if value = 2:bar|else:baz}}");
            var store    = new BuiltinStore();

            store["value"] = 1;
            var result = document.Render(store);

            Assert.AreEqual("foo", result);
            store["value"] = 2;
            result         = document.Render(store);
            Assert.AreEqual("bar", result);
            store["value"] = 3;
            result         = document.Render(store);
            Assert.AreEqual("baz", result);
        }
 public void TestTemplateOneOf()
 {
     Random random = new Random();
     var document = new SimpleDocument("The letter is {OneOf(\"a\", \"b\", \"c\", \"d\", null)}.");
     var store = new BuiltinStore();
     store["OneOf"] = new NativeFunction((values) =>
     {
         return values[random.Next(values.Count)];
     });
     store["system"] = "Alrai";
     List<string> results = new List<string>();
     for (int i = 0; i < 1000; i++)
     {
         results.Add(document.Render(store));
     }
     Assert.IsTrue(results.Contains(@"The letter is a."));
     results.RemoveAll(result => result == @"The letter is a.");
     Assert.IsTrue(results.Contains(@"The letter is b."));
     results.RemoveAll(result => result == @"The letter is b.");
     Assert.IsTrue(results.Contains(@"The letter is c."));
     results.RemoveAll(result => result == @"The letter is c.");
     Assert.IsTrue(results.Contains(@"The letter is d."));
     results.RemoveAll(result => result == @"The letter is d.");
     Assert.IsTrue(results.Contains(@"The letter is ."));
     results.RemoveAll(result => result == @"The letter is .");
     Assert.IsTrue(results.Count == 0);
 }
 public void TestTemplateSimple()
 {
     var document = new SimpleDocument(@"Hello {name}!");
     var store = new BuiltinStore();
     store["name"] = "world";
     var result = document.Render(store);
     Assert.AreEqual("Hello world!", result);
 }
        public void TestTemplateSimple()
        {
            var document = new SimpleDocument(@"Hello {name}!");
            var store    = new BuiltinStore();

            store["name"] = "world";
            var result = document.Render(store);

            Assert.AreEqual("Hello world!", result);
        }
 public void TestTemplateFunctional()
 {
     var document = new SimpleDocument(@"You are entering the {System(system)} system.");
     var store = new BuiltinStore();
     store["System"] = new NativeFunction((values) =>
     {
         return Translations.StarSystem(values[0].AsString);
     }, 1);
     store["system"] = "Alrai";
     var result = document.Render(store);
     Assert.AreEqual("You are entering the <phoneme alphabet=\"ipa\" ph=\"ˈalraɪ\">Alrai</phoneme> system.", result);
 }
Esempio n. 15
0
        /// <summary>
        /// Resolve a script with an existing store
        /// </summary>
        public string resolveScript(string script, BuiltinStore store, bool master = true)
        {
            try
            {
                // Before we start, we remove the context for master scripts.
                // This means that scripts without context still work as expected
                if (master)
                {
                    EDDI.Instance.State["eddi_context_last_subject"] = null;
                    EDDI.Instance.State["eddi_context_last_action"]  = null;
                }

                var document = new SimpleDocument(script, setting);
                var result   = document.Render(store);
                // Tidy up the output script
                result = Regex.Replace(result, " +", " ").Replace(" ,", ",").Replace(" .", ".").Trim();
                Logging.Debug("Turned script " + script + " in to speech " + result);
                result = result.Trim() == "" ? null : result.Trim();

                if (master && result != null)
                {
                    string stored = result;
                    // Remove any leading pause
                    if (stored.StartsWith("<break"))
                    {
                        string pattern     = "^<break[^>]*>";
                        string replacement = "";
                        Regex  rgx         = new Regex(pattern);
                        stored = rgx.Replace(stored, replacement);
                    }

                    EDDI.Instance.State["eddi_context_last_speech"] = stored;
                    object lastSubject;
                    if (EDDI.Instance.State.TryGetValue("eddi_context_last_subject", out lastSubject))
                    {
                        if (lastSubject != null)
                        {
                            string csLastSubject = ((string)lastSubject).ToLowerInvariant().Replace(" ", "_");
                            EDDI.Instance.State["eddi_context_last_speech_" + csLastSubject] = stored;
                        }
                    }
                }

                return(result);
            }
            catch (Exception e)
            {
                Logging.Warn("Failed to resolve script: " + e.ToString());
                return("There is a problem with the script: " + e.Message.Replace("'", ""));
            }
        }
Esempio n. 16
0
        private string RenderDocument(IDocument document, BuiltinStore store, string file)
        {
            string text;

            try
            {
                text = document.Render(store); // throws ParseException on error
            }
            catch (ParseException ex)
            {
                Errors      = string.Format("Error rendering {3}:\r\nLine {0}, pos {1}: {2}", ex.Line, ex.Column, ex.Message, file);
                ErrorLine   = ex.Line;
                ErrorColumn = ex.Column;
                return(null);
            }
            if (enableAltRender)
            {
                IDocument altDocument;

                using (var stream = new MemoryStream(Encoding.ASCII.GetBytes(text)))
                {
                    using (var reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        try
                        {
                            altDocument = new SimpleDocument(reader, pjAltSetting); // throws ParseException on error
                        }
                        catch (ParseException ex)
                        {
                            Errors      = string.Format("Error in alt parsing {3}:\r\nLine {0}, pos {1}: {2}", ex.Line, ex.Column, ex.Message, file);
                            ErrorLine   = ex.Line;
                            ErrorColumn = ex.Column;
                            stream.Dispose();
                            return(null);
                        }
                    }
                }
                try
                {
                    text = altDocument.Render(store); // throws ParseException on error
                }
                catch (ParseException ex)
                {
                    Errors      = string.Format("Error in alt rendering {3}:\r\nLine {0}, pos {1}: {2}", ex.Line, ex.Column, ex.Message, file);
                    ErrorLine   = ex.Line;
                    ErrorColumn = ex.Column;
                    return(null);
                }
            }
            return(text);
        }
        public void TestTemplateFunctional()
        {
            var document = new SimpleDocument(@"You are entering the {System(system)} system.");
            var store    = new BuiltinStore();

            store["System"] = new NativeFunction((values) =>
            {
                return(Translations.StarSystem(values[0].AsString));
            }, 1);
            store["system"] = "Alrai";
            var result = document.Render(store);

            Assert.AreEqual("You are entering the <phoneme alphabet=\"ipa\" ph=\"ˈalraɪ\">Alrai</phoneme> system.", result);
        }
Esempio n. 18
0
        /// <summary> From a custom store </summary>
        public string resolveFromValue(string script, BuiltinStore store, bool isTopLevelScript, Script scriptObject = null)
        {
            try
            {
                var document = new SimpleDocument(script, setting);
                var result   = document.Render(store);
                // Tidy up the output script
                result = Regex.Replace(result, " +", " ").Replace(" ,", ",").Replace(" .", ".").Trim();
                Logging.Debug("Turned script " + script + " in to speech " + result);
                result = result.Trim() == "" ? null : result.Trim();

                if (isTopLevelScript && result != null)
                {
                    string stored = result;
                    // Remove any leading pause
                    if (stored.StartsWith("<break"))
                    {
                        string pattern     = "^<break[^>]*>";
                        string replacement = "";
                        Regex  rgx         = new Regex(pattern);
                        stored = rgx.Replace(stored, replacement);
                    }
                    EDDI.Instance.State["eddi_context_last_speech"] = stored;
                }

                return(result);
            }
            catch (Cottle.Exceptions.ParseException e)
            {
                // Report the failing the script name, if it is available
                string scriptName;
                if (scriptObject != null)
                {
                    scriptName = "the script \"" + scriptObject.Name + "\"";
                }
                else
                {
                    scriptName = "this script";
                }

                Logging.Warn($"Failed to resolve {scriptName} at line {e.Line}. {e}");
                return($"There is a problem with {scriptName} at line {e.Line}. {errorTranslation(e.Message)}");
            }
            catch (Exception e)
            {
                Logging.Warn(e.Message, e);
                return($"Error with {scriptObject?.Name ?? "this"} script: {e.Message}");
            }
        }
 /// <summary>
 /// Resolve a script with an existing store
 /// </summary>
 public string resolveScript(string script, BuiltinStore store)
 {
     try
     {
         var document = new SimpleDocument(script, setting);
         var result   = document.Render(store);
         Logging.Debug("Turned script " + script + " in to speech " + result);
         return(result.Trim() == "" ? null : result.Trim());
     }
     catch (Exception e)
     {
         Logging.Warn("Failed to resolve script: " + e.ToString());
         return("There is a problem with the script: " + e.Message.Replace("'", ""));
     }
 }
 /// <summary>
 /// Resolve a script with an existing store
 /// </summary>
 public string resolveScript(string script, BuiltinStore store)
 {
     try
     {
         var document = new SimpleDocument(script, setting);
         var result = document.Render(store);
         Logging.Debug("Turned script " + script + " in to speech " + result);
         return result.Trim() == "" ? null : result.Trim();
     }
     catch (Exception e)
     {
         Logging.Warn("Failed to resolve script: " + e.ToString());
         return "There is a problem with the script: " + e.Message.Replace("'", "");
     }
 }
Esempio n. 21
0
        private static void AssertEqual(string expression, string expected)
        {
            IDocument	document = new SimpleDocument ("{eq(" + expression + ", " + expected + ")}");
            IStore		store = new BuiltinStore ();

            Assert.AreEqual ("true", document.Render (store), "'{0}' doesn't evaluate to '{1}'", expression, expected);
        }
Esempio n. 22
0
        private static void AssertPrint(string expression, string expected)
        {
            IDocument	document = new SimpleDocument ("{echo " + expression + "}");
            IStore		store = new BuiltinStore ();

            Assert.AreEqual (expected, document.Render (store), "'{0}' doesn't render to '{1}'", expression, expected);
        }
Esempio n. 23
0
        public string CreateDocument()
        {
            IDocument document;
            IScope scope;

            using (StreamReader template = new StreamReader(new FileStream("template.html", FileMode.Open), Encoding.UTF8))
            {
                document = new SimpleDocument(template);
            }

            scope = new DefaultScope();

            scope["slideTitle"] = titleTextBox.Text;

            int numExts = extensionsListBox.SelectedItems.Count;

            var stylesheets = new Value[numExts];
            var scripts = new Value[numExts];
            var extSnippets = new Dictionary<Value, Value>();
            int i = 0;
            foreach (string extName in extensionsListBox.SelectedItems)
            {
                stylesheets[i] = extensions[extName] + ".css";
                scripts[i] = extensions[extName] + ".js";

                var snippetName = "deck." + extName;

                if (snippets.ContainsKey(snippetName))
                    extSnippets.Add(snippetName, snippets[snippetName]);

                i++;
            }

            scope["extensionStylesheets"] = stylesheets;
            scope["extensionScripts"] = scripts;
            scope["extensionSnippets"] = extSnippets;
            scope["styleTheme"] = styles[(string)stylesListBox.SelectedItem];
            scope["transitionTheme"] = transitions[(string)transitionsListBox.SelectedItem];

            return document.Render(scope);
        }
Esempio n. 24
0
        private void buttonEvaluate_Click(object sender, EventArgs e)
        {
            SimpleDocument	document;
            ISetting		setting;
            IStore			store;

            setting = this.SettingCreate ();

            try
            {
                document = new SimpleDocument (this.textBoxInput.Text, setting);
                store = new BuiltinStore ();

                foreach (TreeNode root in this.treeViewValue.Nodes)
                {
                    foreach (KeyValuePair<Value, Value> pair in this.ValuesBuild (root.Nodes))
                        store.Set (pair.Key, pair.Value, StoreMode.Global);
                }

                this.DisplayText (document.Render (store));
            }
            catch (ParseException exception)
            {
                this.DisplayError (exception);
            }
        }
Esempio n. 25
0
        private static void AssertResult(string expression, bool expected)
        {
            IDocument	document = new SimpleDocument ("{" + (expected ? "" : "!") + "(" + expression + ")}");
            IStore		store = new BuiltinStore ();

            Assert.AreEqual ("true", document.Render (store));
        }