首页前端开发HTMLHTML目录生成工具基础介绍

HTML目录生成工具基础介绍

时间2024-05-09 03:22:03发布访客分类HTML浏览54
导读:园子里面很多博主都会为自己的博文创建目录,方便大家浏览。我很好奇大家是怎么做的,是不是有自动生成目录的工具可以推荐一下(我知道word可以,但是直接贴word文档会生成很多多余的html tag 。 前几天写前端网页最佳实践目录项实在有点...
园子里面很多博主都会为自己的博文创建目录,方便大家浏览。我很好奇大家是怎么做的,是不是有自动生成目录的工具可以推荐一下(我知道word可以,但是直接贴word文档会生成很多多余的html tag)。 前几天写前端网页最佳实践目录项实在有点多,手动加起来太麻烦了,我尝试搜了下没有找到,于是写了几行代码来完成这工作。拿出来分享给有同样需要的朋友。 工具代码 using System; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace HtmlIndexGenerator { class Program { const string HeaderPattern = @"[sS]*?"; const string TagPattern = @""; const string IdPattern = "(id|name)="(?[\s\S]*?)""; const int MaxHeaderLimit = 6; const string H1Style = @"font-weight:bold"; const string H2Style = @""; const string H3Style = @""; const string H4Style = @""; const string H5Style = @""; const string H6Style = @"font-size:10px; "; static string[] HeaderStyles = new string[]{ H1Style, H2Style, H3Style, H4Style, H5Style, H6Style } ; static void Main(string[] args) { string fileName; int limit; ParseParameter(args, out fileName, out limit); string html = GetHtml(fileName); if (string.IsNullOrEmpty(html)) return; string index = GenerateIndex(html, limit); string outputFile = "index.htm"; File.WriteAllText(outputFile, index, Encoding.UTF8); Console.WriteLine("{ 0} generated.", outputFile); } /// /// Prints help document. /// private static void PrintHelp() { Console.WriteLine("Usage: IndexGen.exe [filename] [-l] level"); Console.WriteLine("-l: header level limit, -l 3 limit the output to

"); Console.WriteLine("Example: IndexGen.exe page.htm"); } /// /// Parses command line paramters. /// /// Input parameters /// Output parameter for parsed file name. Null if parse failed. /// Output parameter for header level limit. private static void ParseParameter(string[] args, out string fileName, out int limit) { fileName = null; limit = MaxHeaderLimit; for (int i = 0; i = args.Length || !int.TryParse(args[i + 1], out limit)) { Console.WriteLine("Invalid parameter for -l"); PrintHelp(); return; } } } if (args.Length > 0) { fileName = args[args.Length - 1]; } } /// /// Reads html content according to specified file name. /// /// File name /// Html content of the specific file. private static string GetHtml(string fileName) { string html = null; if (string.IsNullOrEmpty(fileName)) { Console.WriteLine("Specify a file name"); PrintHelp(); return html; } if (!File.Exists(fileName)) { Console.WriteLine("File { 0} dose not exist", fileName); PrintHelp(); return html; } // Auto defect file encoding. using (StreamReader reader = new StreamReader(fileName, detectEncodingFromByteOrderMarks: true)) { Encoding encoding = reader.CurrentEncoding; html = File.ReadAllText(fileName, encoding); } return html; } /// /// Generates the index html. /// /// Html content of specified file. /// Header limit /// Generated index html private static string GenerateIndex(string html, int limit) { Regex regex = new Regex(HeaderPattern, RegexOptions.IgnoreCase); Regex regexId = new Regex(IdPattern, RegexOptions.IgnoreCase); MatchCollection headerMatches = regex.Matches(html); int previousLevel = 1; StringBuilder indexBuilder = new StringBuilder(); indexBuilder.Append("
"); indexBuilder.Append("
    "); foreach (Match headerMatch in headerMatches) { int currentLevel = int.Parse(headerMatch.Groups["level"].Value); string header = Regex.Replace(headerMatch.Value, TagPattern, string.Empty); Match idMatch = regexId.Match(headerMatch.Value); string id = idMatch.Success ? idMatch.Groups["id"].Value : null; string link = string.IsNullOrEmpty(id) ? header : string.Format("{ 1} ", id, header); if (currentLevel == previousLevel) { indexBuilder.AppendFormat("
  • { 0}
  • ", link, HeaderStyles[currentLevel - 1]); } else if (currentLevel > previousLevel & & currentLevel

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: HTML目录生成工具基础介绍
本文地址: https://pptw.com/jishu/655954.html
html-webpack-plugin详解 JavaScript与HTML交互——事件

游客 回复需填写必要信息