Class: Html2fortitude::SourceTemplate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, contents, options) ⇒ SourceTemplate

Returns a new instance of SourceTemplate.



7
8
9
10
11
12
13
14
15
# File 'lib/html2fortitude/source_template.rb', line 7

def initialize(filename, contents, options)
  options.assert_valid_keys(:output, :class_name, :class_base, :superclass, :method, :assigns,
    :do_end, :new_style_hashes)

  @filename = filename
  @contents = contents
  @options = options
  @line_count = @contents.split(/(\r|\n|\r\n)/).length
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/html2fortitude/source_template.rb', line 5

def filename
  @filename
end

#line_countObject (readonly)

Returns the value of attribute line_count.



5
6
7
# File 'lib/html2fortitude/source_template.rb', line 5

def line_count
  @line_count
end

Instance Method Details

#output_filenameObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/html2fortitude/source_template.rb', line 37

def output_filename
  if (! options[:output]) && (filename == "-")
    "-"
  elsif (! options[:output])
    if filename =~ /^(.*)\.html\.erb$/i || filename =~ /^(.*)\.rhtml$/i
      "#{$1}.rb"
    else
      "#{filename}.rb"
    end
  elsif File.directory?(options[:output])
    File.join(File.expand_path(options[:output]), "#{output_class_name.underscore}.rb")
  else
    File.expand_path(options[:output])
  end
end

#write_transformed_content!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/html2fortitude/source_template.rb', line 17

def write_transformed_content!
  html_options = {
    :class_name => output_class_name,
    :superclass => options[:superclass],
    :method => options[:method],
    :assigns => options[:assigns],
    :do_end => options[:do_end],
    :new_style_hashes => options[:new_style_hashes]
  }

  html = HTML.new(contents, html_options).render

  if output_filename == '-'
    puts html
  else
    FileUtils.mkdir_p(File.dirname(output_filename))
    File.open(output_filename, 'w') { |f| f << html }
  end
end