2020-09-03 09:33:09 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# frozen_string_literal: true
|
2018-04-20 15:27:59 +00:00
|
|
|
|
|
|
|
require 'sanitize'
|
2020-11-09 09:04:40 +00:00
|
|
|
require 'thor'
|
2018-04-20 15:27:59 +00:00
|
|
|
|
2020-09-03 09:33:09 +00:00
|
|
|
class Syllabus
|
|
|
|
def initialize
|
|
|
|
@entries = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(level, content)
|
|
|
|
# puts "Adding L#{level} - #{content}"
|
|
|
|
entry = @entries.last.clone || []
|
|
|
|
entry[level] = content
|
|
|
|
@entries.push entry
|
|
|
|
end
|
2018-04-20 15:27:59 +00:00
|
|
|
|
2020-09-03 09:33:09 +00:00
|
|
|
def display_entry(level, value, addspace=false)
|
|
|
|
case level
|
|
|
|
when 0 then puts "# #{value}\n\n"
|
|
|
|
when 1 then puts "## #{value}\n\n"
|
|
|
|
when 2 then puts "* #{value}\n"
|
|
|
|
else
|
|
|
|
puts ' ' * (level - 2) + value
|
|
|
|
end
|
|
|
|
puts '\n' if addspace
|
2018-04-20 15:27:59 +00:00
|
|
|
end
|
2020-09-03 09:33:09 +00:00
|
|
|
|
|
|
|
def display
|
|
|
|
old_entry = nil
|
|
|
|
old_level = 0
|
|
|
|
@entries.each do |entry|
|
|
|
|
entry.each.with_index do |val, level|
|
|
|
|
next if val.nil? # TODO: detect inconsistensy
|
|
|
|
next if !old_entry.nil? && old_entry[level] == val
|
|
|
|
|
|
|
|
display_entry(level, val, (old_level > level))
|
|
|
|
end
|
|
|
|
old_entry = entry
|
|
|
|
end
|
2018-04-20 15:27:59 +00:00
|
|
|
end
|
|
|
|
|
2020-09-03 09:33:09 +00:00
|
|
|
def syllabus_file(filename)
|
|
|
|
File.readlines(filename).each do |line|
|
|
|
|
next unless line =~ /^(#+)\s+(.*)\n/
|
|
|
|
|
|
|
|
level = ($1.size - 1)
|
|
|
|
content = Sanitize.clean($2)
|
|
|
|
add(level, content)
|
|
|
|
end
|
2018-04-20 15:27:59 +00:00
|
|
|
end
|
2020-09-03 09:33:09 +00:00
|
|
|
|
|
|
|
def syllabus_dir(dir)
|
2020-11-09 09:04:40 +00:00
|
|
|
Dir[dir + '/**/*.md'].sort.each do |name|
|
2020-09-03 09:33:09 +00:00
|
|
|
syllabus_file(name)
|
|
|
|
end
|
2018-04-20 15:27:59 +00:00
|
|
|
end
|
|
|
|
end
|
2020-09-03 09:33:09 +00:00
|
|
|
|
2020-11-09 09:04:40 +00:00
|
|
|
class SyllabusCli < Thor
|
|
|
|
desc 'extract DIR', 'Extract data from DIR'
|
|
|
|
def extract dir
|
|
|
|
s = Syllabus.new
|
|
|
|
s.syllabus_dir(dir)
|
|
|
|
s.display
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
SyllabusCli.start(ARGV)
|