Class: NbfTools::HTML

Inherits:
Object
  • Object
show all
Defined in:
lib/nbf_tools/html.rb

Constant Summary collapse

CONTENT_FORMAT =
"<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<!-- This is an automatically generated file.\n     It will be read and overwritten.\n     DO NOT EDIT! -->\n<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n<TITLE>Bookmarks</TITLE>\n<H1>Bookmarks</H1>\n<DL><p>\n%{content}\n</DL><p>"
"%{indent}<DT><A %{attributes}>%{text}</A>"
FOLDER_FORMAT =
"%{indent}<DT><H3 %{attributes}>%{text}</H3>\n%{indent}<DL><p>\n%{items}\n%{indent}</DL><p>"

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ HTML



11
12
13
# File 'lib/nbf_tools/html.rb', line 11

def initialize(data)
  @data = data
end

Instance Method Details

#data_html(data = @data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nbf_tools/html.rb', line 23

def data_html(data = @data)
  return if data.nil?
  data.map do |e|
    case e["type"]
    when NbfTools::Type::LINK
      link_html(e)
    when NbfTools::Type::FOLDER
      folder_html(e)
    end
  end.join("\n")
end

#folder_html(folder) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/nbf_tools/html.rb', line 42

def folder_html(folder)
  indent = "\t" * folder["path"].count("/")
  attributes = generate_attributes(folder, "path", "text", "type", "items")
  text = folder["text"]
  items = data_html(folder["items"])
  sprintf(FOLDER_FORMAT, indent: indent, attributes: attributes, text: text, items: items)
end

#generate_attributes(element, *except_keys) ⇒ Object



19
20
21
# File 'lib/nbf_tools/html.rb', line 19

def generate_attributes(element, *except_keys)
  element.except(*except_keys).transform_keys(&:upcase).map { |k, v| "#{k}=\"#{v}\"" }.join(" ")
end


35
36
37
38
39
40
# File 'lib/nbf_tools/html.rb', line 35

def link_html(item)
  indent = "\t" * (item["path"].count("/") + 1)
  attributes = generate_attributes(item, "path", "text", "type")
  text = item["text"]
  sprintf(LINK_FORMAT, indent: indent, attributes: attributes, text: text)
end

#to_sObject



15
16
17
# File 'lib/nbf_tools/html.rb', line 15

def to_s
  sprintf(CONTENT_FORMAT, content: data_html)
end