Class: Smithy::Description

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Description

Returns a new instance of Description.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/smithy/description.rb', line 43

def initialize(args = {})
  @www_root = Smithy::Config.web_root
  @package = args[:package]
  @global_description = Smithy::Config.descriptions_root if Smithy::Config.descriptions_root

  if @package.class == Package
    @root = @package.root
    @arch = @package.arch
    @name = @package.name
    @path = args[:package].application_directory
    @path = File.join(Smithy::Config.descriptions_root, @name) if @global_description
  else
    @root = Smithy::Config.root
    @arch = Smithy::Config.arch

    if @global_description
      @name = @package.gsub(/#{Smithy::Config.descriptions_root}\/?/, "")
      @path = File.join(Smithy::Config.descriptions_root, @name) if @global_description
    else
      if @package == 'last'
        @name = last_prefix.split('/').try(:first)
      else
        @name = Package.normalize_name(args[:package])
      end
      @path = File.join @root, @arch, @name
    end
  end
  @categories = []
end

Instance Attribute Details

#archObject

Returns the value of attribute arch.



40
41
42
# File 'lib/smithy/description.rb', line 40

def arch
  @arch
end

#buildsObject

Returns the value of attribute builds.



40
41
42
# File 'lib/smithy/description.rb', line 40

def builds
  @builds
end

#categoriesObject

Returns the value of attribute categories.



40
41
42
# File 'lib/smithy/description.rb', line 40

def categories
  @categories
end

#contentObject

Returns the value of attribute content.



40
41
42
# File 'lib/smithy/description.rb', line 40

def content
  @content
end

#global_descriptionObject

Returns the value of attribute global_description.



40
41
42
# File 'lib/smithy/description.rb', line 40

def global_description
  @global_description
end

#nameObject

Returns the value of attribute name.



40
41
42
# File 'lib/smithy/description.rb', line 40

def name
  @name
end

#packageObject

Returns the value of attribute package.



40
41
42
# File 'lib/smithy/description.rb', line 40

def package
  @package
end

#pathObject

Returns the value of attribute path.



40
41
42
# File 'lib/smithy/description.rb', line 40

def path
  @path
end

#rootObject

Returns the value of attribute root.



40
41
42
# File 'lib/smithy/description.rb', line 40

def root
  @root
end

#versionsObject

Returns the value of attribute versions.



40
41
42
# File 'lib/smithy/description.rb', line 40

def versions
  @versions
end

#www_rootObject

Returns the value of attribute www_root.



40
41
42
# File 'lib/smithy/description.rb', line 40

def www_root
  @www_root
end

Class Method Details

.publishable?(path) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
# File 'lib/smithy/description.rb', line 86

def self.publishable?(path)
  exceptions_file = File.join(path, Package::PackageFileNames[:exception])
  description_file = File.join(path, "description")
  publishable = true
  if File.exists?(exceptions_file) && ( File.exists?(description_file) || File.exists?(description_file+".markdown") )
    File.open(exceptions_file).readlines.each do |line|
      publishable = false if line =~ /^\s*noweb\s*$/
    end
  end
  return publishable
end

.update_page(file = 'alphabetical', args = {}) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/smithy/description.rb', line 225

def self.update_page(file = 'alphabetical', args = {})
  root = Smithy::Config.root
  arch = Smithy::Config.arch
  if Smithy::Config.descriptions_root
    www_arch = File.join(Smithy::Config.web_root, "all")
  else
    www_arch = File.join(Smithy::Config.web_root, arch)
  end

  unless args[:descriptions].nil?
    @descriptions = args[:descriptions]
    @descriptions.sort! {|x,y| x.name <=> y.name}

    #tags.each do |tag|
      #t = tag.gsub(/^ *| *$|"/, '').downcase
      #@packages[@last_package][:tags] << t
      #@tags[t] = [] unless @tags.has_key?(t)
      #@tags[t] << name
    #end
    #@max_tag_count = 0
    #@min_tag_count = 1000000
    #@tags.each do |tag|
      #@max_tag_count = tag[1].size if tag[1].size > @max_tag_count
      #@min_tag_count = tag[1].size if tag[1].size < @min_tag_count
    #end
  end

  @packages = Package.all_web :root => File.join(root,arch)
  @packages.collect! do |p|
    if Smithy::Config.descriptions_root
      File.basename(p)
    else
      Package.normalize_name(p)
    end
  end
  @packages.sort!

  erb_file = File.join(@@smithy_bin_root, "/etc/templates/web/#{file}.html.erb")
  output = File.join(www_arch, "/#{file}.html")

  erb = ERB.new(File.read(erb_file), nil, "<>")
  unless args[:dry_run]
    File.open(output, "w+") do |f|
      f.write erb.result(binding)
    end
  end

  puts "updated ".rjust(12).bright + output
end

Instance Method Details

#add_system_info!Object



127
128
129
130
131
132
# File 'lib/smithy/description.rb', line 127

def add_system_info!
  system_string = @machine_table.keys.reject{|m| @machine_table[m].empty?}.collect{|m| m.humanize}.sort.join(', ')
  @content.sub!(/(<h\d>.*?<\/h\d>\n)/) do
    "#{$&}\n<p>Systems: #{system_string}</p>\n"
  end
end

#deploy(args = {}) ⇒ Object



178
179
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
217
218
219
220
221
222
223
# File 'lib/smithy/description.rb', line 178

def deploy(args = {})
  options = {:verbose => false, :noop => false}
  options = {:verbose => true, :noop => true} if args[:dry_run]

  www_arch = File.join(www_root, "/#{arch.downcase}")
  www_arch = File.join(www_root, "all") if global_description

  FileUtils.mkdir_p www_root, options                  unless Dir.exists? www_root
  raise "Cannot access web-root directory #{www_root}" unless Dir.exists? www_root
  FileUtils.mkdir_p www_arch, options                  unless Dir.exists? www_arch
  raise "Cannot create web-root directory #{www_arch}" unless Dir.exists? www_arch

  description_file = File.join(path, "description.markdown")

  begin
    if File.exist? description_file
      f = File.open description_file
      d = Kramdown::Document.new(f.read, :auto_ids => false)
      @content = d.to_html
      remove_ptag_linebreaks!
    else
      description_file = File.join(path, "description")
      f = File.open description_file
      @content = f.read
    end
  rescue => exception
    raise "#{exception}\nCannot read #{description_file}"
  end

  @content += render_version_table
  sanitize_content!
  parse_categories
  add_system_info! if global_description

  description_output  = File.join(www_arch, "/#{name.downcase}.html")
  unless args[:dry_run]
    d = File.open(description_output, "w+")
    d.write(@content)
    d.close
  end
  puts "updated ".rjust(12).bright + description_output

  #TODO update category list

  #notice_success "SUCCESS #{path} published to web"
end

#description_file_pathObject



73
74
75
# File 'lib/smithy/description.rb', line 73

def description_file_path
  File.join(@path, "description.markdown")
end

#exceptions_fileObject



82
83
84
# File 'lib/smithy/description.rb', line 82

def exceptions_file
  File.join(@path, Package::PackageFileNames[:exception])
end

#parse_categoriesObject



134
135
136
137
138
139
140
141
142
# File 'lib/smithy/description.rb', line 134

def parse_categories
  if @content =~ /Categor(y|ies):\s+(.*?)(<\/p>)$/i
    @categories = $2.split(',')
    @categories.map do |t|
      t.downcase!
      t.strip!
    end
  end
end

#publishable?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/smithy/description.rb', line 98

def publishable?
  Description.publishable?(path)
end

#remove_ptag_linebreaks!Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/smithy/description.rb', line 102

def remove_ptag_linebreaks!
  # Find paragraph tag contents
  results = []
  @content.scan(/<p>(.*?)<\/p>/m) {|m| results << [m.first, Regexp.last_match.offset(0)[0]] }
  newlines = []
  # For each paragraph
  results.each do |string, index|
    # Find newlines and save their index
    # index + 3 to accomodate '<p>'
    string.scan(/\n/) {|m| newlines << index+3+Regexp.last_match.offset(0)[0] }
  end
  # Replace the newlines with spaces
  newlines.each {|i| @content[i] = ' '}
end

#render_version_tableObject



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
# File 'lib/smithy/description.rb', line 144

def render_version_table
  if Smithy::Config.descriptions_root
    @machine_table = {}
    web_arches = Smithy::Config.web_architecture_names.keys
    web_arches.each do |a|
      architecture_path = File.join( Smithy::Config.root, a, @name )
      next unless Dir.exists? architecture_path
      web_machine_name = Smithy::Config.web_architecture_names[a]
      @machine_table[web_machine_name] = {}
      Package.alternate_versions(architecture_path).each do |v|
        @machine_table[web_machine_name][v] = Package.alternate_builds(File.join(architecture_path, v))
      end
    end
    erb_file = File.join(@@smithy_bin_root, "/etc/templates/web/machine_version_table.html.erb")

  else
    @version_table = {}
    universal = false
    Package.alternate_versions(@path).each do |v|
      @version_table[v] = Package.alternate_builds(File.join(@path, v))
      universal = true if @version_table[v].select{|b| b =~ /(universal|binary)/}.size > 0
    end
    if universal
      erb_file = File.join(@@smithy_bin_root, "/etc/templates/web/version_list.html.erb")
    else
      erb_file = File.join(@@smithy_bin_root, "/etc/templates/web/version_table.html.erb")
    end

  end

  erb = ERB.new(File.read(erb_file), nil, "<>")
  return erb.result(binding)
end

#sanitize_content!Object



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

def sanitize_content!
  # Increment h tags by 2
  @content.gsub!(/<h(\d)>/)   {|m| "<h#{$1.to_i+1}>"}
  @content.gsub!(/<\/h(\d)>/) {|m| "</h#{$1.to_i+1}>"}

  # Don't use <code> inside a <pre>
  @content.gsub!(/<pre(.*?)><code>/) {|m| "<pre#{$1}>"}
  @content.gsub!(/<\/code><\/pre>/, "</pre>")
end

#valid?Boolean

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/smithy/description.rb', line 77

def valid?
  raise "Cannot find the package #{@path}" unless Dir.exists? @path
  return true
end