-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert
More file actions
executable file
·61 lines (51 loc) · 1.44 KB
/
convert
File metadata and controls
executable file
·61 lines (51 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby
require 'nokogiri'
require 'pry'
require 'rufo'
unless STDIN.tty?
data = STDIN.read
SPECIAL_ATTRIBUTES = {
"href": "path"
}
MATE_ELEMENT_NAMES = {
"p" => "paragraph",
"a" => "link"
}
def identify_elements(element)
if (element.elements.any?)
children = element.elements.map { |e| identify_elements(e) }
identify_element(element, children)
else
identify_element(element, [])
end
end
def identify_element(element, matestack_children = [])
element_name = MATE_ELEMENT_NAMES[element.name] || element.name
if matestack_children.any?
<<~SQL.chomp
#{element_name} #{identify_attributes(element)} do
#{matestack_children.join("\n")}
end
SQL
else
<<~SQL.chomp
#{element_name} #{identify_attributes(element, true)}
SQL
end
end
def identify_attributes(element, no_children = false)
matt_attributes = element.attributes.to_a.map do |attribute|
name = SPECIAL_ATTRIBUTES[attribute[1].name] || attribute[1].name
if name.include? '-'
"'#{name}':\"#{attribute[1].value}\""
else
"#{name}:\"#{attribute[1].value}\""
end
end
matt_attributes << "text:\"#{element.text.strip}\"" if element.children.first&.text.to_s.strip != ""
matt_attributes.join(",\s")
end
@doc = Nokogiri::XML(data)
result = identify_elements(@doc.elements.first)
puts Rufo::Formatter.format(result)
end