about summary refs log tree commit diff
path: root/ny2html
blob: e23a08fa15139c63ee859f2d39a617d75e952907 (plain) (blame)
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
#!/usr/bin/env ruby

require 'date'
TODAY = Date.today

class Entry < Struct.new(:depth, :state, :desc, :children)
end

def parse(io, filename=nil)
  todos = [Entry.new(-1, "/", "", [])]

  while line = io.gets
    if line =~ /\A(?:\s*(?:#|\/\/)\s)?(\s*)([-xX?*])\s+(.*)/
      i, state, desc = $1.size, $2, $3
      while i <= todos.last.depth
        todos.pop
      end

      e = Entry.new(i, state, desc, [])
      todos.last.children << e
      todos << e
    end
  end

  todos.first
end

def render(e)
  puts "<dl><dt>"
  case e.state
  when "-"; puts "&#x2610;"
  when "x"; puts "&#x2611;"
  when "X"; puts "&#x2612;"
  when "?"; puts "&#xFF1F;"  # or 2370
  when "*"; puts "&#x229e;"
  end
  puts e.desc.sub(/(^\s*)\(([A-Z])\)/) { $1 + "<b>" + [$2.ord + 9333].pack("U") + "</b>" }.
         gsub(/(?<=\s)@\w+/, '<i>\&</i>')
  puts "</dt>"
  unless e.children.empty?
    puts "<dd>"
    e.children.each { |c|
      render(c)
    }
    puts "</dd>"
  end
  puts "</dl>"
end

puts <<EOF
<!doctype html>
<meta charset=utf-8>
EOF
render(parse(STDIN))