/// <summary> /// Gets a <see cref="IReadOnlyList{T}"/> of <see cref="Chunk"/> containing parsed results of _ViewStart files /// that are used for inheriting tag helpers and chunks to the page located at <paramref name="pagePath"/>. /// </summary> /// <param name="pagePath">The path of the page to locate inherited chunks for.</param> /// <returns>A <see cref="IReadOnlyList{T}"/> of <see cref="Chunk"/> from _ViewStart pages.</returns> public IReadOnlyList <Chunk> GetInheritedChunks([NotNull] string pagePath) { var inheritedChunks = new List <Chunk>(); var templateEngine = new RazorTemplateEngine(_razorHost); foreach (var viewStart in ViewStartUtility.GetViewStartLocations(_fileSystem, pagePath)) { CodeTree codeTree; IFileInfo fileInfo; if (_parsedCodeTrees.TryGetValue(viewStart, out codeTree)) { inheritedChunks.AddRange(codeTree.Chunks); } else if (_fileSystem.TryGetFileInfo(viewStart, out fileInfo)) { codeTree = ParseViewFile(templateEngine, fileInfo); _parsedCodeTrees.Add(viewStart, codeTree); inheritedChunks.AddRange(codeTree.Chunks); } } inheritedChunks.AddRange(_defaultInheritedChunks); return(inheritedChunks); }
/// <summary> /// Gets an ordered <see cref="IReadOnlyList{T}"/> of parsed <see cref="CodeTree"/> for each _ViewStart that /// is applicable to the page located at <paramref name="pagePath"/>. The list is ordered so that the /// <see cref="CodeTree"/> for the _ViewStart closest to the <paramref name="pagePath"/> in the fileProvider /// appears first. /// </summary> /// <param name="pagePath">The path of the page to locate inherited chunks for.</param> /// <returns>A <see cref="IReadOnlyList{CodeTree}"/> of parsed _ViewStart <see cref="CodeTree"/>s.</returns> public IReadOnlyList <CodeTree> GetInheritedCodeTrees([NotNull] string pagePath) { var inheritedCodeTrees = new List <CodeTree>(); var templateEngine = new RazorTemplateEngine(_razorHost); foreach (var viewStartPath in ViewStartUtility.GetViewStartLocations(pagePath)) { CodeTree codeTree; if (_parsedCodeTrees.TryGetValue(viewStartPath, out codeTree)) { inheritedCodeTrees.Add(codeTree); } else { var fileInfo = _fileProvider.GetFileInfo(viewStartPath); if (fileInfo.Exists) { // viewStartPath contains the app-relative path of the ViewStart. // Since the parsing of a _ViewStart would cause parent _ViewStarts to be parsed // we need to ensure the paths are app-relative to allow the GetViewStartLocations // for the current _ViewStart to succeed. codeTree = ParseViewFile(templateEngine, fileInfo, viewStartPath); _parsedCodeTrees.Add(viewStartPath, codeTree); inheritedCodeTrees.Add(codeTree); } } } return(inheritedCodeTrees); }
/// <summary> /// Gets the list of chunks that are to be inherited by a specified page. /// Chunks are inherited from _ViewStarts that are applicable to the page. /// </summary> /// <param name="razorHost">The <see cref="MvcRazorHost"/> used to parse _ViewStart pages.</param> /// <param name="fileSystem">The filesystem that represents the application.</param> /// <param name="pagePath">The path of the page to locate inherited chunks for.</param> /// <returns>A list of chunks that are applicable to the given page.</returns> public List <Chunk> GetInheritedChunks([NotNull] MvcRazorHost razorHost, [NotNull] IFileSystem fileSystem, [NotNull] string pagePath) { var inheritedChunks = new List <Chunk>(); var templateEngine = new RazorTemplateEngine(razorHost); foreach (var viewStart in ViewStartUtility.GetViewStartLocations(fileSystem, pagePath)) { IFileInfo fileInfo; if (fileSystem.TryGetFileInfo(viewStart, out fileInfo)) { var parsedTree = ParseViewFile(templateEngine, fileInfo); var chunksToAdd = parsedTree.Chunks .Where(chunk => ChunkMergers.ContainsKey(chunk.GetType())); inheritedChunks.AddRange(chunksToAdd); } } inheritedChunks.AddRange(_defaultInheritedChunks); return(inheritedChunks); }