Example #1
0
        private void WriteNamespaces(TextWriter writer)
        {
            writer.Write(this.formatter.GlobalsFormat);

            if (!String.IsNullOrEmpty(this.ProxyNamespace))
            {
                EcmaScriptWriter.WriteNamespaceDeclaration(writer, this.ProxyNamespace, null, this.formatter.IsDebug);
            }
        }
Example #2
0
        public void Render(TextWriter writer)
        {
            this.ProcessDirectives();

            // add JSLINT directives
            string globals = this.GetGlobals();

            if (!String.IsNullOrEmpty(globals))
            {
                writer.WriteLine("/*global {0} */", globals);
            }

            if (!EcmaScriptWriter.WriteNamespaceDeclaration(writer, this.JbstName, null, true))
            {
                writer.Write("var ");
            }

            // wrap with ctor and assign
            writer.Write(this.JbstName);
            writer.WriteLine(" = JsonML.BST(");

            // render root node of content (null is OK)
            EcmaScriptWriter jsWriter = new EcmaScriptWriter(writer);

            jsWriter.Settings.PrettyPrint = true;
            jsWriter.Write(this.JbstParseTree);

            writer.WriteLine(");");

            // render any declarations
            if (this.Declarations.HasCode)
            {
                this.Declarations.OwnerName = this.JbstName;
                jsWriter.Write(this.Declarations);
            }
        }
Example #3
0
        /// <summary>
        /// Renders the data items as a block of JavaScript
        /// </summary>
        /// <param name="writer"></param>
        public void Write(TextWriter writer, IDictionary <string, object> data)
        {
            if (data == null || data.Count < 1)
            {
                // emit nothing when empty
                return;
            }

            List <string> namespaces = new List <string>();

            StringWriter     markup;
            EcmaScriptWriter jsWriter;

            if (this.AutoMarkup == AutoMarkupType.Data)
            {
                markup   = new StringWriter();
                jsWriter = new JsonMarkupWriter(writer, markup);
            }
            else
            {
                markup   = null;
                jsWriter = new EcmaScriptWriter(writer);
            }

            if (this.IsDebug)
            {
                jsWriter.Settings.PrettyPrint = true;
                jsWriter.Settings.NewLine     = Environment.NewLine;
                jsWriter.Settings.Tab         = "\t";
            }

            writer.Write(DataBlockWriter.ScriptOpen);

            foreach (string key in data.Keys)
            {
                if (markup != null)
                {
                    if (this.IsDebug)
                    {
                        markup.WriteLine();
                    }
                    markup.Write("<div title=\"");
                    HttpUtility.HtmlAttributeEncode(key, markup);
                    markup.Write("\">");
                }

                string declaration;
                if (!EcmaScriptWriter.WriteNamespaceDeclaration(writer, key, namespaces, this.IsDebug))
                {
                    declaration = "var " + key;
                }
                else
                {
                    declaration = key;
                }

                if (this.IsDebug)
                {
                    writer.Write(DataBlockWriter.VarAssignmentDebug, declaration);
                    if (data[key] != null &&
                        data[key].GetType().IsClass)
                    {
                        writer.WriteLine();
                    }
                }
                else
                {
                    writer.Write(DataBlockWriter.VarAssignment, declaration);
                }

                // emit the value as JSON
                jsWriter.Write(data[key]);
                writer.Write(DataBlockWriter.VarAssignmentEnd);

                if (markup != null)
                {
                    markup.Write("</div>");
                }

                if (this.IsDebug)
                {
                    writer.WriteLine();
                }
            }

            writer.Write(DataBlockWriter.ScriptClose);

            if (markup != null)
            {
                writer.Write(DataBlockWriter.NoScriptOpen);
                writer.Write(markup.ToString());
                writer.Write(DataBlockWriter.NoScriptClose);
            }
        }