public SchemaDrivenDocumentProcessor(
            DocumentSchema schema,
            ICompositionContainer container,
            MarkdigMarkdownService markdigMarkdownService,
            FolderRedirectionManager folderRedirectionManager)
        {
            if (string.IsNullOrWhiteSpace(schema.Title))
            {
                throw new ArgumentException("Title for schema must not be empty");
            }

            _schemaName               = schema.Title;
            _schema                   = schema;
            SchemaValidator           = schema.Validator;
            _allowOverwrite           = schema.AllowOverwrite;
            _serializerPool           = new ResourcePoolManager <JsonSerializer>(GetSerializer, 0x10);
            _markdigMarkdownService   = markdigMarkdownService ?? throw new ArgumentNullException(nameof(MarkdigMarkdownService));
            _folderRedirectionManager = folderRedirectionManager;
            if (container != null)
            {
                var commonSteps         = container.GetExports <IDocumentBuildStep>(nameof(SchemaDrivenDocumentProcessor));
                var schemaSpecificSteps = container.GetExports <IDocumentBuildStep>($"{nameof(SchemaDrivenDocumentProcessor)}.{_schemaName}");
                BuildSteps = commonSteps.Union(schemaSpecificSteps).ToList();
            }
        }
Пример #2
0
        public static DocumentSchema Load(TextReader reader, string title)
        {
            using (var json = new JsonTextReader(reader))
            {
                DocumentSchema schema;
                try
                {
                    schema = DefaultSerializer.Value.Deserialize <DocumentSchema>(json);
                }
                catch (Exception e)
                {
                    throw new InvalidSchemaException($"Not a valid schema: {e.Message}", e);
                }

                SchemaValidator.Validate(schema);

                if (string.IsNullOrWhiteSpace(schema.Title))
                {
                    if (string.IsNullOrWhiteSpace(title))
                    {
                        throw new InvalidSchemaException($"Title of schema must be specified.");
                    }
                    schema.Title = title;
                }

                if (schema.Type != JSchemaType.Object)
                {
                    throw new InvalidSchemaException("Type for the root schema object must be object");
                }

                return(schema);
            }
        }
Пример #3
0
        public static DocumentSchema Load(TextReader reader, string title)
        {
            DocumentSchema schema;

            using (var jtr = new JsonTextReader(reader))
            {
                var jObject = JObject.Load(jtr);
                var jSchema = JSchema.Load(jObject.CreateReader());

                var validator = new SchemaValidator(jObject, jSchema);

                // validate schema here
                validator.ValidateMetaSchema();

                try
                {
                    schema = LoadSchema <DocumentSchema>(jSchema, new Dictionary <JSchema, BaseSchema>());
                    schema.SchemaVersion = jSchema.SchemaVersion;
                    schema.Id            = jSchema.Id;
                    schema.Version       = GetValueFromJSchemaExtensionData <string>(jSchema, "version");
                    schema.Metadata      = GetValueFromJSchemaExtensionData <string>(jSchema, "metadata");
                    schema.Validator     = validator;
                }
                catch (Exception e)
                {
                    throw new InvalidSchemaException($"{title} is not a valid schema: {e.Message}", e);
                }

                if (string.IsNullOrWhiteSpace(schema.Title))
                {
                    if (string.IsNullOrWhiteSpace(title))
                    {
                        throw new InvalidSchemaException($"Title of schema must be specified.");
                    }
                    schema.Title = title;
                }

                if (schema.Type != JSchemaType.Object)
                {
                    throw new InvalidSchemaException("Type for the root schema object must be object");
                }

                if (!JsonPointer.TryCreate(schema.Metadata, out var pointer))
                {
                    throw new InvalidJsonPointerException($"Metadata's json pointer {schema.Metadata} is invalid.");
                }

                var metadataSchema = pointer.FindSchema(schema);
                if (metadataSchema != null && metadataSchema.Type != JSchemaType.Object)
                {
                    throw new InvalidJsonPointerException($"The referenced object is in type: {metadataSchema.Type}, only object can be a metadata reference");
                }

                schema.MetadataReference = pointer;
                schema.AllowOverwrite    = CheckOverwriteAbility(schema);
                return(schema);
            }
        }
Пример #4
0
        public static DocumentSchema Load(TextReader reader, string title)
        {
            using (var json = new JsonTextReader(reader))
            {
                DocumentSchema schema;
                try
                {
                    schema = DefaultSerializer.Value.Deserialize <DocumentSchema>(json);
                }
                catch (Exception e)
                {
                    throw new InvalidSchemaException($"Not a valid schema: {e.Message}", e);
                }

                SchemaValidator.Validate(schema);

                if (string.IsNullOrWhiteSpace(schema.Title))
                {
                    if (string.IsNullOrWhiteSpace(title))
                    {
                        throw new InvalidSchemaException($"Title of schema must be specified.");
                    }
                    schema.Title = title;
                }

                if (schema.Type != JSchemaType.Object)
                {
                    throw new InvalidSchemaException("Type for the root schema object must be object");
                }

                if (!JsonPointer.TryCreate(schema.Metadata, out var pointer))
                {
                    throw new InvalidJsonPointerException($"Metadata's json pointer {schema.Metadata} is invalid.");
                }

                var metadataSchema = pointer.FindSchema(schema);
                if (metadataSchema != null && metadataSchema.Type != JSchemaType.Object)
                {
                    throw new InvalidJsonPointerException($"The referenced object is in type: {metadataSchema.Type}, only object can be a metadata reference");
                }

                schema.MetadataReference = pointer;

                return(schema);
            }
        }
Пример #5
0
        public SchemaDrivenDocumentProcessor(DocumentSchema schema, ICompositionContainer container)
        {
            if (string.IsNullOrWhiteSpace(schema.Title))
            {
                throw new ArgumentException("Title for schema must not be empty");
            }

            _schemaName      = schema.Title;
            _schema          = schema;
            _schemaValidator = new SchemaValidator(_schema);

            _serializerPool = new ResourcePoolManager <JsonSerializer>(GetSerializer, 0x10);
            if (container != null)
            {
                var commonSteps         = container.GetExports <IDocumentBuildStep>(nameof(SchemaDrivenDocumentProcessor));
                var schemaSpecificSteps = container.GetExports <IDocumentBuildStep>($"{nameof(SchemaDrivenDocumentProcessor)}.{_schemaName}");
                BuildSteps = commonSteps.Union(schemaSpecificSteps).ToList();
            }
        }
        public override FileModel Load(FileAndType file, ImmutableDictionary <string, object> metadata)
        {
            switch (file.Type)
            {
            case DocumentType.Article:
                // TODO: Support dynamic in YAML deserializer
                try
                {
                    // MUST be a dictionary
                    var obj = YamlUtility.Deserialize <Dictionary <string, object> >(file.File);

                    // load overwrite fragments
                    string markdownFragmentsContent = null;
                    var    markdownFragmentsFile    = file.File + ".md";
                    if (_folderRedirectionManager != null)
                    {
                        markdownFragmentsFile = _folderRedirectionManager.GetRedirectedPath((RelativePath)markdownFragmentsFile).ToString();
                    }
                    if (EnvironmentContext.FileAbstractLayer.Exists(markdownFragmentsFile))
                    {
                        markdownFragmentsContent = EnvironmentContext.FileAbstractLayer.ReadAllText(markdownFragmentsFile);
                    }
                    else
                    {
                        // Validate against the schema first, only when markdown fragments don't exist
                        SchemaValidator.Validate(obj);
                    }

                    var content = ConvertToObjectHelper.ConvertToDynamic(obj);
                    if (!(_schema.MetadataReference.GetValue(content) is IDictionary <string, object> pageMetadata))
                    {
                        pageMetadata = new ExpandoObject();
                        _schema.MetadataReference.SetValue(ref content, pageMetadata);
                    }
                    foreach (var pair in metadata)
                    {
                        if (!pageMetadata.ContainsKey(pair.Key))
                        {
                            pageMetadata[pair.Key] = pair.Value;
                        }
                    }

                    var localPathFromRoot = PathUtility.MakeRelativePath(EnvironmentContext.BaseDirectory, EnvironmentContext.FileAbstractLayer.GetPhysicalPath(file.File));

                    var fm = new FileModel(
                        file,
                        content,
                        serializer: new BinaryFormatter())
                    {
                        LocalPathFromRoot = localPathFromRoot,
                    };
                    fm.MarkdownFragmentsModel = new FileModel(
                        new FileAndType(
                            file.BaseDir,
                            markdownFragmentsFile,
                            DocumentType.MarkdownFragments,
                            file.SourceDir,
                            file.DestinationDir),
                        markdownFragmentsContent,
                        serializer: new BinaryFormatter());
                    fm.Properties.Schema   = _schema;
                    fm.Properties.Metadata = pageMetadata;
                    fm.MarkdownFragmentsModel.Properties.MarkdigMarkdownService = _markdigMarkdownService;
                    fm.MarkdownFragmentsModel.Properties.Metadata = pageMetadata;
                    if (markdownFragmentsContent != null)
                    {
                        fm.MarkdownFragmentsModel.LocalPathFromRoot = PathUtility.MakeRelativePath(
                            EnvironmentContext.BaseDirectory,
                            EnvironmentContext.FileAbstractLayer.GetPhysicalPath(markdownFragmentsFile));
                    }
                    return(fm);
                }
                catch (YamlDotNet.Core.YamlException e)
                {
                    throw new DocumentException($"{file.File} is not in supported format: {e.Message}", e);
                }

            case DocumentType.Overwrite:
                return(OverwriteDocumentReader.Read(file));

            default:
                throw new NotSupportedException();
            }
        }