Changes to Indexer

This commit is contained in:
Jaex 2016-02-03 12:58:54 +02:00
parent 49d20dd4c0
commit e374ccbd43
4 changed files with 87 additions and 78 deletions

View file

@ -33,52 +33,45 @@ namespace ShareX.IndexerLib
{
public abstract class Indexer
{
protected IndexerSettings config = null;
protected StringBuilder sbContent = new StringBuilder();
protected IndexerSettings settings = null;
protected Indexer(IndexerSettings indexerSettings)
{
config = indexerSettings;
settings = indexerSettings;
}
public static string Index(string folderPath, IndexerSettings config)
public static string Index(string folderPath, IndexerSettings settings)
{
Indexer indexer = null;
switch (config.Output)
switch (settings.Output)
{
case IndexerOutput.Html:
indexer = new IndexerHtml(config);
indexer = new IndexerHtml(settings);
break;
case IndexerOutput.Txt:
indexer = new IndexerText(config);
indexer = new IndexerText(settings);
break;
case IndexerOutput.Xml:
indexer = new IndexerXml(config);
indexer = new IndexerXml(settings);
break;
case IndexerOutput.Json:
indexer = new IndexerJson(config);
indexer = new IndexerJson(settings);
break;
}
return indexer.Index(folderPath);
}
public virtual string Index(string folderPath)
{
FolderInfo folderInfo = GetFolderInfo(folderPath);
folderInfo.Update();
public abstract string Index(string folderPath);
IndexFolder(folderInfo);
return sbContent.ToString();
}
protected abstract void IndexFolder(FolderInfo dir, int level = 0);
protected FolderInfo GetFolderInfo(string folderPath, int level = 0)
{
FolderInfo folderInfo = new FolderInfo(folderPath);
if (config.MaxDepthLevel == 0 || level < config.MaxDepthLevel)
if (settings.MaxDepthLevel == 0 || level < settings.MaxDepthLevel)
{
try
{
@ -86,7 +79,7 @@ protected FolderInfo GetFolderInfo(string folderPath, int level = 0)
foreach (DirectoryInfo directoryInfo in currentDirectoryInfo.EnumerateDirectories())
{
if (config.SkipHiddenFolders && directoryInfo.Attributes.HasFlag(FileAttributes.Hidden))
if (settings.SkipHiddenFolders && directoryInfo.Attributes.HasFlag(FileAttributes.Hidden))
{
continue;
}
@ -98,7 +91,7 @@ protected FolderInfo GetFolderInfo(string folderPath, int level = 0)
foreach (FileInfo fileInfo in currentDirectoryInfo.EnumerateFiles())
{
if (config.SkipHiddenFiles && fileInfo.Attributes.HasFlag(FileAttributes.Hidden))
if (settings.SkipHiddenFiles && fileInfo.Attributes.HasFlag(FileAttributes.Hidden))
{
continue;
}
@ -115,36 +108,5 @@ protected FolderInfo GetFolderInfo(string folderPath, int level = 0)
return folderInfo;
}
protected abstract void IndexFolder(FolderInfo dir, int level = 0);
protected virtual string GetFolderNameRow(FolderInfo dir, int level = 0)
{
string folderNameRow = string.Format("{0}{1}", config.IndentationText.Repeat(level), dir.FolderName);
if (config.ShowSizeInfo && dir.Size > 0)
{
folderNameRow += string.Format(" [{0}]", dir.Size.ToSizeString(config.BinaryUnits));
}
return folderNameRow;
}
protected virtual string GetFileNameRow(FileInfo fi, int level = 0)
{
string fileNameRow = config.IndentationText.Repeat(level) + fi.Name;
if (config.ShowSizeInfo)
{
fileNameRow += string.Format(" [{0}]", fi.Length.ToSizeString(config.BinaryUnits));
}
return fileNameRow;
}
protected virtual string GetFooter()
{
return $"Generated by {Application.ProductName} {Application.ProductVersion} on {DateTime.UtcNow.ToString("yyyy-MM-dd 'at' HH:mm:ss 'UTC'")}. Latest version can be downloaded from: {Links.URL_WEBSITE}";
}
}
}

View file

@ -34,6 +34,8 @@ namespace ShareX.IndexerLib
{
public class IndexerHtml : Indexer
{
protected StringBuilder sbContent = new StringBuilder();
public IndexerHtml(IndexerSettings indexerSettings) : base(indexerSettings)
{
}
@ -50,15 +52,21 @@ public override string Index(string folderPath)
sbHtmlIndex.AppendLine(GetCssStyle());
sbHtmlIndex.AppendLine(HtmlHelper.EndTag("head"));
sbHtmlIndex.AppendLine(HtmlHelper.StartTag("body"));
string index = base.Index(folderPath).Trim();
FolderInfo folderInfo = GetFolderInfo(folderPath);
folderInfo.Update();
IndexFolder(folderInfo);
string index = sbContent.ToString().Trim();
sbHtmlIndex.AppendLine(index);
if (config.AddFooter) sbHtmlIndex.AppendLine(HtmlHelper.StartTag("div") + GetFooter() + HtmlHelper.EndTag("div"));
if (settings.AddFooter) sbHtmlIndex.AppendLine(HtmlHelper.StartTag("div") + GetFooter() + HtmlHelper.EndTag("div"));
sbHtmlIndex.AppendLine(HtmlHelper.EndTag("body"));
sbHtmlIndex.AppendLine(HtmlHelper.EndTag("html"));
return sbHtmlIndex.ToString().Trim();
}
protected override void IndexFolder(FolderInfo dir, int level)
protected override void IndexFolder(FolderInfo dir, int level = 0)
{
sbContent.AppendLine(GetFolderNameRow(dir, level));
@ -85,15 +93,15 @@ protected override void IndexFolder(FolderInfo dir, int level)
sbContent.AppendLine(HtmlHelper.EndTag("div"));
}
protected override string GetFolderNameRow(FolderInfo dir, int level)
private string GetFolderNameRow(FolderInfo dir, int level)
{
string folderNameRow = "";
if (!dir.IsEmpty)
{
if (config.ShowSizeInfo)
if (settings.ShowSizeInfo)
{
folderNameRow += dir.Size.ToSizeString(config.BinaryUnits) + " ";
folderNameRow += dir.Size.ToSizeString(settings.BinaryUnits) + " ";
}
folderNameRow += "(";
@ -122,13 +130,13 @@ protected override string GetFolderNameRow(FolderInfo dir, int level)
return HtmlHelper.StartTag("h" + heading) + URLHelpers.HtmlEncode(dir.FolderName) + folderNameRow + HtmlHelper.EndTag("h" + heading);
}
protected override string GetFileNameRow(FileInfo fi, int level)
private string GetFileNameRow(FileInfo fi, int level)
{
string fileNameRow = HtmlHelper.StartTag("li") + URLHelpers.HtmlEncode(fi.Name);
if (config.ShowSizeInfo)
if (settings.ShowSizeInfo)
{
fileNameRow += " " + HtmlHelper.Tag("span", fi.Length.ToSizeString(config.BinaryUnits), "", "class=\"FileSize\"");
fileNameRow += " " + HtmlHelper.Tag("span", fi.Length.ToSizeString(settings.BinaryUnits), "", "class=\"FileSize\"");
}
fileNameRow += HtmlHelper.EndTag("li");
@ -136,7 +144,7 @@ protected override string GetFileNameRow(FileInfo fi, int level)
return fileNameRow;
}
protected override string GetFooter()
private string GetFooter()
{
return string.Format("Generated by {0} on {1}.", string.Format("<a href=\"{0}\">{1} {2}</a>", Links.URL_WEBSITE, Application.ProductName, Application.ProductVersion),
DateTime.UtcNow.ToString("yyyy-MM-dd 'at' HH:mm:ss 'UTC'"));
@ -146,9 +154,9 @@ private string GetCssStyle()
{
string css;
if (config.UseCustomCSSFile && !string.IsNullOrEmpty(config.CustomCSSFilePath) && File.Exists(config.CustomCSSFilePath))
if (settings.UseCustomCSSFile && !string.IsNullOrEmpty(settings.CustomCSSFilePath) && File.Exists(settings.CustomCSSFilePath))
{
css = File.ReadAllText(config.CustomCSSFilePath, Encoding.UTF8);
css = File.ReadAllText(settings.CustomCSSFilePath, Encoding.UTF8);
}
else
{

View file

@ -24,13 +24,17 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace ShareX.IndexerLib
{
public class IndexerText : Indexer
{
protected StringBuilder sbContent = new StringBuilder();
public IndexerText(IndexerSettings indexerSettings) : base(indexerSettings)
{
}
@ -38,9 +42,15 @@ public IndexerText(IndexerSettings indexerSettings) : base(indexerSettings)
public override string Index(string folderPath)
{
StringBuilder sbTxtIndex = new StringBuilder();
string index = base.Index(folderPath).Trim();
FolderInfo folderInfo = GetFolderInfo(folderPath);
folderInfo.Update();
IndexFolder(folderInfo);
string index = sbContent.ToString().Trim();
sbTxtIndex.AppendLine(index);
if (config.AddFooter)
if (settings.AddFooter)
{
string footer = GetFooter();
sbTxtIndex.AppendLine("_".Repeat(footer.Length));
@ -49,13 +59,13 @@ public override string Index(string folderPath)
return sbTxtIndex.ToString().Trim();
}
protected override void IndexFolder(FolderInfo dir, int level)
protected override void IndexFolder(FolderInfo dir, int level = 0)
{
sbContent.AppendLine(GetFolderNameRow(dir, level));
foreach (FolderInfo subdir in dir.Folders)
{
if (config.AddEmptyLineAfterFolders)
if (settings.AddEmptyLineAfterFolders)
{
sbContent.AppendLine();
}
@ -65,7 +75,7 @@ protected override void IndexFolder(FolderInfo dir, int level)
if (dir.Files.Count > 0)
{
if (config.AddEmptyLineAfterFolders)
if (settings.AddEmptyLineAfterFolders)
{
sbContent.AppendLine();
}
@ -76,5 +86,34 @@ protected override void IndexFolder(FolderInfo dir, int level)
}
}
}
private string GetFolderNameRow(FolderInfo dir, int level = 0)
{
string folderNameRow = string.Format("{0}{1}", settings.IndentationText.Repeat(level), dir.FolderName);
if (settings.ShowSizeInfo && dir.Size > 0)
{
folderNameRow += string.Format(" [{0}]", dir.Size.ToSizeString(settings.BinaryUnits));
}
return folderNameRow;
}
private string GetFileNameRow(FileInfo fi, int level = 0)
{
string fileNameRow = settings.IndentationText.Repeat(level) + fi.Name;
if (settings.ShowSizeInfo)
{
fileNameRow += string.Format(" [{0}]", fi.Length.ToSizeString(settings.BinaryUnits));
}
return fileNameRow;
}
private string GetFooter()
{
return $"Generated by {Application.ProductName} {Application.ProductVersion} on {DateTime.UtcNow.ToString("yyyy-MM-dd 'at' HH:mm:ss 'UTC'")}. Latest version can be downloaded from: {Links.URL_WEBSITE}";
}
}
}

View file

@ -66,22 +66,22 @@ protected override void IndexFolder(FolderInfo dir, int level = 0)
{
xmlWriter.WriteStartElement("Folder");
if (config.UseAttribute)
if (settings.UseAttribute)
{
xmlWriter.WriteAttributeString("Name", dir.FolderName);
if (config.ShowSizeInfo && !dir.IsEmpty)
if (settings.ShowSizeInfo && !dir.IsEmpty)
{
xmlWriter.WriteAttributeString("Size", dir.Size.ToSizeString(config.BinaryUnits));
xmlWriter.WriteAttributeString("Size", dir.Size.ToSizeString(settings.BinaryUnits));
}
}
else
{
xmlWriter.WriteElementString("Name", dir.FolderName);
if (config.ShowSizeInfo && !dir.IsEmpty)
if (settings.ShowSizeInfo && !dir.IsEmpty)
{
xmlWriter.WriteElementString("Size", dir.Size.ToSizeString(config.BinaryUnits));
xmlWriter.WriteElementString("Size", dir.Size.ToSizeString(settings.BinaryUnits));
}
}
@ -93,22 +93,22 @@ protected override void IndexFolder(FolderInfo dir, int level = 0)
{
xmlWriter.WriteStartElement("File");
if (config.UseAttribute)
if (settings.UseAttribute)
{
xmlWriter.WriteAttributeString("Name", fi.Name);
if (config.ShowSizeInfo)
if (settings.ShowSizeInfo)
{
xmlWriter.WriteAttributeString("Size", fi.Length.ToSizeString(config.BinaryUnits));
xmlWriter.WriteAttributeString("Size", fi.Length.ToSizeString(settings.BinaryUnits));
}
}
else
{
xmlWriter.WriteElementString("Name", fi.Name);
if (config.ShowSizeInfo)
if (settings.ShowSizeInfo)
{
xmlWriter.WriteElementString("Size", fi.Length.ToSizeString(config.BinaryUnits));
xmlWriter.WriteElementString("Size", fi.Length.ToSizeString(settings.BinaryUnits));
}
}