Esempio n. 1
0
        protected override void Write(HtmlRenderer renderer, CodeBlock codeBlock)
        {
            renderer.EnsureLine();

            var fencedCodeBlock = codeBlock as FencedCodeBlock;
            var info            = fencedCodeBlock?.Info;

            if (info != null && (info.StartsWithIgnoreCase("{r") || info.StartsWithIgnoreCase("{ r")))
            {
                var text       = fencedCodeBlock.GetText();
                var rCodeBlock = new RCodeBlock(_blockNumber, text, fencedCodeBlock.Arguments);

                var result = GetCachedResult(_blockNumber, rCodeBlock.Hash, fencedCodeBlock);
                if (result != null)
                {
                    WriteBlockContent(renderer, _blockNumber, text);
                    renderer.Write(result);
                }
                else
                {
                    var elementId = rCodeBlock.HtmlElementId;
                    _blocks.Add(rCodeBlock);

                    WriteBlockContent(renderer, _blockNumber, text);
                    // Write placeholder first. We will insert actual data when the evaluation is done.
                    renderer.Write(GetBlockPlaceholder(elementId, text));
                }
                _blockNumber++;
            }
        }
Esempio n. 2
0
        public async Task <string> EvaluateBlockAsync(RCodeBlock block, CancellationToken ct)
        {
            block.State = CodeBlockState.Created;
            await TaskUtilities.SwitchToBackgroundThread();

            try {
                ct.ThrowIfCancellationRequested();
                var session = await EvalSession.StartSessionAsync(ct);

                var callback = EvalSession.SessionCallback;

                session.Output += OnSessionOutput;
                await ExecuteAndCaptureOutputAsync(session, block.Text, ct);

                if (callback.PlotResult != null)
                {
                    block.Result        = Invariant($"<img src='data:image/gif;base64, {Convert.ToBase64String(callback.PlotResult)}' />");
                    callback.PlotResult = null;
                }
                else if (_output.Length > 0)
                {
                    block.Result = Invariant($"<code style='white-space: pre-wrap'>{_output.ToString()}</code>");
                }
                else if (_errors.Length > 0)
                {
                    block.Result = block.DisplayErrors ? FormatError(_errors.ToString()) : string.Empty;
                }
            } catch (Exception ex) when(!ex.IsCriticalException())
            {
                _output      = _errors = null;
                block.Result = FormatError(ex.Message);
            } finally {
                if (EvalSession.RSession != null)
                {
                    EvalSession.RSession.Output -= OnSessionOutput;
                }
                SignalActivity();
            }

            block.State = CodeBlockState.Evaluated;
            return(block.Result);
        }
Esempio n. 3
0
        protected override void Write(HtmlRenderer renderer, CodeBlock codeBlock)
        {
            renderer.EnsureLine();

            var fencedCodeBlock = codeBlock as FencedCodeBlock;

            if (codeBlock.Column > 0 || fencedCodeBlock?.Info == null)
            {
                if (codeBlock.Span.Length > 0)
                {
                    var text = new string(' ', codeBlock.Column) + _documentText.Substring(codeBlock.Span.Start, codeBlock.Span.Length);
                    renderer.Write(GetCodeHtml(text));
                }
                return;
            }

            // For R blocks indent count must be zero (no whitespace before ```{r}
            if (MarkdownUtility.GetRCodeBlockSeparatorLength(fencedCodeBlock.Info, out var start))
            {
                var text       = fencedCodeBlock.GetText();
                var rCodeBlock = new RCodeBlock(_blockNumber, text, fencedCodeBlock.Arguments);

                var result = GetCachedResult(_blockNumber, rCodeBlock.Hash, fencedCodeBlock);
                if (result != null)
                {
                    renderer.Write(result);
                }
                else
                {
                    var elementId = rCodeBlock.HtmlElementId;
                    _blocks.Add(rCodeBlock);

                    // Write placeholder first. We will insert actual data when the evaluation is done.
                    var renderText = rCodeBlock.Eval
                            ? GetBlockPlaceholder(elementId, text)
                            : GetCodeHtml(text);
                    renderer.Write(renderText);
                }
                _blockNumber++;
            }
        }