docmachine-containers/scripts/build-syllabus

70 lines
1.4 KiB
Ruby
Executable file

#!/usr/bin/env ruby
# frozen_string_literal: true
require 'sanitize'
require 'thor'
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
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
end
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
end
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
end
def syllabus_dir(dir)
Dir[dir + '/**/*.md'].sort.each do |name|
syllabus_file(name)
end
end
end
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)