public static IEnumerable <IAsset> GetAllAssets(this IAssetsFolder folder) { foreach (var asset in folder.Assets) { yield return(asset); } foreach (var subFolder in folder.Folders) { foreach (var subAsset in GetAllAssets(subFolder)) { yield return(subAsset); } } if (folder is IPage) { foreach (var subPage in (folder as IPage).SubPages) { foreach (var subPageAsset in GetAllAssets(subPage)) { yield return(subPageAsset); } } } }
private Task OnPreCompile(ISite site) { m_Site = site; AssetsHelper.AddTextAsset(Resources.code_snippet_css, site.MainPage, CSS_FILE_PATH); AssetsHelper.AddTextAsset(Resources.code_snippet_js, site.MainPage, JS_FILE_PATH); m_SnippetFileIds = new List <string>(); m_UsedTabIds = new Dictionary <IPage, List <string> >(); m_UsedSnippetIds = new Dictionary <IPage, List <string> >(); if (!string.IsNullOrEmpty(m_Settings.SnippetsFolder)) { try { m_SnippetsFolder = site.MainPage.FindFolder(PluginLocation.FromPath(m_Settings.SnippetsFolder)); } catch (AssetNotFoundException) { throw new PluginUserMessageException($"Failed to find the folder for snippets: '{m_Settings.SnippetsFolder}'"); } foreach (var snipAsset in m_SnippetsFolder.GetAllAssets()) { m_SnippetFileIds.Add(snipAsset.Id); } } return(Task.CompletedTask); }
private void LoadAssets(IAssetsFolder folder, List <IFile> assets, ILocation curLoc) { var pageAssets = assets.Where(a => a.Location.IsInLocation(curLoc, m_Comparison)).ToList(); var children = pageAssets.Where(p => p.Location.GetParent().IsSame(curLoc, m_Comparison)).ToList(); folder.Assets.AddRange(children.Select(a => { var fileName = a.Location.FileName; if (string.IsNullOrEmpty(fileName)) { //file with no extension fileName = a.Location.Segments.Last(); } return(new Asset(fileName, a.Content, a.Id)); })); children.ForEach(a => assets.Remove(a)); children.ForEach(a => pageAssets.Remove(a)); if (pageAssets.Any()) { foreach (var subFolderName in pageAssets.Select( a => GetBaseSegment(a.Location.GetRelative(curLoc))).Distinct(m_Comparer)) { var subFolder = new AssetsFolder(subFolderName); folder.Folders.Add(subFolder); LoadAssets(subFolder, assets, curLoc.Combine(new Location("", "", new string[] { subFolderName }))); } } }
public static IAsset FindAsset(ISite site, IAssetsFolder page, string path) { var loc = PluginLocation.FromPath(path); var folder = GetBaseFolder(site, page, loc); return(folder.FindAsset(loc)); }
private async IAsyncEnumerable <IFile> CompileAssets(IAssetsFolder folder, IPage page, ISite site, ILocation baseLoc) { foreach (var asset in folder.Assets) { var thisLoc = baseLoc.Combine(new Location("", asset.FileName, Enumerable.Empty <string>())); if (thisLoc.Matches(m_Config.CompilableAssetsFilter)) { yield return(await CompileAsset(asset, site, page, thisLoc)); } else { yield return(new File(thisLoc, asset.Content, asset.Id)); } } foreach (var subFolder in folder.Folders) { var folderLoc = baseLoc.Combine(new Location("", "", new string[] { subFolder.Name })); await foreach (var subFolderAsset in CompileAssets(subFolder, page, site, folderLoc)) { yield return(subFolderAsset); } } }
public static IAssetsFolder GetBaseFolder(ISite site, IAssetsFolder page, ILocation loc) { if (loc.IsRelative()) { return(page); } else { return(site.MainPage); } }
public static IAsset FindAsset(this IAssetsFolder folder, ILocation path) { if (!path.IsFile()) { throw new BaseUserMessageException("Location is not a file"); } var fileName = path.FileName; var curFolder = folder.FindFolder(path); var asset = curFolder.Assets.FirstOrDefault(a => string.Equals(a.FileName, fileName)); if (asset == null) { throw new AssetNotFoundException(curFolder, fileName); } return(asset); }
private IAssetsFolder FindSnippetFolder(ISite site, IPage page, ref string snipLoc) { IAssetsFolder snippetsBaseFolder = null; if (snipLoc.StartsWith(SNIPPETS_FOLDER_PATH)) { if (m_SnippetsFolder == null) { throw new PluginUserMessageException("Snippets folder is not set"); } snipLoc = snipLoc.TrimStart(SNIPPETS_FOLDER_PATH); snippetsBaseFolder = m_SnippetsFolder; } else { snippetsBaseFolder = AssetsHelper.GetBaseFolder(site, page, PluginLocation.FromPath(snipLoc)); } return(snippetsBaseFolder.FindFolder(PluginLocation.FromPath(snipLoc))); }
public static IAssetsFolder FindFolder(this IAssetsFolder curFolder, ILocation path) { foreach (var curDir in path.Segments) { var nextDir = curFolder.Folders.FirstOrDefault(f => string.Equals(f.Name, curDir)); if (nextDir == null) { if (curFolder is IPage) { nextDir = (curFolder as IPage).SubPages.FirstOrDefault(p => string.Equals(p.Name, curDir)); } } if (nextDir == null) { throw new AssetNotFoundException(curFolder, curDir); } curFolder = nextDir; } return(curFolder); }
public static void AddAsset(byte[] content, IPage page, string path) { var parts = path.Split(PluginLocation.PathSeparators, StringSplitOptions.RemoveEmptyEntries); IAssetsFolder curFolder = page; for (int i = 0; i < parts.Length - 1; i++) { var name = parts[i]; var nextFolder = curFolder.Folders.FirstOrDefault( f => string.Equals(f.Name, name, StringComparison.CurrentCultureIgnoreCase)); if (nextFolder == null) { nextFolder = new PluginAssetsFolder(name); curFolder.Folders.Add(nextFolder); } curFolder = nextFolder; } curFolder.Assets.Add(new PluginAsset(content, parts.Last())); }
public AssetNotFoundException(IAssetsFolder dir, string path) : base($"Failed to find the asset from path: '{path}' in '{dir.Name}'") { }
public static IAsset FindAsset(this IAssetsFolder folder, string path) => FindAsset(folder, new BaseLocation(path));
public static IAssetsFolder FindFolder(this IAssetsFolder curFolder, string path) => FindFolder(curFolder, new BaseLocation(path));