From 5c24d7687fa0e3a0ad3f35418ea87738e02e0a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 27 Mar 2009 01:10:03 +0100 Subject: [PATCH] refactor template handling/rendering. cleanup the index page and prepare for cross-linking --- lib/hanna.rb | 63 +++++++++++++++++----------- lib/hanna/template.rb | 15 +++++-- lib/hanna/template_files/index.haml | 46 +++++++++++--------- lib/hanna/template_files/styles.sass | 42 +++++++++---------- 4 files changed, 96 insertions(+), 70 deletions(-) diff --git a/lib/hanna.rb b/lib/hanna.rb index d5c34b6..5ed96ac 100644 --- a/lib/hanna.rb +++ b/lib/hanna.rb @@ -15,6 +15,14 @@ def self.for(options) new(options) end + def class_dir + 'classes' + end + + def file_dir + 'files' + end + # STYLE = read('styles.sass') # # CLASS_PAGE = read('page.haml') @@ -31,7 +39,6 @@ def self.for(options) def initialize(options) @options = options - template_files = File.join(File.dirname(__FILE__), 'hanna', 'template_files') @template_dir = Pathname.new(template_files).expand_path @base_dir = Pathname.pwd.expand_path @@ -45,44 +52,50 @@ def generate(top_levels) @methods = @classes.map { |m| m.method_list }.flatten.sort # @modsort = get_sorted_module_list( @classes ) - index = Template.new(@template_dir) - index.load_template 'layout.haml' - index.load_template 'index.haml' - - index.vars.page_title = @options.title - index.vars.page_encoding = @options.charset - - index.vars.files = @files - index.vars.methods = @methods - index.vars.classes = @classes - index.vars.main_page = find_main_page + template('index.haml', 'index.html') do |index| + index.vars.page_title = @options.title + index.vars.page_encoding = @options.charset - File.open(@output_dir + 'index.html', 'w') do |out| - index.write(out) + index.vars.files = @files + index.vars.methods = @methods + index.vars.classes = @classes + index.vars.main_page = find_main_page end - css = Template.new(@template_dir) - css.load_template 'styles.sass' - File.open(@output_dir + 'styles.css', 'w') do |stylesheet| - css.write(stylesheet) - end + template('styles.sass', 'styles.css').write! end + protected + def find_main_page - if @options.main_page + main = if @options.main_page @files.find { |f| f.full_name == @options.main_page } else @files.find { |f| f.name =~ /^README(\.|$)/i } end + + if main + def main.path # hack to fix relative linking when + http_url(nil) # this file is rendered in index.html + end + end + return main end - def class_dir - 'classes' - end + private - def file_dir - 'files' + def template(*names) + tm = Template.new(@template_dir, @output_dir) + tm.set_target names.pop + tm.load_template 'layout.haml' if names.first =~ /\.haml$/ + tm.load_template *names + if block_given? + yield tm + tm.write! + else + return tm + end end end diff --git a/lib/hanna/template.rb b/lib/hanna/template.rb index 4134f47..2db2d0d 100644 --- a/lib/hanna/template.rb +++ b/lib/hanna/template.rb @@ -12,8 +12,9 @@ class TemplateValues < OpenStruct end class Template - def initialize(base_dir) + def initialize(base_dir, output_dir) @base_dir = base_dir + @output_dir = output_dir @templates = [] end @@ -36,11 +37,17 @@ def load_template(*names) end end end + + def set_target(file) + @target = @output_dir + file + end - def write(io) - io.write render + def write! + File.open(@target, 'w') do |file| + file.write(render) + end rescue - $stderr.puts "error writing to #{io.inspect}" + $stderr.puts "error writing to #{@target}" raise end diff --git a/lib/hanna/template_files/index.haml b/lib/hanna/template_files/index.haml index 5361444..4c20bdd 100644 --- a/lib/hanna/template_files/index.haml +++ b/lib/hanna/template_files/index.haml @@ -4,24 +4,30 @@ #main.description ~ sanitize_code_blocks main_page.description.sub(%r{^\s*}, '') -%ol - - for klass in classes - %li - %h3= klass.full_name - = klass.path - %pre&= klass.description - %ol - - for section in klass.sections - %li - %pre&= section.description -%h2 Files -%ol#files - - for file in files - %li&= file.relative_name +.list_box + %h2 Files + - any_hidden = false + %ol#files + - for file in files + - hide = file.name =~ /\.(rb|c)$/ + - any_hidden = true if hide + %li{ :class => hide ? 'other' : nil }= link_to file.relative_name, file.path + - if any_hidden + %li + %a.show_hidden{ :href => '#', :onclick => 'this.parentNode.parentNode.className += " expanded"; this.parentNode.removeChild(this); return false' } show all -%h2 Methods -%ol#methods - - for method in methods - %li{ :class => method.visibility } - %span.name= method.name - %span.classmod== (#{method.parent.full_name}) +.list_box + %h2 Classes + %ol#classes + - for klass in classes + %li + %a{ :href => klass.path }= klass.full_name + +.list_box + %h2 Methods + %ol#methods + - for method in methods + %li{ :class => method.visibility } + %a{ :href => method.path } + %span.name= method.name + %span.classmod== (#{method.parent.full_name}) diff --git a/lib/hanna/template_files/styles.sass b/lib/hanna/template_files/styles.sass index 4c3a559..8a35aa5 100644 --- a/lib/hanna/template_files/styles.sass +++ b/lib/hanna/template_files/styles.sass @@ -53,7 +53,13 @@ body h1, h2, h3, h4, h5, h6 :margin 0 :font-weight normal - +h1 + :font-family = !title_font + :font-size 2.2em + /:margin-bottom .2em + :border-bottom 3px double #d8d8d8 + :padding .2em 1em .1em 1em + a :color = !link &:hover @@ -83,8 +89,17 @@ ol :white-space nowrap &.other :display none -ol.expanded li.other - :display list-item + a.show_hidden + :text-decoration underline + :font-style italic + :color = !light_text + &:after + :content " ..." + &:hover + :color black + :background #ffe + &.expanded li.other + :display list-item table :margin-bottom 1em @@ -99,23 +114,13 @@ table :color = !light_text tr :border-bottom 1px solid silver - -#index, #header - a.show - :text-decoration underline - :font-style italic - :color = !light_text - &:after - :content " ..." - &:hover - :color black - :background #ffe -#index +.list_box + :margin 2em :font 85%/1.2 Arial, Helvetica, sans-serif a :text-decoration none - h1 + h2 :padding .2em .5em .1em :background #ccc :font = "small-caps 1.2em" !title_font @@ -212,11 +217,6 @@ h1, h2, h3, h4, h5, h6 :margin-top .5em h1, h2, h3, h4, h5, h6 :font-family = !title_font - h1 - :font-size 2.2em - :margin-bottom .2em - :border-bottom 3px double #d8d8d8 - :padding-bottom .1em h2 :font-size 1.8em :color = !dark_blue_text