Module: MarkdownServer::Helpers::FormattingHelpers

Defined in:
lib/markdown_server/helpers/formatting_helpers.rb

Instance Method Summary collapse

Instance Method Details



36
37
38
39
40
41
42
43
44
# File 'lib/markdown_server/helpers/formatting_helpers.rb', line 36

def breadcrumbs(path)
  parts = path.split("/").reject(&:empty?)
  crumbs = [{ name: "home", href: "/browse/" }]
  parts.each_with_index do |part, i|
    href = "/browse/" + parts[0..i].map { |p| encode_path_component(p) }.join("/") + "/"
    crumbs << { name: part, href: href }
  end
  crumbs
end

#dir_titleObject



46
47
48
49
# File 'lib/markdown_server/helpers/formatting_helpers.rb', line 46

def dir_title
  return settings.custom_title if settings.respond_to?(:custom_title) && settings.custom_title
  File.basename(root_dir).gsub(/[-_]/, " ").gsub(/\b\w/, &:upcase)
end

#format_date(time) ⇒ Object



14
15
16
# File 'lib/markdown_server/helpers/formatting_helpers.rb', line 14

def format_date(time)
  time.strftime("%Y-%m-%d %H:%M")
end

#format_size(bytes) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/markdown_server/helpers/formatting_helpers.rb', line 4

def format_size(bytes)
  if bytes < 1024
    "#{bytes} B"
  elsif bytes < 1024 * 1024
    "%.1f KB" % (bytes / 1024.0)
  else
    "%.1f MB" % (bytes / (1024.0 * 1024))
  end
end

#icon_for(entry_name, is_dir) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/markdown_server/helpers/formatting_helpers.rb', line 18

def icon_for(entry_name, is_dir)
  if is_dir
    "\u{1F4C1}"
  else
    ext = File.extname(entry_name).downcase
    case ext
    when ".md" then "\u{1F4DD}"
    when ".pdf" then "\u{1F4D5}"
    when ".json" then "\u{1F4CB}"
    when ".py" then "\u{1F40D}"
    when ".rb" then "\u{1F48E}"
    when ".csv" then "\u{1F4CA}"
    when ".epub" then "\u{1F4D6}"
    else "\u{1F4C4}"
    end
  end
end

#inline_directory_html(dir_path, relative_dir) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/markdown_server/helpers/formatting_helpers.rb', line 51

def inline_directory_html(dir_path, relative_dir)
  entries = Dir.entries(dir_path).reject { |e| e.start_with?(".") || EXCLUDED.include?(e) }
  items = entries.map do |name|
    stat = File.stat(File.join(dir_path, name)) rescue next
    is_dir = stat.directory?
    href = "/browse/" + (relative_dir.empty? ? "" : relative_dir + "/") +
           encode_path_component(name) + (is_dir ? "/" : "")
    { name: name, is_dir: is_dir, href: href }
  end.compact.sort_by { |i| [i[:is_dir] ? 0 : 1, i[:name].downcase] }

  rows = items.map do |i|
    %(<li><a href="#{h(i[:href])}"><span class="icon">#{icon_for(i[:name], i[:is_dir])}</span> ) +
    %(#{h(i[:name])}#{i[:is_dir] ? "/" : ""}</a></li>)
  end.join
  %(<ul class="dir-listing">#{rows}</ul>)
end

#parent_dir_path(relative_path) ⇒ Object



72
73
74
75
# File 'lib/markdown_server/helpers/formatting_helpers.rb', line 72

def parent_dir_path(relative_path)
  parts = relative_path.split("/")
  parts.length > 1 ? parts[0..-2].join("/") : ""
end

#search_form_path(relative_path) ⇒ Object



68
69
70
# File 'lib/markdown_server/helpers/formatting_helpers.rb', line 68

def search_form_path(relative_path)
  "/search/" + relative_path.split("/").map { |p| encode_path_component(p) }.join("/")
end