示例#1
0
        public ParseResult Run(MapFileItemViewModel item, IndexerContext context)
        {
            var    filePath = context.MarkdownFilePath;
            var    markdownMapFileOutputFolder = context.MarkdownMapFileOutputFolder;
            string markdownMapFileName         = Path.GetFileName(filePath) + Constants.MapFileExtension;
            string markdownFolder = Path.GetDirectoryName(filePath);

            // Use the same folder as api.yaml if the output folder is not set
            string markdownMapFileFolder = (string.IsNullOrEmpty(markdownMapFileOutputFolder) ? markdownFolder : markdownMapFileOutputFolder)
                                           ?? string.Empty;
            string markdownMapFileFullPath = Path.Combine(markdownMapFileFolder, markdownMapFileName);

            // Post-process item
            // if references'/overrides count is 0, set it to null
            if (item.References != null && item.References.Count == 0)
            {
                item.References = null;
            }
            if (item.CustomProperties != null && item.CustomProperties.Count == 0)
            {
                item.CustomProperties = null;
            }

            if (!item.HasUsefulInfo())
            {
                return(new ParseResult(ResultLevel.Info, "Finish processing {0}, Map file will not be generated as no neccessory info is contained", context.MarkdownFilePath));
            }

            MapFileViewModel mapFile = new MapFileViewModel();

            // For map file, always one key corresponding to the value
            mapFile.Add("default", item);
            JsonUtility.Serialize(markdownMapFileFullPath, mapFile);

            return(new ParseResult(ResultLevel.Success, "Finish processing {0}, successfully generated {0}", context.MarkdownFilePath, markdownMapFileFullPath));
        }
示例#2
0
        public ParseResult Run(MapFileItemViewModel item, IndexerContext context)
        {
            var filePath = context.MarkdownFilePath;
            var content  = context.MarkdownContent;
            var apis     = context.ExternalApiIndex;
            var apiMapFileOutputFolder = context.ApiMapFileOutputFolder;
            var yamlHeaders            = YamlHeaderParser.Select(content);

            if (yamlHeaders == null || yamlHeaders.Count == 0)
            {
                return(new ParseResult(ResultLevel.Info, "No valid yaml header reference found for {0}", filePath));
            }

            if (item.References == null)
            {
                item.References = new ReferencesViewModel();
            }
            ReferencesViewModel    references = item.References;
            List <MarkdownSection> sections   = SplitToSections(content, yamlHeaders);
            Dictionary <string, MarkdownSection> validMarkdownSections = new Dictionary <string, MarkdownSection>();

            foreach (var markdownSection in sections)
            {
                if (!string.IsNullOrEmpty(markdownSection.Id))
                {
                    validMarkdownSections[markdownSection.Id] = markdownSection;
                }
            }

            foreach (var yamlHeader in yamlHeaders)
            {
                var referenceId = yamlHeader.Id;
                var apiId       = yamlHeader.Id;

                MetadataItem api;
                if (apis.TryGetValue(apiId, out api))
                {
                    var reference = new MapFileItemViewModel
                    {
                        Id            = referenceId,
                        ReferenceKeys = yamlHeader.MatchedSections,
                        Href          = api.Href,
                    };
                    // *DONOT* Add references to Markdown file
                    // references.AddItem(reference);

                    // 2. Write api reference to API's map file
                    MarkdownSection markdownSection;
                    if (!validMarkdownSections.TryGetValue(apiId, out markdownSection))
                    {
                        continue;
                    }

                    var    apiPath        = api.Href;
                    var    apiIndexPath   = context.ApiIndexFilePath;
                    var    apiYamlPath    = FileExtensions.GetFullPath(Path.GetDirectoryName(apiIndexPath), apiPath);
                    string apiMapFileName = Path.GetFileName(apiPath) + Constants.MapFileExtension;
                    string apiFolder      = Path.GetDirectoryName(apiYamlPath);

                    // Use the same folder as api.yaml if the output folder is not set
                    string apiMapFileFolder   = (string.IsNullOrEmpty(apiMapFileOutputFolder) ? apiFolder : apiMapFileOutputFolder);
                    string apiMapFileFullPath = FileExtensions.GetFullPath(apiMapFileFolder, apiMapFileName);

                    // Path should be the relative path from .yml to .md
                    var markdownFilePath = context.MarkdownFilePath;
                    var indexFolder      = Path.GetDirectoryName(context.ApiIndexFilePath);
                    var apiYamlFilePath  = FileExtensions.GetFullPath(indexFolder, api.Href);
                    var relativePath     = FileExtensions.MakeRelativePath(Path.GetDirectoryName(apiYamlFilePath), markdownFilePath).BackSlashToForwardSlash();
                    MapFileItemViewModel apiMapFileSection = new MapFileItemViewModel
                    {
                        Id               = apiId,
                        Remote           = item.Remote,
                        Href             = relativePath,
                        Startline        = markdownSection.Location.StartLocation.Line + 1,
                        Endline          = markdownSection.Location.EndLocation.Line + 1, // Endline + 1 - 1, +1 for it starts from 0, -1 for it is actually the start line for next charactor, in code snippet, is always a \n
                        References       = SelectReferenceSection(references, markdownSection.Location),
                        CustomProperties = yamlHeader.Properties,
                        MapFileType      = MapFileType.Yaml
                    };
                    MapFileViewModel apiMapFile;
                    if (File.Exists(apiMapFileFullPath))
                    {
                        apiMapFile = JsonUtility.Deserialize <MapFileViewModel>(apiMapFileFullPath);
                    }
                    else
                    {
                        apiMapFile = new MapFileViewModel();
                    }

                    // Current behavior: Override existing one
                    apiMapFile[apiId] = apiMapFileSection;

                    // Post-process item
                    // if references'/overrides count is 0, set it to null
                    if (apiMapFileSection.References != null && apiMapFileSection.References.Count == 0)
                    {
                        apiMapFileSection.References = null;
                    }
                    if (apiMapFileSection.CustomProperties != null && apiMapFileSection.CustomProperties.Count == 0)
                    {
                        apiMapFileSection.CustomProperties = null;
                    }

                    JsonUtility.Serialize(apiMapFileFullPath, apiMapFile);
                    ParseResult.WriteToConsole(ResultLevel.Success, "Successfully generated {0}.", apiMapFileFullPath);
                }
            }

            // Select references to the indices where DefinedLine is between startline and endline
            return(new ParseResult(ResultLevel.Success));
        }