protected override void Visit(MacroChunk chunk)
        {
            _source.Write("def ").Write(chunk.Name).Write("(");
            string delimiter = "";
            foreach (var parameter in chunk.Parameters)
            {
                _source.Write(delimiter).Write(parameter.Name);
                delimiter = ",";
            }
            _source.WriteLine(")");
            _source.Indent++;

            var generator = new GeneratedCodeVisitor(_source, _globals);
            _source.WriteLine("__output__scope__=output_scope");

            _source.WriteLine("begin");
            _source.Indent++;
            generator.Accept(chunk.Body);
            _source.WriteLine("return output.to_string");
            _source.Indent--;
            _source.WriteLine("ensure");
            _source.Indent++;
            _source.WriteLine("__output__scope__.dispose");
            _source.Indent--;
            _source.WriteLine("end");

            _source.Indent--;
            _source.WriteLine("end");
        }
Example #2
0
        protected override void Visit(MacroChunk chunk)
        {
            _source.Write("def ").Write(chunk.Name).Write("(");
            string delimiter = "";

            foreach (var parameter in chunk.Parameters)
            {
                _source.Write(delimiter).Write(parameter.Name);
                delimiter = ",";
            }
            _source.WriteLine(")");
            _source.Indent++;

            var generator = new GeneratedCodeVisitor(_source, _globals);

            _source.WriteLine("__output__scope__=output_scope");

            _source.WriteLine("begin");
            _source.Indent++;
            generator.Accept(chunk.Body);
            _source.WriteLine("return output.to_string");
            _source.Indent--;
            _source.WriteLine("ensure");
            _source.Indent++;
            _source.WriteLine("__output__scope__.dispose");
            _source.Indent--;
            _source.WriteLine("end");

            _source.Indent--;
            _source.WriteLine("end");
        }
Example #3
0
        public override void GenerateSourceCode(IEnumerable<IList<Chunk>> viewTemplates, IEnumerable<IList<Chunk>> allResources)
        {
            var script = new SourceWriter();
            var globals = new Dictionary<string, object>();

            script.WriteLine(ScriptHeader);

            script.WriteLine("class<<view");
            script.Indent++;

            var globalMembersVisitor = new GlobalMembersVisitor(script, globals);
            foreach (var resource in allResources)
                globalMembersVisitor.Accept(resource);

            var globalFunctionsVisitor = new GlobalFunctionsVisitor(script, globals);
            foreach (var resource in allResources)
                globalFunctionsVisitor.Accept(resource);

            var templateIndex = 0;
            foreach (var template in viewTemplates)
            {
                script.Write("def render_view_level").WriteLine(templateIndex);
                script.Indent++;

                var generator = new GeneratedCodeVisitor(script, globals);
                generator.Accept(template);

                script.Indent--;
                script.WriteLine("end");

                templateIndex++;
            }

            script.WriteLine("def render");
            script.Indent++;

            var globalInitializeVisitor = new GlobalInitializeVisitor(script);
            foreach(var resource in allResources)
                globalInitializeVisitor.Accept(resource);

            for (var renderIndex = 0; renderIndex != templateIndex; ++renderIndex)
            {
                if (renderIndex < templateIndex - 1)
                {
                    script.WriteLine("scope = output_scope");
                    script. Write("render_view_level").WriteLine(renderIndex);
                    script.WriteLine("content.set_Item \"view\", output");
                    script.WriteLine("scope.dispose");
                }
                else
                {
                    script.Write("render_view_level").WriteLine(renderIndex);
                }
            }
            script.Indent--;
            script.WriteLine("end");

            script.Indent--;
            script.WriteLine("end");
            script.WriteLine("view.view_data.each {|kv| view.instance_variable_set \"@\"+kv.key, kv.value}");
            script.WriteLine("view.render");

            var baseClassGenerator = new BaseClassVisitor { BaseClass = BaseClass };
            foreach (var resource in allResources)
                baseClassGenerator.Accept(resource);

            BaseClass = baseClassGenerator.BaseClassTypeName;

            var source = new StringBuilder();

            var viewClassName = "View" + GeneratedViewId.ToString("n");
            if (Descriptor != null && !string.IsNullOrEmpty(Descriptor.TargetNamespace))
            {
                ViewClassFullName = Descriptor.TargetNamespace + "." + viewClassName;
                source.Append("namespace ").AppendLine(Descriptor.TargetNamespace);
                source.AppendLine("{");
            }
            else
            {
                ViewClassFullName = viewClassName;
            }

            if (Descriptor != null)
            {
                // [SparkView] attribute
                source.AppendLine("[global::Spark.SparkViewAttribute(");
                if (TargetNamespace != null)
                    source.AppendFormat("    TargetNamespace=\"{0}\",", TargetNamespace).AppendLine();
                source.AppendLine("    Templates = new string[] {");
                source.Append("      ").AppendLine(string.Join(",\r\n      ",
                                                               Descriptor.Templates.Select(
                                                                   t => "\"" + t.Replace("\\", "\\\\") + "\"").ToArray()));
                source.AppendLine("    })]");
            }

            source.Append("public class ").Append(viewClassName).Append(" : ").Append(BaseClass).AppendLine(", global::Spark.Ruby.IScriptingSparkView");
            source.AppendLine("{");

            source.Append("static System.Guid _generatedViewId = new System.Guid(\"").Append(GeneratedViewId).AppendLine("\");");
            source.AppendLine("public override System.Guid GeneratedViewId");
            source.AppendLine("{");
            source.AppendLine("get { return _generatedViewId; }");
            source.AppendLine("}");

            source.AppendLine("public global::System.IDisposable OutputScopeAdapter(object arg) ");
            source.AppendLine("{");
            source.AppendLine("if (arg == null) return OutputScope();");
            source.AppendLine("if (arg is global::System.IO.TextWriter) return OutputScope((global::System.IO.TextWriter)arg);");
            source.AppendLine("return OutputScope(global::System.Convert.ToString(arg));");
            source.AppendLine("}");

            source.AppendLine("public void OutputWriteAdapter(object arg) ");
            source.AppendLine("{");
            source.AppendLine("Output.Write(arg);");
            source.AppendLine("}");

            source.AppendLine("public global::Microsoft.Scripting.Hosting.CompiledCode CompiledCode {get;set;}");

            source.AppendLine("public string ScriptSource");
            source.AppendLine("{");
            source.Append("get { return @\"").Append(script.ToString().Replace("\"", "\"\"")).AppendLine("\"; }");
            source.AppendLine("}");

            source.AppendLine("public override void Render()");
            source.AppendLine("{");
            source.AppendLine("CompiledCode.Execute(");
            source.AppendLine("CompiledCode.Engine.CreateScope(");
            source.AppendLine("new global::Spark.Ruby.ScriptingViewSymbolDictionary(this)");
            source.AppendLine("));");
            source.AppendLine("}");

            source.AppendLine("}");

            if (Descriptor != null && !string.IsNullOrEmpty(Descriptor.TargetNamespace))
            {
                source.AppendLine("}");
            }

            SourceCode = source.ToString();
        }