|
| 1 | +def escape_htmlspecialchars(content) |
| 2 | + # see: http://php.net/htmlspecialchars |
| 3 | + replaces = { |
| 4 | + "&" => "&", |
| 5 | + "\"" => """, |
| 6 | + "'" => "'", |
| 7 | + "<" => "<", |
| 8 | + ">" => ">" |
| 9 | + } |
| 10 | + replaces.each { |key, value| content.gsub!(key, value) } |
| 11 | + content |
| 12 | +end |
| 13 | + |
| 14 | +def anchor_footerlinks(footer) |
| 15 | + footer.gsub!(/^(\[\d+\]:) (.*)/, '\1 <a href="\2">\2</a>') |
| 16 | +end |
| 17 | + |
| 18 | +def main |
| 19 | + # First, make sure that the required files are present |
| 20 | + all_files = (Dir.entries(".") - [".", "..", "design", ".git"]).reject { |file| |
| 21 | + /\.html$/ =~ file |
| 22 | + } |
| 23 | + if not all_files.include? "index" |
| 24 | + puts "error: index file not found; aborting" |
| 25 | + exit 1 |
| 26 | + end |
| 27 | + |
| 28 | + ["template.index.html", "template.html"].each { |file| |
| 29 | + if not Dir.entries("design").include? file |
| 30 | + puts "error: design/#{file} file not found; aborting" |
| 31 | + exit 1 |
| 32 | + end |
| 33 | + } |
| 34 | + |
| 35 | + # Next, look for stray files |
| 36 | + index_files = ["index"] + all_files.select { |file| /\.index/ =~ file } |
| 37 | + topics = index_files.map { |file| file.split(".index")[0] } |
| 38 | + content_files = all_files - index_files |
| 39 | + (content_files.reject { |file| topics.include? (file.split(":", 2)[0]) }) |
| 40 | + .each { |stray_file| |
| 41 | + content_files = content_files - [stray_file] |
| 42 | + puts "warning: #{stray_file} is a stray file; ignored" |
| 43 | + } |
| 44 | + |
| 45 | + # Generate all the pages |
| 46 | + (index_files + content_files).each { |file| |
| 47 | + if file == "index" |
| 48 | + target = "index.html" |
| 49 | + permalink = file |
| 50 | + template = IO.read("design/template.index.html") |
| 51 | + elsif index_files.include? file |
| 52 | + target = file.sub(".index", ".html") |
| 53 | + permalink = file.split(".index")[0] |
| 54 | + template = IO.read("design/template.index.html") |
| 55 | + else |
| 56 | + target = "#{file.split(':', 2)[1]}.html" |
| 57 | + permalink = file.split(":", 2)[0] |
| 58 | + template = IO.read("design/template.html") |
| 59 | + end |
| 60 | + content = escape_htmlspecialchars(IO.read file) |
| 61 | + title, rest = content.split("\n\n", 2) |
| 62 | + begin |
| 63 | + # Optional footer |
| 64 | + body, partial_footer = rest.split("\n\n[1]: ", 2) |
| 65 | + footer = "\n\n[1]: #{partial_footer}" |
| 66 | + rescue |
| 67 | + end |
| 68 | + anchor_footerlinks footer if footer |
| 69 | + ["permalink", "title", "body", "footer"].each { |template_var| |
| 70 | + template.gsub!("\{% #{template_var} %\}", eval(template_var)) |
| 71 | + } |
| 72 | + File.open(target, mode="w") { |targetio| |
| 73 | + nbytes = targetio.write(template) |
| 74 | + puts "[GEN] #{target} (#{nbytes} bytes out)" |
| 75 | + } |
| 76 | + } |
| 77 | +end |
| 78 | + |
| 79 | +main |
0 commit comments