Class: PastiePacker

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

Instance Method Summary collapse

Instance Method Details

#files_to_string(files) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pastiepacker/conversion.rb', line 10

def files_to_string(files)
  readme = files.find { |f| f =~ /^readme/i }
  files = [readme] + (files - [readme]) if readme
  output = files.inject([]) do |mem, file|
    file = file.strip
    if File.file?(file)
      mem << "## #{file}"
      if ascii? file
        mem << File.open(file, 'r').read
      else
      end
      mem << ""
    end
    mem
  end.join("\n")
  output
end

#path_to_string(path) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/pastiepacker/conversion.rb', line 2

def path_to_string(path)
  output = ""
  FileUtils.cd path do
    output = files_to_string Dir['**/*'].sort
  end
  output
end

#unpack(target_path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pastiepacker/conversion.rb', line 28

def unpack(target_path)
  FileUtils.rm_rf "#{target_path}/*" # destroys previously unpacked pastie with same id
  FileUtils.mkdir_p target_path
  FileUtils.cd target_path do
    files_contents = contents.split(/^## /)[1..-1] # ignore first ""
    files_contents.each do |file_contents|
      file_name, contents = file_contents.match(/([^\n]+)\n(.*)/m)[1,2]
      if file_name =~ /about:/
        # ignoring header
      else
        contents = contents.gsub(/\n\Z/,'')
        FileUtils.mkdir_p File.dirname(file_name)
        File.open(file_name, "w") do |f|
          f << contents
        end
      end
    end
  end
  target_path
end