lib/bookingit/config.rb in bookingit-0.0.1 vs lib/bookingit/config.rb in bookingit-0.1.0

- old
+ new

@@ -5,52 +5,86 @@ class Config include FileUtils attr_reader :front_matter, :main_matter, - :back_matter + :back_matter, + :rendering_config, + :cache, + :options, + :templates def initialize(config_json,root_dir) config_hash = JSON.parse(config_json) - @front_matter = Matter.new(config_hash['front_matter'],root_dir) - @main_matter = Matter.new(config_hash['main_matter'],root_dir) - @back_matter = Matter.new(config_hash['back_matter'],root_dir) + @front_matter = Matter.new(config_hash.delete('front_matter'),root_dir) + @main_matter = Matter.new(config_hash.delete('main_matter'),root_dir) + @back_matter = Matter.new(config_hash.delete('back_matter'),root_dir) + @templates = config_hash.delete("templates") || {} + @templates["index"] ||= "index.html" + @rendering_config = create_rendering_config(config_hash.delete('rendering')) + @cache = false + @options = config_hash + + all_chapters = (@front_matter.chapters + @main_matter.chapters + @back_matter.chapters) + all_chapters.each_with_index do |chapter,i| + if i > 0 + all_chapters[i-1].next_chapter = chapter + chapter.previous_chapter = all_chapters[i-1] + end + if i < (all_chapters.size-1) + all_chapters[i+1].previous_chapter = chapter + chapter.next_chapter = all_chapters[i+1] + end + end end + def cache=(cache) + @cache = cache + end + + private + + def create_rendering_config(raw_config) + raw_config ||= {} + rendering_config = {} + rendering_config[:stylesheets] = Array(raw_config['stylesheets']) + rendering_config[:basedir] = raw_config['git_repos_basedir'] + rendering_config[:languages] = Hash[(raw_config['languages'] || {}).map { |match,language| + if match =~ /^\/(.+)\/$/ + [Regexp.new($1),language] + else + [match,language] + end + }] + rendering_config[:theme] = raw_config['syntax_theme'] + + rendering_config + end + class Matter attr_reader :chapters - def initialize(chapters_config,root_dir) - @chapters = Array(chapters_config).map { |chapter_config| - Chapter.new(chapter_config,root_dir) + def initialize(chapter_filenames,root_dir) + @chapters = Array(chapter_filenames).map { |chapter_filename| + Chapter.new(markdown_path: File.join(root_dir,chapter_filename)) } end end class Chapter - attr_reader :sections - attr_reader :path + attr_reader :markdown_path, :relative_url, :sections + attr_accessor :title, :previous_chapter, :next_chapter - def initialize(chapter_config,root_dir) - files = Array(chapter_config).flatten(1).map { |file| - Dir[File.join(root_dir,file)] - }.flatten - if files.size == 1 - @sections = [] - @path = files[0] - else - @sections = files.map { |file| - Section.new(file) - } - end + def initialize(markdown_path: nil, relative_url: nil) + @markdown_path = markdown_path + @relative_url = relative_url || (File.basename(markdown_path, File.extname(markdown_path)) + ".html") + @sections = [] end - end - class Section - attr_reader :path - def initialize(path) - @path = path + def add_section(title,anchor) + section = Chapter.new(relative_url: self.relative_url + "##{anchor}") + section.title = title + @sections << section end end end - end