今天分享下”前端页面目录生成代码“这篇文章,文中根据实例编码详细介绍,或许对大家的编程之路有着一定的参考空间与使用价值,需要的朋友接下来跟着云南仟龙Mark一起学习一下吧。
许多小伙伴都是为了自己的博闻建立文件目录,便捷大伙儿访问。我很好奇大伙儿是怎么做的,是否有全自动生成目录的专用工具能够强烈推荐一下(我明白word能够,可是立即贴word文本文档会转化成许多不必要的html tag)。
前几日写前面网页页面最佳实践文件目录项确实有些多,手动式加起來太麻烦了,我试着搜过下沒有寻找,因此写了两行编码来结束这工作中。拿出来共享给有一样必须的盆友。
工具代码
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace HtmlIndexGenerator
{
class Program
{
const string HeaderPattern = @"<h(?[1-6])[sS]?>[sS]?</h([1-6])>";
const string TagPattern = @"<[sS]?>";
const string IdPattern = “(id|name)=”(?[sS]?)"";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152const 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); } /// <summary> /// Prints help document. /// </summary> private static void PrintHelp() { Console.WriteLine("Usage: IndexGen.exe [filename] [-l] level"); Console.WriteLine("-l: header level limit, -l 3 limit the output to <h3>"); Console.WriteLine("Example: IndexGen.exe page.htm"); } /// <summary> /// Parses command line paramters. /// </summary> /// <param name="args">Input parameters</param> /// <param name="fileName">Output parameter for parsed file name. Null if parse failed.</param> /// <param name="limit">Output parameter for header level limit.</param> private static void ParseParameter(string[] args, out string fileName, out int limit) { fileName = null; limit = MaxHeaderLimit; for (int i = 0; i < args.Length; i++) { if (args[i].Equals("-l", StringComparison.InvariantCultureIgnoreCase)) { if (i + 1 >= 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]; } } /// <summary> /// Reads html content according to specified file name. /// </summary> /// <param name="fileName">File name</param> /// <returns>Html content of the specific file.</returns> 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(http://www.qlyl1688.com/products/yytzmq.html); 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; } /// <summary> /// Generates the index html. /// </summary> /// <param name="html">Html content of specified file.</param> /// <param name="limit">Header limit</param> /// <returns>Generated index html</returns> 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("<div >"); indexBuilder.Append("<ul>"); 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("<a href="#{0}">{1}</a>", id, header); if (currentLevel == previousLevel) { indexBuilder.AppendFormat("<li style="{1}">{0}</li>", link, HeaderStyles[currentLevel - 1]); } else if (currentLevel > previousLevel && currentLevel <= limit) { indexBuilder.AppendFormat("<ul><li style="{1}">{0}</li>", link, HeaderStyles[currentLevel - 1]); previousLevel = currentLevel; } else if (currentLevel < previousLevel) { indexBuilder.AppendFormat("</ul><li style="{1}">{0}</li>", link, HeaderStyles[currentLevel - 1]); previousLevel = currentLevel; } } indexBuilder.Append("</ul></div>"); return indexBuilder.ToString(); } }
}
使用方法
将程序编译成执行文件,把博文存成本地文件,注意要存成unicode或utf-8,通过命令行运行。一个名叫index.htm的文件会生成在相同目录下。
如果你只希望限制生成目录的级数,可以用 -l 参数指定,-l 3代表只生成
到
的目录。
下面必须做的是将生成的內容拷贝到博闻你想放目录的地区。简易的目录就生成了,参考文中目录。
假如你想变更款式,能够同时改动编码中对不一样的header的款式界定。
专用工具改善
这只不过是个实用工具,毫无疑问有很多让朋友们惊叹的不够,
最先不应该用正则表达式分析html,实际缘故能够看看吧,假如说真的要剖析html,.net强烈推荐应用htmlagilitypack,python强烈推荐应用beautifulsoup,我这里不愿再引入外界库,因此假定大家分析的html全是规范格式。
此外我没敲代码去生成文章标题的id属性,由于许多小伙伴期待id是令人难忘的姓名而了不起的header1、lable2这类的,因此id或是需要自身加上,要不然网页链接不出。
也试着把这一段代码转换成powershell脚本制作省了大伙儿编译程序,这里有详细介绍怎样做的方式 ,遗憾软件也是有缺陷,有一些英语的语法还不兼容,例如using, out 主要参数等。
今天的文章就分享到这啦,内容转自divcss5平台,下篇文章再见!
最后
以上就是稳重钢笔最近收集整理的关于前端页面目录生成代码 到的全部内容,更多相关前端页面目录生成代码内容请搜索靠谱客的其他文章。
发表评论 取消回复