|
3 | 3 | ## By Alison Sanderson. Attribution is encouraged, though not required.
|
4 | 4 | ## <https://creativecommons.org/publicdomain/zero/1.0/legalcode>
|
5 | 5 | ## TOCGen: Generates inter-file TOCs.
|
| 6 | +## Example usage: |
| 7 | +## tools/tocgen.rb |
6 | 8 |
|
7 |
| -$d = Dir["*.md"] |
| 9 | +def emit_filtered_file_toc file_matcher |
| 10 | + links = [] |
| 11 | + files = ALL_MD_FILES.map do |p| p.match file_matcher end.compact |
8 | 12 |
|
9 |
| -def filter_emit r |
10 |
| - res = [] |
11 |
| - for m in $d.map{|p| p.match r}.compact |
12 |
| - res << "* [#{m[1]}](#{m[0]})" |
13 |
| - end |
14 |
| - res.join ?\n |
| 13 | + for md_file in files |
| 14 | + links << "* [#{md_file[1]}](#{md_file[0]})" |
| 15 | + end |
| 16 | + |
| 17 | + links.sort.join ?\n |
15 | 18 | end
|
16 | 19 |
|
17 |
| -def find_toc_areas f |
18 |
| - re = /^(<!-- inter-toc\s*([^\s]+)\s*-->)(?:.|\n)*?(<!-- end -->)/i |
19 |
| - f.to_enum(:scan, re).map{$~} |
| 20 | +TOC_REGEX = /^(<!-- inter-toc\s*([^\s]+)?\s*-->)(?:.|\n)*?(<!-- end -->)/i |
| 21 | +def find_inter_toc_areas file |
| 22 | + file.to_enum(:scan, TOC_REGEX).map do $~ end |
20 | 23 | end
|
21 | 24 |
|
22 |
| -def filter_toc_areas f |
23 |
| - o = String.new |
24 |
| - lof = 0 |
25 |
| - for area in find_toc_areas f |
26 |
| - of = area.offset 0 |
27 |
| - o << f[lof...of[0]] |
28 |
| - o << area[1] |
29 |
| - o << "\n\n" |
30 |
| - o << filter_emit(yield area[2]) |
31 |
| - o << "\n\n" |
32 |
| - o << area[3] |
33 |
| - lof = of[1] |
34 |
| - end |
35 |
| - o << f[lof..-1] |
36 |
| - o |
| 25 | +def filter_toc_areas file |
| 26 | + content = "" |
| 27 | + last_offset = 0 |
| 28 | + |
| 29 | + for area in find_inter_toc_areas file |
| 30 | + offset = area.offset 0 |
| 31 | + content << file[last_offset...offset[0]] |
| 32 | + content << area[1] |
| 33 | + content << "\n\n" |
| 34 | + content << emit_filtered_file_toc(yield area[2]) |
| 35 | + content << "\n\n" |
| 36 | + content << area[3] |
| 37 | + last_offset = offset[1] |
| 38 | + end |
| 39 | + |
| 40 | + content << file[last_offset..-1] |
| 41 | + content |
37 | 42 | end
|
38 | 43 |
|
39 |
| -def rewrite fnam |
40 |
| - f = File.read fnam |
41 |
| - o = yield f |
42 |
| - File.write fnam, o |
| 44 | +def rewrite filename |
| 45 | + file = File.read filename |
| 46 | + output = yield file |
| 47 | + File.write filename, output |
43 | 48 | end
|
44 | 49 |
|
45 |
| -rewrite "api.md" do |f| |
46 |
| - filter_toc_areas f do |a| /api-#{a}-(\w+).md/ end |
| 50 | +ALL_MD_FILES = Dir["{api,glossary,language}/**/*.md"] |
| 51 | + |
| 52 | +rewrite "api.md" do |file| |
| 53 | + filter_toc_areas file do |folder| /api\/#{folder}\/(\w+).md/ end |
47 | 54 | end
|
48 | 55 |
|
49 |
| -rewrite "glossary.md" do |f| |
50 |
| - filter_toc_areas f do |a| /glossary-(\w+).md/ end |
| 56 | +rewrite "glossary.md" do |file| |
| 57 | + filter_toc_areas file do |_| /glossary\/(\w+).md/ end |
51 | 58 | end
|
52 | 59 |
|
53 |
| -rewrite "lang.md" do |f| |
54 |
| - filter_toc_areas f do |a| /lang-(\w+).md/ end |
| 60 | +rewrite "language.md" do |file| |
| 61 | + filter_toc_areas file do |_| /language\/(\w+).md/ end |
55 | 62 | end
|
56 | 63 |
|
57 | 64 | ## EOF
|
0 commit comments