Module: Dolt::View::Tree

Included in:
Sinatra::MultiRepoBrowser, Sinatra::SingleRepoBrowser
Defined in:
lib/dolt/view/tree.rb

Instance Method Summary collapse

Instance Method Details

#accumulate_path(pieces) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/dolt/view/tree.rb', line 75

def accumulate_path(pieces)
  acc = []
  pieces.map do |piece|
    piece.map do |p|
      next p if p == ""
      acc << p
      File.join(acc)
    end
  end
end

#partition_path(path, maxdepth = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/dolt/view/tree.rb', line 64

def partition_path(path, maxdepth = nil)
  path = path.sub(/^\.?\//, "")
  result = [[""]]
  return result if path == ""
  parts = path.split("/")
  maxdepth ||= parts.length
  (parts.length - maxdepth + 1).times { result[0] << parts.shift }
  result << [parts.shift] while parts.length > 0
  result
end

#tree_context(repo, ref, path, maxdepth = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dolt/view/tree.rb', line 31

def tree_context(repo, ref, path, maxdepth = nil)
  levels = accumulate_path(partition_path(path, maxdepth))
  return "" if levels.length == 1 && levels[0].length == 1
  total = 4 + levels.length
  colspan = total
  (levels.map do |level|
    html = <<-HTML
      <tr>
        #{'<td></td>' * (total - colspan)}
        <td colspan="#{colspan}">
          #{tree_context_level_links(repo, ref, level)}
        </td>
      </tr>
    HTML
    colspan -= 1
    html
  end).join
end


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dolt/view/tree.rb', line 50

def tree_context_level_links(repo, ref, level)
  extra = "<i class=\"icon icon-folder-open\"></i>"

  (level.map do |path|
     dir = File.dirname(path)
     dir = "" if dir == "."
     file = path == "" ? "/" : File.basename(path)
     url = object_url(repo, ref, dir, { :type => :tree, :name => file })
     html = "<a href=\"#{url}\">#{extra} #{file}</a>"
     extra = ""
     html
  end).join(" ")
end

#tree_entries(tree) ⇒ Object



26
27
28
29
# File 'lib/dolt/view/tree.rb', line 26

def tree_entries(tree)
  sort(tree.entries.select { |e| e[:type] == :tree }) +
    sort(tree.entries.select { |e| e[:type] == :blob })
end

#tree_table_padding_td(path) ⇒ Object



90
91
92
# File 'lib/dolt/view/tree.rb', line 90

def tree_table_padding_td(path)
  "<td></td>" * tree_table_padding_width(path)
end

#tree_table_padding_width(path) ⇒ Object



86
87
88
# File 'lib/dolt/view/tree.rb', line 86

def tree_table_padding_width(path)
  path.split("/").length
end

#tree_url(repository, ref, path) ⇒ Object



22
23
24
# File 'lib/dolt/view/tree.rb', line 22

def tree_url(repository, ref, path)
  repo_url(repository, "/tree/#{ref}:#{path}")
end