Class: Output

Inherits:
Object
  • Object
show all
Defined in:
lib/output.rb

Instance Method Summary collapse

Constructor Details

#initialize(input_repo) ⇒ Output

Returns a new instance of Output.



3
4
5
6
7
# File 'lib/output.rb', line 3

def initialize input_repo
  @input_repo = input_repo

  @content_width = 600
end

Instance Method Details

#create(output_dir) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/output.rb', line 9

def create output_dir
  @output_dir = output_dir

  if !File.exists? output_dir
    Dir::mkdir output_dir
  end

  public_source_dir = File.expand_path("_view/public", @input_repo )
  if File.exists? public_source_dir
    public_dir = File.expand_path( "public", output_dir )
    if !File.exists? public_dir
      Dir::mkdir public_dir
    end

    cmd = "cp #{public_source_dir}/* #{public_dir}"
    system cmd
  end

  @input_path = Array.new
  @output_path = Array.new

  @root_page = Page.new
  @root_page.directory_path = @output_path
  @root_page.level = -1

  process_directory @input_repo, @root_page

  postprocess_page @root_page

  create_pages @root_page
end

#create_file(template_name, output_filename) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/output.rb', line 140

def create_file template_name, output_filename
  template = File.read File.expand_path(template_name, @input_repo )
  engine = Haml::Engine.new template

  File.open output_filename, "w" do |file|
    file.puts engine.render( binding )
  end
end

#create_page(page) ⇒ Object



134
135
136
137
138
# File 'lib/output.rb', line 134

def create_page page
  STDERR.puts "Create page: #{page.path} -> #{page.output_file}"
  @page = page
  create_file "_view/template.haml", page.output_file
end

#create_pages(parent_page) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/output.rb', line 117

def create_pages parent_page
  parent_page.children.each do |page|
    if page.content
      create_page page
    end
    create_pages page
  end
end

#css(name) ⇒ Object



149
150
151
152
# File 'lib/output.rb', line 149

def css name
  "<link rel='stylesheet' href='#{@page.relative_site_root}public/#{name}.css'" +
  " type='text/css'>"
end

#input_pathObject



126
127
128
# File 'lib/output.rb', line 126

def input_path
  @input_repo + "/" + @input_path.join( "/" )
end

#output_pathObject



130
131
132
# File 'lib/output.rb', line 130

def output_path
  @output_dir + "/" + @output_path.join( "/" )
end

#postprocess_page(parent_page) ⇒ Object



41
42
43
44
45
46
# File 'lib/output.rb', line 41

def postprocess_page parent_page
  parent_page.postprocess
  parent_page.children.each do |page|
    postprocess_page page
  end
end

#process_directory(dir, parent_page) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/output.rb', line 48

def process_directory dir, parent_page
  Dir.entries( dir ).sort.each do |entry|
    case entry
    when  /^\d\d\d_(.*)$/
      input_name = $1
      full_name = input_path + "/" + entry
      if File.directory?( full_name )
        @input_path.push entry
        @output_path.push input_name
        if !File.exists? output_path
          Dir.mkdir output_path
        end

        page = Page.new
        parent_page.add_child page
        page.directory_path = full_name

        process_directory full_name, page
        @input_path.pop
        @output_path.pop
      else
        file_basename = nil
        file_format = nil
        if input_name =~ /^(.*)\.(.*)$/
          file_basename = $1
          file_format = $2
          if file_format != "md"
            raise ParseError, 
              "Unsupported format '#{file_format}' in file #{input_name}."
          end
        else
          raise ParseError, 
            "Input file #{input_name} doesn't have an extension."
        end

        page = nil
        if file_basename == "index"
          page = parent_page
        else
          page = Page.new
          parent_page.add_child page
        end

        page.file_basename = file_basename
        page.path = input_path + "/" + entry
        page.content = File.read( input_path + "/" + entry )
        page.file_format = file_format

        output_file_name = file_basename + ".html"
        if ( @output_path.empty? )
          page.target = output_file_name
        else
          page.target = @output_path.join( "/" ) + "/" + output_file_name
        end
        page.output_file = output_path + "/" + output_file_name
      end
    when /.*\.png$/
      cmd = "cp #{input_path}/#{entry} #{output_path}/#{entry}"
      system cmd
      cmd = "mogrify -resize #{@content_width}x5000 #{output_path}/#{entry}"
      STDERR.puts "Resize picture: #{cmd}"
      system cmd
    when /.*\.xml$/
      cmd = "cp #{input_path}/#{entry} #{output_path}/#{entry}"
      system cmd
    end
  end
end

#render_contentObject



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/output.rb', line 158

def render_content
  @out = ""

  on "<div class=\"content page-#{@page.file_basename}\">"

  doc = Maruku.new @page.content
  on doc.to_html

  on "</div>"

  @out
end

#render_tocObject



171
172
173
174
175
176
177
178
# File 'lib/output.rb', line 171

def render_toc
  @out = ""

  on "<h1>Table of contents</h1>"
  render_toc_section @root_page

  @out
end

#render_toc_section(parent_page) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/output.rb', line 180

def render_toc_section parent_page
  on "<ul>"
  parent_page.children.each do |page|
    item_classes = []

    if parent_page != @root_page
      item_classes.push "sub-item"
    end

    title = page.title
    if page.has_children?
      item_classes.push "has-children"
      if page == @page
        item_classes.push "expanded"
      end
    end

    if item_classes.empty?
      o "<li>"
    else
      o "<li class=\"#{item_classes.join " "}\">"
    end

    if page == @page
      o "<span class=\"current-page\">#{title}</span>"
    else
      if page.title && !page.title.empty?
        o "<a href='#{@page.relative_site_root}#{page.target}'>#{title}</a>"
      end
    end
    on "</li>"
    if @page == page || ( page.has_children? && @page.has_parent( page ) )
      render_toc_section page
    end
  end
  on "</ul>"
end

#titleObject



154
155
156
# File 'lib/output.rb', line 154

def title
  @page.title
end