Ejemplo n.º 1
0
        public void Write(TextWriter textWriter, PageContext pageContext)
        {
            if (textWriter == null)
            {
                throw new ArgumentNullException("textWriter");
            }

            if (pageContext == null)
            {
                pageContext = new PageContext(this, new Dictionary <string, object>(), true);
            }

            var blocks = pageContext.RenderHtml ? this.HtmlBlocks : this.MarkdownBlocks;

            if (Interlocked.Increment(ref timesRun) == 1)
            {
                lock (readWriteLock)
                {
                    try
                    {
                        isBusy = true;

                        this.ExecutionContext.BaseType       = Markdown.MarkdownBaseType;
                        this.ExecutionContext.TypeProperties = Markdown.MarkdownGlobalHelpers;

                        pageContext.MarkdownPage = this;
                        var initHtmlContext     = pageContext.Create(this, true);
                        var initMarkdownContext = pageContext.Create(this, false);

                        foreach (var block in this.HtmlBlocks)
                        {
                            lastBlockProcessed = block;
                            block.DoFirstRun(initHtmlContext);
                        }
                        foreach (var block in this.MarkdownBlocks)
                        {
                            lastBlockProcessed = block;
                            block.DoFirstRun(initMarkdownContext);
                        }

                        this.evaluator = this.ExecutionContext.Build();

                        foreach (var block in this.HtmlBlocks)
                        {
                            lastBlockProcessed = block;
                            block.AfterFirstRun(evaluator);
                        }
                        foreach (var block in this.MarkdownBlocks)
                        {
                            lastBlockProcessed = block;
                            block.AfterFirstRun(evaluator);
                        }

                        AddDependentPages(blocks);

                        lastBlockProcessed   = null;
                        initException        = null;
                        hasCompletedFirstRun = true;
                    }
                    catch (Exception ex)
                    {
                        initException = ex;
                        throw;
                    }
                    finally
                    {
                        isBusy = false;
                    }
                }
            }

            lock (readWriteLock)
            {
                while (isBusy)
                {
                    Monitor.Wait(readWriteLock);
                }
            }

            if (initException != null)
            {
                timesRun = 0;
                throw initException;
            }

#if NETSTANDARD1_6
            textWriter.Write(pageContext.MarkdownPage.Contents);
            return;
#else
            MarkdownViewBase instance = null;
            if (this.evaluator != null)
            {
                instance = (MarkdownViewBase)this.evaluator.CreateInstance();

                object model;
                pageContext.ScopeArgs.TryGetValue(ModelName, out model);

                instance.Init(Markdown.AppHost, this, pageContext.ScopeArgs, model, pageContext.RenderHtml);
                instance.ViewEngine = Markdown;
            }

            foreach (var block in blocks)
            {
                block.Write(instance, textWriter, pageContext.ScopeArgs);
            }

            if (instance != null)
            {
                instance.OnLoad();
            }
#endif
        }
Ejemplo n.º 2
0
        public static T Eval <T>(string code)
        {
            var eval = new Evaluator(typeof(T), code, StaticMethodName);

            return((T)eval.Evaluate(StaticMethodName));
        }
Ejemplo n.º 3
0
        public void AfterFirstRun(Evaluator evaluator)
        {
            this.Evaluator = evaluator;

            OnAfterFirstRun();
        }
Ejemplo n.º 4
0
        public static object Eval(string code)
        {
            var eval = new Evaluator(typeof(object), code, StaticMethodName);

            return(eval.Evaluate(StaticMethodName));
        }
Ejemplo n.º 5
0
        public T Evaluate <T>(Dictionary <string, object> scopeArgs, bool defaultToNullValues)
        {
            var paramValues = GetParamValues(scopeArgs, defaultToNullValues);

            return((T)Evaluator.Evaluate(CodeGenMethodName, paramValues.ToArray()));
        }