ShareX/ShareX.IndexerLib/IndexerHtml.cs

182 lines
6.3 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2023-01-10 09:31:02 +13:00
Copyright (c) 2007-2023 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
using ShareX.IndexerLib.Properties;
2013-11-03 23:53:49 +13:00
using System;
using System.IO;
using System.Text;
2014-12-11 09:25:20 +13:00
namespace ShareX.IndexerLib
2013-11-03 23:53:49 +13:00
{
public class IndexerHtml : Indexer
{
2016-02-03 23:58:54 +13:00
protected StringBuilder sbContent = new StringBuilder();
protected int prePathTrim = 0;
2016-02-03 23:58:54 +13:00
2016-02-03 09:06:22 +13:00
public IndexerHtml(IndexerSettings indexerSettings) : base(indexerSettings)
2013-11-03 23:53:49 +13:00
{
}
public override string Index(string folderPath)
{
StringBuilder sbHtmlIndex = new StringBuilder();
sbHtmlIndex.AppendLine("<!DOCTYPE html>");
sbHtmlIndex.AppendLine(HtmlHelper.StartTag("html"));
sbHtmlIndex.AppendLine(HtmlHelper.StartTag("head"));
sbHtmlIndex.AppendLine("<meta charset=\"UTF-8\">");
sbHtmlIndex.AppendLine("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">");
2013-11-03 23:53:49 +13:00
sbHtmlIndex.AppendLine(HtmlHelper.Tag("title", "Index for " + Path.GetFileName(folderPath)));
sbHtmlIndex.AppendLine(GetCssStyle());
2013-11-03 23:53:49 +13:00
sbHtmlIndex.AppendLine(HtmlHelper.EndTag("head"));
sbHtmlIndex.AppendLine(HtmlHelper.StartTag("body"));
2016-02-03 23:58:54 +13:00
folderPath = Path.GetFullPath(folderPath).TrimEnd('\\');
prePathTrim = folderPath.LastIndexOf(@"\") + 1;
2016-02-03 23:58:54 +13:00
FolderInfo folderInfo = GetFolderInfo(folderPath);
folderInfo.Update();
IndexFolder(folderInfo);
string index = sbContent.ToString().Trim();
2013-11-03 23:53:49 +13:00
sbHtmlIndex.AppendLine(index);
2016-02-03 23:58:54 +13:00
if (settings.AddFooter) sbHtmlIndex.AppendLine(HtmlHelper.StartTag("div") + GetFooter() + HtmlHelper.EndTag("div"));
2013-11-03 23:53:49 +13:00
sbHtmlIndex.AppendLine(HtmlHelper.EndTag("body"));
sbHtmlIndex.AppendLine(HtmlHelper.EndTag("html"));
return sbHtmlIndex.ToString().Trim();
}
2016-02-03 23:58:54 +13:00
protected override void IndexFolder(FolderInfo dir, int level = 0)
2013-11-03 23:53:49 +13:00
{
sbContent.AppendLine(GetFolderNameRow(dir, level));
string divClass = level > 0 ? "FolderBorder" : "MainFolderBorder";
sbContent.AppendLine(HtmlHelper.StartTag("div", "", $"class=\"{divClass}\""));
2013-11-03 23:53:49 +13:00
if (dir.Files.Count > 0)
{
sbContent.AppendLine(HtmlHelper.StartTag("ul"));
foreach (FileInfo fi in dir.Files)
{
2021-06-11 06:44:40 +12:00
sbContent.AppendLine(GetFileNameRow(fi));
2013-11-03 23:53:49 +13:00
}
sbContent.AppendLine(HtmlHelper.EndTag("ul"));
}
foreach (FolderInfo subdir in dir.Folders)
{
IndexFolder(subdir, level + 1);
}
sbContent.AppendLine(HtmlHelper.EndTag("div"));
}
2016-02-03 23:58:54 +13:00
private string GetFolderNameRow(FolderInfo dir, int level)
2013-11-03 23:53:49 +13:00
{
string folderNameRow = "";
2013-11-03 23:53:49 +13:00
if (!dir.IsEmpty)
{
2016-02-03 23:58:54 +13:00
if (settings.ShowSizeInfo)
{
2016-02-03 23:58:54 +13:00
folderNameRow += dir.Size.ToSizeString(settings.BinaryUnits) + " ";
}
folderNameRow += "(";
2013-11-03 23:53:49 +13:00
if (dir.TotalFileCount > 0)
{
folderNameRow += dir.TotalFileCount.ToString("n0") + " file" + (dir.TotalFileCount > 1 ? "s" : "");
2013-11-03 23:53:49 +13:00
}
if (dir.TotalFolderCount > 0)
{
if (dir.TotalFileCount > 0)
{
folderNameRow += ", ";
2013-11-03 23:53:49 +13:00
}
folderNameRow += dir.TotalFolderCount.ToString("n0") + " folder" + (dir.TotalFolderCount > 1 ? "s" : "");
2013-11-03 23:53:49 +13:00
}
folderNameRow += ")";
folderNameRow = " " + HtmlHelper.Tag("span", folderNameRow, "", "class=\"FolderInfo\"");
2013-11-03 23:53:49 +13:00
}
2021-06-10 10:14:01 +12:00
string pathTitle;
if (settings.DisplayPath)
{
pathTitle = settings.DisplayPathLimited ? dir.FolderPath.Substring(prePathTrim) : dir.FolderPath;
}
else
{
pathTitle = dir.FolderName;
}
2019-05-24 07:59:16 +12:00
int heading = (level + 1).Clamp(1, 6);
return HtmlHelper.StartTag("h" + heading) + URLHelpers.HtmlEncode(pathTitle) + folderNameRow + HtmlHelper.EndTag("h" + heading);
2013-11-03 23:53:49 +13:00
}
2021-06-11 06:44:40 +12:00
private string GetFileNameRow(FileInfo fi)
2013-11-03 23:53:49 +13:00
{
string fileNameRow = HtmlHelper.StartTag("li") + URLHelpers.HtmlEncode(fi.Name);
2016-02-03 23:58:54 +13:00
if (settings.ShowSizeInfo)
{
2016-02-03 23:58:54 +13:00
fileNameRow += " " + HtmlHelper.Tag("span", fi.Length.ToSizeString(settings.BinaryUnits), "", "class=\"FileSize\"");
}
fileNameRow += HtmlHelper.EndTag("li");
2013-11-03 23:53:49 +13:00
return fileNameRow;
2013-11-03 23:53:49 +13:00
}
2016-02-03 23:58:54 +13:00
private string GetFooter()
2013-11-03 23:53:49 +13:00
{
2022-05-15 09:32:09 +12:00
return $"Generated by <a href=\"{Links.Website}\">ShareX Directory Indexer</a> on {DateTime.UtcNow:yyyy-MM-dd 'at' HH:mm:ss 'UTC'}";
2013-11-03 23:53:49 +13:00
}
private string GetCssStyle()
{
string css;
2016-02-03 23:58:54 +13:00
if (settings.UseCustomCSSFile && !string.IsNullOrEmpty(settings.CustomCSSFilePath) && File.Exists(settings.CustomCSSFilePath))
{
2016-02-03 23:58:54 +13:00
css = File.ReadAllText(settings.CustomCSSFilePath, Encoding.UTF8);
}
else
{
css = Resources.IndexerDefault;
}
return $"<style type=\"text/css\">\r\n{css}\r\n</style>";
}
2013-11-03 23:53:49 +13:00
}
}