Пример #1
0
        public virtual CodeFenceOptionsParseResult TryParseCodeFenceOptions(
            string line,
            MarkdownParserContext parserContext = null)
        {
            if (parserContext.TryGetDefaultCodeBlockAnnotations(out var defaults))
            {
                if (defaults.Package != null &&
                    !line.Contains(PackageOptionName))
                {
                    line += $" {PackageOptionName} {defaults.Package}";
                }

                if (defaults.PackageVersion != null &&
                    !line.Contains(PackageVersionOptionName))
                {
                    line += $" {PackageVersionOptionName} {defaults.PackageVersion}";
                }
            }

            var result = _parser.Parse(line);

            if (!_supportedLanguages.Contains(result.CommandResult.Command.Name) ||
                result.Tokens.Count == 1)
            {
                return(CodeFenceOptionsParseResult.None);
            }

            // FIX: (TryParseCodeFenceOptions) account for different options types

            if (result.Errors.Any())
            {
                return(CodeFenceOptionsParseResult.Failed(new List <string>(result.Errors.Select(e => e.Message))));
            }

            var modelBinder = _modelBindersByCommand[result.CommandResult.Command].Value;

            var annotations = modelBinder.CreateInstance(new BindingContext(result));

            switch (annotations)
            {
            case CodeBlockAnnotations codeBlockAnnotations:
                return(CodeFenceOptionsParseResult.Succeeded(codeBlockAnnotations));

            case OutputBlockAnnotations outputBlockAnnotations:
                return(CodeFenceOptionsParseResult.Succeeded(outputBlockAnnotations));

            case null:
                return(CodeFenceOptionsParseResult.Failed($"Failed to bind annotations: {result}"));

            default:
                return(CodeFenceOptionsParseResult.Failed($"Unrecognized annotations type: {annotations}"));
            }
        }
Пример #2
0
        public virtual CodeFenceOptionsParseResult TryParseCodeFenceOptions(
            string line,
            MarkdownParserContext parserContext = null)
        {
            if (parserContext.TryGetDefaultCodeBlockAnnotations(out var defaults))
            {
                if (defaults.Package != null &&
                    !line.Contains(packageOptionName))
                {
                    line += $" {packageOptionName} {defaults.Package}";
                }

                if (defaults.PackageVersion != null &&
                    !line.Contains(packageVersionOptionName))
                {
                    line += $" {packageVersionOptionName} {defaults.PackageVersion}";
                }
            }

            var result = _parser.Parse(line);

            if (result.CommandResult.Name != "csharp" ||
                result.Tokens.Count == 1)
            {
                return(CodeFenceOptionsParseResult.None);
            }

            if (result.Errors.Any())
            {
                return(CodeFenceOptionsParseResult.Failed(new List <string>(result.Errors.Select(e => e.Message))));
            }
            else
            {
                var annotations = (CodeBlockAnnotations)_modelBinder.Value.CreateInstance(new BindingContext(result));

                annotations.Language = result.Tokens.First().Value;
                annotations.RunArgs  = Untokenize(result);

                return(CodeFenceOptionsParseResult.Succeeded(annotations));
            }
        }