Class: Distil::SourceFile

Inherits:
Object
  • Object
show all
Includes:
ErrorReporter
Defined in:
lib/distil/source-file.rb

Direct Known Subclasses

CssFile, HtmlFile, JavascriptFile

Constant Summary collapse

@@file_types =
[]
@@file_cache =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ErrorReporter

error, #ignore_warnings, #ignore_warnings=, #report, warning

Constructor Details

#initialize(filepath) ⇒ SourceFile

Returns a new instance of SourceFile.



14
15
16
17
18
19
20
21
22
# File 'lib/distil/source-file.rb', line 14

def initialize(filepath)
  @full_path= File.expand_path(filepath)

  @parent_folder= File.dirname(@full_path)
  @dependencies= []
  @assets= []

  @@file_cache[@full_path]= self
end

Instance Attribute Details

#full_pathObject

Returns the value of attribute full_path.



8
9
10
# File 'lib/distil/source-file.rb', line 8

def full_path
  @full_path
end

#parent_folderObject

Returns the value of attribute parent_folder.



8
9
10
# File 'lib/distil/source-file.rb', line 8

def parent_folder
  @parent_folder
end

Class Method Details

.file_typesObject



41
42
43
# File 'lib/distil/source-file.rb', line 41

def self.file_types
  @@file_types
end

.from_path(filepath) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/distil/source-file.rb', line 46

def self.from_path(filepath)
  full_path= File.expand_path(filepath)
  file= @@file_cache[full_path]
  return file if file

  extension= File.extname(filepath)[1..-1]

  @@file_types.each { |handler|
    next if (handler.extension != extension)

    return handler.new(filepath)
  }

  return SourceFile.new(filepath)
end

.inherited(subclass) ⇒ Object



37
38
39
# File 'lib/distil/source-file.rb', line 37

def self.inherited(subclass)
  @@file_types << subclass
end

.path_relative_to_folder(path, folder) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/distil/source-file.rb', line 112

def self.path_relative_to_folder(path, folder)
  outputFolder= File.expand_path(folder).to_s
  
  # Remove leading slash and split into parts
  file_parts= path.slice(1..-1).split('/');
  output_parts= outputFolder.slice(1..-1).split('/');

  common_prefix_length= 0

  file_parts.each_index { |i|
    common_prefix_length= i
    break if file_parts[i]!=output_parts[i]
  }

  return '../'*(output_parts.length-common_prefix_length) + file_parts[common_prefix_length..-1].join('/')
end

Instance Method Details

#add_asset(file) ⇒ Object



151
152
153
# File 'lib/distil/source-file.rb', line 151

def add_asset(file)
  @assets << file
end

#add_dependency(file) ⇒ Object



142
143
144
145
# File 'lib/distil/source-file.rb', line 142

def add_dependency(file)
  return if @dependencies.include?(file)
  @dependencies << file
end

#assetsObject



147
148
149
# File 'lib/distil/source-file.rb', line 147

def assets
  @assets
end

#basename(suffix = extension) ⇒ Object



70
71
72
# File 'lib/distil/source-file.rb', line 70

def basename(suffix=extension)
  File.basename(@full_path, suffix)
end

#can_embed_as_content(file) ⇒ Object



24
25
26
# File 'lib/distil/source-file.rb', line 24

def can_embed_as_content(file)
  false
end

#contentObject



90
91
92
# File 'lib/distil/source-file.rb', line 90

def content
  @content ||= load_content
end

#copy_to(folder, prefix) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/distil/source-file.rb', line 155

def copy_to(folder, prefix)
  file_path= self.file_path || relative_to_folder(prefix||"")
  final_target_folder= File.join(folder, File.dirname(file_path))
  FileUtils.mkdir_p final_target_folder
  FileUtils.cp self.full_path, final_target_folder
  File.join(final_target_folder, File.basename(file_path))
end

#dependenciesObject



138
139
140
# File 'lib/distil/source-file.rb', line 138

def dependencies
  @dependencies
end

#error(message, line = nil) ⇒ Object



32
33
34
# File 'lib/distil/source-file.rb', line 32

def error(message, line=nil)
  super(message, self, line)
end

#escape_embeded_content(content) ⇒ Object



86
87
88
# File 'lib/distil/source-file.rb', line 86

def escape_embeded_content(content)
  content
end

#file_pathObject



74
75
76
# File 'lib/distil/source-file.rb', line 74

def file_path
  @file_path
end

#file_path=(path) ⇒ Object



78
79
80
# File 'lib/distil/source-file.rb', line 78

def file_path=(path)
  @file_path=path
end

#last_modifiedObject



94
95
96
# File 'lib/distil/source-file.rb', line 94

def last_modified
  @last_modified ||= File.stat(@full_path).mtime
end

#load_contentObject



82
83
84
# File 'lib/distil/source-file.rb', line 82

def load_content
  File.read(@full_path)
end

#minified_content(source) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/distil/source-file.rb', line 98

def minified_content(source)
	# Run the Y!UI Compressor
  return source if !content_type
	buffer= ""

	IO.popen("java -jar #{$compressor} --type #{content_type}", "r+") { |pipe|
	  pipe.puts(source)
	  pipe.close_write
	  buffer= pipe.read
 }
	  
  buffer
end

#relative_to_file(source_file) ⇒ Object



133
134
135
136
# File 'lib/distil/source-file.rb', line 133

def relative_to_file(source_file)
  folder= File.dirname(File.expand_path(source_file))
  self.relative_to_folder(folder)
end

#relative_to_folder(output_folder) ⇒ Object



129
130
131
# File 'lib/distil/source-file.rb', line 129

def relative_to_folder(output_folder)
  self.class.path_relative_to_folder(@full_path, output_folder)
end

#to_sObject



62
63
64
# File 'lib/distil/source-file.rb', line 62

def to_s
  @full_path
end

#to_strObject



66
67
68
# File 'lib/distil/source-file.rb', line 66

def to_str
  @full_path
end

#warning(message, line = nil) ⇒ Object



28
29
30
# File 'lib/distil/source-file.rb', line 28

def warning(message, line=nil)
  super(message, self, line)
end