public static string ReplaceInlineArgs(AbstractEmitterBlock block, string inline, Expression[] args)
        {
            var emitter = block.Emitter;

            inline = _formatArg.Replace(inline, delegate(Match m)
            {
                int count       = emitter.Writers.Count;
                string separate = m.Groups[1].Value;
                string key      = m.Groups[3].Value;
                string modifier = m.Groups[2].Success ? m.Groups[5].Value : null;

                StringBuilder oldSb = emitter.Output;
                emitter.Output      = new StringBuilder();

                Expression expr = null;

                if (Regex.IsMatch(key, "^\\d+$"))
                {
                    expr = args.Skip(int.Parse(key)).FirstOrDefault();
                }
                else
                {
                    expr = args.FirstOrDefault(e => e.ToString() == key);
                }

                string s = "";
                if (expr != null)
                {
                    var writer = block.SaveWriter();
                    block.NewWriter();
                    expr.AcceptVisitor(emitter);
                    s = emitter.Output.ToString();
                    block.RestoreWriter(writer);

                    if (modifier == "raw")
                    {
                        s = s.Trim('"');
                    }
                }

                block.Write(block.WriteIndentToString(s));

                if (emitter.Writers.Count != count)
                {
                    block.PopWriter();
                }

                string replacement = emitter.Output.ToString();
                emitter.Output     = oldSb;

                return(replacement);
            });

            return(inline);
        }
Esempio n. 2
0
        protected virtual Dictionary <string, string> CombineOutputs()
        {
            Dictionary <string, string> result = new Dictionary <string, string>();

            foreach (var outputPair in this.Outputs)
            {
                var fileName = outputPair.Key;
                var output   = outputPair.Value;

                string extension = Path.GetExtension(fileName);
                bool   isJs      = extension == ('.' + Bridge.Translator.AssemblyInfo.JAVASCRIPT_EXTENSION);

                foreach (var moduleOutput in output.ModuleOutput)
                {
                    WriteNewLine(output.NonModuletOutput, moduleOutput.Value.ToString());
                }

                var tmp = new StringBuilder(output.TopOutput.Length + output.BottomOutput.Length + output.NonModuletOutput.Length + 100);

                if (output.TopOutput.Length > 0)
                {
                    tmp.Append(output.TopOutput.ToString());
                    tmp.Append("\n");
                }

                if (isJs)
                {
                    tmp.Append(this.GetOutputHeader(true, false));
                    tmp.Append("\n");
                }

                if (output.NonModuletOutput.Length > 0)
                {
                    if (isJs)
                    {
                        tmp.Append("(function (globals) {");
                        tmp.Append("\n");
                        tmp.Append("    ");
                        tmp.Append(this.GetOutputHeader(false, true));
                        tmp.Append("\n");
                    }

                    var code = output.NonModuletOutput.ToString() + (isJs ? "\n\nBridge.init();" : "");

                    if (isJs)
                    {
                        code = "    " + AbstractEmitterBlock.WriteIndentToString(code, 1);
                    }

                    tmp.Append(code);

                    if (isJs)
                    {
                        tmp.Append("\n");
                        tmp.Append("})(this);");
                        tmp.Append("\n");
                    }
                }

                if (output.BottomOutput.Length > 0)
                {
                    tmp.Append("\n");
                    tmp.Append(output.BottomOutput.ToString());
                }

                result.Add(fileName, tmp.ToString());
            }

            return(result);
        }
        public static string ReplaceInlineArgs(AbstractEmitterBlock block, string inline, Expression[] args)
        {
            var emitter = block.Emitter;
            inline = _formatArg.Replace(inline, delegate(Match m)
            {
                int count = emitter.Writers.Count;
                string key = m.Groups[2].Value;
                string modifier = m.Groups[1].Success ? m.Groups[4].Value : null;

                StringBuilder oldSb = emitter.Output;
                emitter.Output = new StringBuilder();

                Expression expr = null;

                if (Regex.IsMatch(key, "^\\d+$"))
                {
                    expr = args.Skip(int.Parse(key)).FirstOrDefault();
                }
                else
                {
                    expr = args.FirstOrDefault(e => e.ToString() == key);
                }

                string s = "";
                if (expr != null)
                {
                    var writer = block.SaveWriter();
                    block.NewWriter();
                    expr.AcceptVisitor(emitter);
                    s = emitter.Output.ToString();
                    block.RestoreWriter(writer);

                    if (modifier == "raw")
                    {
                        s = s.Trim('"');
                    }
                }

                block.Write(block.WriteIndentToString(s));

                if (emitter.Writers.Count != count)
                {
                    block.PopWriter();
                }

                string replacement = emitter.Output.ToString();
                emitter.Output = oldSb;

                return replacement;
            });

            return inline;
        }