Class: SemiStatic::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/semi-static/site.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_dir) ⇒ Site

Initializes a new Site with the given source directory.



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/semi-static/site.rb', line 77

def initialize(source_dir)
    @clean_first = false
    @first_pass = true
    @quick_mode = false
    @check_mtime = false
    @show_statistics = false
    @source_dir = source_dir
    @time = Time.now
    @stats = Statistics.new
    stats.record(:site, :load) { load }
end

Instance Attribute Details

#check_mtimeObject

Only generate a file if its source files are newer than its output file.



9
10
11
# File 'lib/semi-static/site.rb', line 9

def check_mtime
  @check_mtime
end

#clean_firstObject

Clean the output directory before generating the site.



5
6
7
# File 'lib/semi-static/site.rb', line 5

def clean_first
  @clean_first
end

#day_indexObject (readonly)

Template used to generate the day indices in the output site.



53
54
55
# File 'lib/semi-static/site.rb', line 53

def day_index
  @day_index
end

#layoutsObject (readonly)

List of every Layout in the site.



21
22
23
# File 'lib/semi-static/site.rb', line 21

def layouts
  @layouts
end

#metadataObject (readonly)

Site configuration.



61
62
63
# File 'lib/semi-static/site.rb', line 61

def 
  
end

#month_indexObject (readonly)

Template used to generate the month indices in the output site.



49
50
51
# File 'lib/semi-static/site.rb', line 49

def month_index
  @month_index
end

#pagesObject (readonly)

List of every Page in the site.



25
26
27
# File 'lib/semi-static/site.rb', line 25

def pages
  @pages
end

#postsObject (readonly)

List of every Post in the site.



29
30
31
# File 'lib/semi-static/site.rb', line 29

def posts
  @posts
end

#quick_modeObject

Only process a few posts, to speed up template editing.



13
14
15
# File 'lib/semi-static/site.rb', line 13

def quick_mode
  @quick_mode
end

#show_statisticsObject

Show conversion stats after outputting the site.



17
18
19
# File 'lib/semi-static/site.rb', line 17

def show_statistics
  @show_statistics
end

#snippetsObject (readonly)

List of every Snippet in the site.



33
34
35
# File 'lib/semi-static/site.rb', line 33

def snippets
  @snippets
end

#source_dirObject (readonly)

Site source directory.



69
70
71
# File 'lib/semi-static/site.rb', line 69

def source_dir
  @source_dir
end

#statsObject (readonly)

Statistics for the conversion process.



65
66
67
# File 'lib/semi-static/site.rb', line 65

def stats
  @stats
end

#stylesheetsObject (readonly)

List of every Stylesheet in the site.



37
38
39
# File 'lib/semi-static/site.rb', line 37

def stylesheets
  @stylesheets
end

#tag_indexObject (readonly)

Template used to generate the tag indices in the output site.



57
58
59
# File 'lib/semi-static/site.rb', line 57

def tag_index
  @tag_index
end

#tagsObject (readonly)

List of all Tags in the site.



41
42
43
# File 'lib/semi-static/site.rb', line 41

def tags
  @tags
end

#timeObject (readonly)

Time the output was generated.



73
74
75
# File 'lib/semi-static/site.rb', line 73

def time
  @time
end

#year_indexObject (readonly)

Template used to generate the year indices in the output site.



45
46
47
# File 'lib/semi-static/site.rb', line 45

def year_index
  @year_index
end

Class Method Details

.open(source_dir) {|site| ... } ⇒ Object

Creates a Site with the given source directory and yields it to the given block.

Yields:

  • (site)

Raises:

  • (ArugmentError)


92
93
94
95
96
# File 'lib/semi-static/site.rb', line 92

def self.open(source_dir)
    raise ArugmentError, "block required" unless block_given?
    site = SemiStatic::Site.new source_dir
    yield site
end

Instance Method Details

#output(path) ⇒ Object

Write the Site to the given output directory.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/semi-static/site.rb', line 100

def output(path)
    if @first_pass
        @first_pass = false
        FileUtils.rm_rf path if clean_first
    end
    FileUtils.mkdir_p path
    
    if quick_mode
        posts.chop! 20
    end
    @stats.reset
    
    unless .nil? || !['static'].is_a?(Array)
        stats.record(:site, :static) do
            for dir in ['static']
                FileUtils.cp_r File.join(source_dir, dir), File.join(path, dir)
            end
        end
    end
    
    before = Time.now
    Dir.chdir(path) do
        stats.record(:site, :pages) do
            pages.each do |name, page|
                FileUtils.mkdir_p page.output_dir unless File.directory?(page.output_dir)
                if check_mtime
                    if File.file?(page.output_path) && File.mtime(page.output_path) > page.source_mtime
                        next
                    end
                    page.load
                end
                File.open(page.output_path, 'w') { |f| f.write page.render }
            end
        end
        
        stats.record(:site, :posts) do
            posts.each do |post|
                FileUtils.mkdir_p post.output_dir unless File.directory?(post.output_dir)
                if check_mtime
                    if File.file?(post.output_path) && File.mtime(post.output_path) > post.source_mtime
                        next
                    end
                    post.load
                end
                File.open(post.output_path, 'w') { |f| f.write post.render }
            end
        end
        
        stats.record(:site, :stylesheets) do
            unless stylesheets.nil?
                stylesheets.each do |name, stylesheet|
                    FileUtils.mkdir_p stylesheet.output_dir unless File.directory?(stylesheet.output_dir)
                    if check_mtime
                        if File.file?(stylesheet.output_path) && File.mtime(stylesheet.output_path) > stylesheet.source_mtime
                            next
                        end
                        stylesheet.load
                    end
                    File.open(stylesheet.output_path, 'w') { |f| f.write stylesheet.render }
                end
            end
        end
        
        stats.record(:site, :indices) do
            unless year_index.nil? && month_index.nil? && day_index.nil?
                posts.each_index do |dir|
                    posts = self.posts.from(dir)
                    Dir.chdir(dir) do
                        context = dir.split('/').collect { |c| c.to_i }
                        date_index = case context.length
                        when 1
                            year_index
                        when 2
                            month_index
                        when 3
                            day_index
                        else
                            nil
                        end
                        date_index.posts = posts
                        date_index.context = Time.local *context
                        File.open('index.html', 'w') { |f| f.write date_index.render }
                    end
                end
            end
            
            unless tag_index.nil?
                tags.each do |tag|
                    tag_index.context = tag.name
                    tag_index.posts = tag
                    FileUtils.mkdir_p tag.output_dir
                    File.open(tag.output_path, 'w') { |f| f.write tag_index.render }
                end
            end
        end
    end
    
    self.stats.display if show_statistics
end