|
1 |
| -require "zip" |
2 |
| - |
3 |
| -def wrap_and_flush(zipfile, dest, source) |
4 |
| - ignore_start = Regexp.new("(.*)// {{(.*)") |
5 |
| - ignore_end = "// }}" |
6 |
| - |
7 |
| - ignoring = false |
8 |
| - |
9 |
| - reader = File.new(source, "r") |
10 |
| - writer = zipfile.get_output_stream(dest) |
11 |
| - reader.readlines().each do |line| |
12 |
| - match = line.match(ignore_start) |
13 |
| - if not match.nil? |
14 |
| - writer.write(match[1] + match[2].strip + "\n") |
15 |
| - ignoring = true |
16 |
| - end |
17 |
| - |
18 |
| - if not ignoring |
19 |
| - writer.write(line) |
20 |
| - elsif not line.include?("{{") and not line.include?("}}") |
21 |
| - if line.strip.empty? or line.strip.start_with?("//") |
22 |
| - writer.write(line) |
23 |
| - end |
24 |
| - end |
25 |
| - |
26 |
| - if line.strip == ignore_end |
27 |
| - ignoring = false |
28 |
| - end |
29 |
| - end |
30 |
| - reader.close() |
31 |
| - writer.close() |
32 |
| - |
33 |
| - return |
34 |
| -end |
35 |
| - |
36 |
| -Jekyll::Hooks.register :site, :post_write do |site| |
37 |
| - site.data["archives"].each do |name, inputs| |
38 |
| - # Create archive directory |
39 |
| - destdir = File.join(site.dest, "archives") |
40 |
| - if not Dir.exists?(destdir) |
41 |
| - Dir.mkdir(destdir) |
42 |
| - end |
43 |
| - |
44 |
| - # Retrieve input files and exclusion patterns |
45 |
| - files = [] |
46 |
| - excludes = [] |
47 |
| - inputs.each do |input| |
48 |
| - if input.is_a?(String) |
49 |
| - files.append(input) |
50 |
| - elsif input.is_a?(Hash) |
51 |
| - if not input["exclude"].nil? |
52 |
| - input["exclude"].each do |exc| |
53 |
| - excludes.append(Regexp.new(exc)) |
54 |
| - end |
55 |
| - end |
56 |
| - end |
57 |
| - end |
58 |
| - |
59 |
| - tempfile = File.join(site.dest, "temp.txt") |
60 |
| - |
61 |
| - # Create ZIP archives |
62 |
| - Zip.continue_on_exists_proc = true |
63 |
| - Zip::File.open(File.join(destdir, name + ".zip"), Zip::File::CREATE) do |zipfile| |
64 |
| - files.each do |file| |
65 |
| - if File.file?(file) |
66 |
| - zipfile.add(File.basename(file), File.join(site.config["source"], file)) |
67 |
| - elsif File.directory?(file) |
68 |
| - Dir.glob(file + "/**/*.*").each do |subfile| |
69 |
| - is_include = true |
70 |
| - excludes.each do |exc| |
71 |
| - if not subfile.match(exc).nil? |
72 |
| - is_include = false |
73 |
| - break |
74 |
| - end |
75 |
| - end |
76 |
| - |
77 |
| - if is_include |
78 |
| - dest = subfile.sub(file + "/", "") |
79 |
| - wrap_and_flush(zipfile, dest, subfile) |
80 |
| - end |
81 |
| - end |
82 |
| - end |
83 |
| - end |
84 |
| - end |
85 |
| - end |
86 |
| -end |
| 1 | +require "zip" |
| 2 | + |
| 3 | +def wrap_and_flush(zipfile, dest, source) |
| 4 | + ignore_start = Regexp.new("(.*)// {{(.*)") |
| 5 | + ignore_end = "// }}" |
| 6 | + |
| 7 | + ignoring = false |
| 8 | + |
| 9 | + reader = File.new(source, "r") |
| 10 | + writer = zipfile.get_output_stream(dest) |
| 11 | + reader.readlines().each do |line| |
| 12 | + match = line.match(ignore_start) |
| 13 | + if not match.nil? |
| 14 | + writer.write(match[1] + match[2].strip + "\n") |
| 15 | + ignoring = true |
| 16 | + end |
| 17 | + |
| 18 | + if not ignoring |
| 19 | + writer.write(line) |
| 20 | + elsif not line.include?("{{") and not line.include?("}}") |
| 21 | + if line.strip.empty? or line.strip.start_with?("//") |
| 22 | + writer.write(line) |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + if line.strip == ignore_end |
| 27 | + ignoring = false |
| 28 | + end |
| 29 | + end |
| 30 | + reader.close() |
| 31 | + writer.close() |
| 32 | + |
| 33 | + return |
| 34 | +end |
| 35 | + |
| 36 | +Jekyll::Hooks.register :site, :post_write do |site| |
| 37 | + site.data["archives"].each do |name, inputs| |
| 38 | + # Create archive directory |
| 39 | + destdir = File.join(site.dest, "archives") |
| 40 | + if not Dir.exist?(destdir) |
| 41 | + Dir.mkdir(destdir) |
| 42 | + end |
| 43 | + |
| 44 | + # Retrieve input files and exclusion patterns |
| 45 | + files = [] |
| 46 | + excludes = [] |
| 47 | + inputs.each do |input| |
| 48 | + if input.is_a?(String) |
| 49 | + files.append(input) |
| 50 | + elsif input.is_a?(Hash) |
| 51 | + if not input["exclude"].nil? |
| 52 | + input["exclude"].each do |exc| |
| 53 | + excludes.append(Regexp.new(exc)) |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + tempfile = File.join(site.dest, "temp.txt") |
| 60 | + |
| 61 | + # Create ZIP archives |
| 62 | + Zip.continue_on_exists_proc = true |
| 63 | + Zip::File.open(File.join(destdir, name + ".zip"), Zip::File::CREATE) do |zipfile| |
| 64 | + files.each do |file| |
| 65 | + if File.file?(file) |
| 66 | + zipfile.add(File.basename(file), File.join(site.config["source"], file)) |
| 67 | + elsif File.directory?(file) |
| 68 | + Dir.glob(file + "/**/*.*").each do |subfile| |
| 69 | + is_include = true |
| 70 | + excludes.each do |exc| |
| 71 | + if not subfile.match(exc).nil? |
| 72 | + is_include = false |
| 73 | + break |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + if is_include |
| 78 | + dest = subfile.sub(file + "/", "") |
| 79 | + wrap_and_flush(zipfile, dest, subfile) |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments