Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
refactor template handling/rendering. cleanup the index page and prep…
Browse files Browse the repository at this point in the history
…are for cross-linking
  • Loading branch information
mislav committed Mar 27, 2009
1 parent 267c476 commit 5c24d76
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 70 deletions.
63 changes: 38 additions & 25 deletions lib/hanna.rb
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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
15 changes: 11 additions & 4 deletions lib/hanna/template.rb
Expand Up @@ -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

Expand All @@ -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

Expand Down
46 changes: 26 additions & 20 deletions lib/hanna/template_files/index.haml
Expand Up @@ -4,24 +4,30 @@
#main.description
~ sanitize_code_blocks main_page.description.sub(%r{^\s*<h1.*?/h1>}, '')

%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})
42 changes: 21 additions & 21 deletions lib/hanna/template_files/styles.sass
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c24d76

Please sign in to comment.