shady 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/bin/shady +27 -0
- data/lib/shady.rb +185 -0
- data/shady.gemspec +20 -0
- metadata +7 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2babac6cf01270524c41855da16f5c4b74bcdf604e0672053f51f17c72b74973
|
|
4
|
+
data.tar.gz: f173f58f9cb6b7a1e36d7201d56a008933f5a96eae9ee34c129f55863330495d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ab57125385c372783896996a47a3be9b9ef76c2830e08268266dc8433178a5da99a81d0efd6ed7cb12ec090acabbfee6fbe495395f83bf5edf9d5fb7262f1c16
|
|
7
|
+
data.tar.gz: 0a9b03594c8cd8d5ec19f7b35ae313c8153a04b0daa5394549ca73cdda7d07ad0f28850be2ca7f9841cb6cb537402f4f1d136607e4936ad5377a5aecbf8963be
|
data/Gemfile
ADDED
data/bin/shady
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "optparse"
|
|
4
|
+
require "shady"
|
|
5
|
+
|
|
6
|
+
trap("INT" ) { abort "\n" }
|
|
7
|
+
trap("PIPE") { abort "\n" } rescue nil
|
|
8
|
+
|
|
9
|
+
opts = {}
|
|
10
|
+
|
|
11
|
+
OptionParser.new.instance_eval do
|
|
12
|
+
@version = "0.1.1"
|
|
13
|
+
@banner = "usage: #{program_name} [options] [file]"
|
|
14
|
+
|
|
15
|
+
on "-a", "--attrs", "Honor attributes and namespaces (default)"
|
|
16
|
+
on "-x", "--xml" , "Input is XML"
|
|
17
|
+
|
|
18
|
+
Kernel.abort to_s if ARGV.empty?
|
|
19
|
+
self
|
|
20
|
+
end.parse!(into: opts) rescue abort($!.message)
|
|
21
|
+
|
|
22
|
+
opts.transform_keys!(&:to_s) # stringify keys
|
|
23
|
+
|
|
24
|
+
atts = opts["attrs"] || true
|
|
25
|
+
is_x = opts["xml" ]
|
|
26
|
+
|
|
27
|
+
puts ARGF.read.xml_to_slim if is_x
|
data/lib/shady.rb
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# ==============================================================================
|
|
2
|
+
# shady - A Ruby gem to work with Slim, XML, and hashes
|
|
3
|
+
#
|
|
4
|
+
# Author: Steve Shreeve (steve.shreeve@gmail.com)
|
|
5
|
+
# Date: October 17, 2024
|
|
6
|
+
#
|
|
7
|
+
# Legal: MIT license
|
|
8
|
+
# ==============================================================================
|
|
9
|
+
|
|
10
|
+
require "bindings"
|
|
11
|
+
require "nokogiri"
|
|
12
|
+
require "slim"
|
|
13
|
+
|
|
14
|
+
# ==[ Monkey patch the Temple gem ]==
|
|
15
|
+
|
|
16
|
+
=begin
|
|
17
|
+
|
|
18
|
+
# This requires the following tweak to the 'temple' gem
|
|
19
|
+
|
|
20
|
+
diff --git a/lib/temple/html/pretty.rb b/lib/temple/html/pretty.rb
|
|
21
|
+
index 6143573..29ad071 100644
|
|
22
|
+
--- a/lib/temple/html/pretty.rb
|
|
23
|
+
+++ b/lib/temple/html/pretty.rb
|
|
24
|
+
@@ -10,7 +10,8 @@ module Temple
|
|
25
|
+
header hgroup hr html li link meta nav ol option p
|
|
26
|
+
rp rt ruby section script style table tbody td tfoot
|
|
27
|
+
th thead tr ul video doctype).freeze,
|
|
28
|
+
- pre_tags: %w(code pre textarea).freeze
|
|
29
|
+
+ pre_tags: %w(code pre textarea).freeze,
|
|
30
|
+
+ strip: false
|
|
31
|
+
|
|
32
|
+
def initialize(opts = {})
|
|
33
|
+
super
|
|
34
|
+
@@ -62,6 +63,18 @@ module Temple
|
|
35
|
+
result = [:multi, [:static, "#{tag_indent(name)}<#{name}"], compile(attrs)]
|
|
36
|
+
result << [:static, (closed && @format != :html ? ' /' : '') + '>']
|
|
37
|
+
|
|
38
|
+
+ # strip newlines around terminal nodes
|
|
39
|
+
+ @pretty = true
|
|
40
|
+
+ case content
|
|
41
|
+
+ in [:multi, [:newline]]
|
|
42
|
+
+ return (result << [:static, "</#{name}>"])
|
|
43
|
+
+ in [:multi, [:multi, [:static, str]]]
|
|
44
|
+
+ return (result << [:static, "#{str.strip}</#{name}>"])
|
|
45
|
+
+ in [:multi, [:escape, true, [:dynamic, code]], [:multi, [:newline]]]
|
|
46
|
+
+ return (result << [:multi, [:escape, true, [:dynamic, code]], [:static, "</#{name}>"]])
|
|
47
|
+
+ else nil
|
|
48
|
+
+ end if options[:strip]
|
|
49
|
+
+
|
|
50
|
+
@pretty = !@pre_tags || !options[:pre_tags].include?(name)
|
|
51
|
+
if content
|
|
52
|
+
@indent += 1
|
|
53
|
+
=end
|
|
54
|
+
|
|
55
|
+
module Temple
|
|
56
|
+
module HTML
|
|
57
|
+
class Pretty
|
|
58
|
+
def on_html_tag(name, attrs, content = nil)
|
|
59
|
+
return super unless @pretty
|
|
60
|
+
|
|
61
|
+
name = name.to_s
|
|
62
|
+
closed = !content || (empty_exp?(content) && options[:autoclose].include?(name))
|
|
63
|
+
|
|
64
|
+
@pretty = false
|
|
65
|
+
result = [:multi, [:static, "#{tag_indent(name)}<#{name}"], compile(attrs)]
|
|
66
|
+
result << [:static, (closed && @format != :html ? ' /' : '') + '>']
|
|
67
|
+
|
|
68
|
+
# strip newlines around terminal nodes
|
|
69
|
+
@pretty = true
|
|
70
|
+
case content
|
|
71
|
+
in [:multi, [:newline]]
|
|
72
|
+
return (result << [:static, "</#{name}>"])
|
|
73
|
+
in [:multi, [:multi, [:static, str]]]
|
|
74
|
+
return (result << [:static, "#{str.strip}</#{name}>"])
|
|
75
|
+
in [:multi, [:escape, true, [:dynamic, code]], [:multi, [:newline]]]
|
|
76
|
+
return (result << [:multi, [:escape, true, [:dynamic, code]], [:static, "</#{name}>"]])
|
|
77
|
+
else nil
|
|
78
|
+
end if options[:strip]
|
|
79
|
+
|
|
80
|
+
@pretty = !@pre_tags || !options[:pre_tags].include?(name)
|
|
81
|
+
if content
|
|
82
|
+
@indent += 1
|
|
83
|
+
result << compile(content)
|
|
84
|
+
@indent -= 1
|
|
85
|
+
end
|
|
86
|
+
unless closed
|
|
87
|
+
indent = tag_indent(name)
|
|
88
|
+
result << [:static, "#{content && !empty_exp?(content) ? indent : ''}</#{name}>"]
|
|
89
|
+
end
|
|
90
|
+
@pretty = true
|
|
91
|
+
result
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# ==[ Extend Nokogiri::XML ]==
|
|
98
|
+
|
|
99
|
+
module Nokogiri::XML
|
|
100
|
+
class Document
|
|
101
|
+
def to_hash
|
|
102
|
+
root.to_hash
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
class Node
|
|
107
|
+
def to_hash(hash={})
|
|
108
|
+
this = {}
|
|
109
|
+
|
|
110
|
+
children.each do |c|
|
|
111
|
+
if c.element?
|
|
112
|
+
c.to_hash(this)
|
|
113
|
+
elsif c.text? || c.cdata?
|
|
114
|
+
(this[''] ||= '') << c.content
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
text = this.delete('') and text.strip!
|
|
119
|
+
this = text || '' if this.empty? # wtf if !this.empty? && !text.empty?
|
|
120
|
+
|
|
121
|
+
case hash[name]
|
|
122
|
+
when nil then hash[name] = this
|
|
123
|
+
when Hash, String then hash[name] = [hash[name], this]
|
|
124
|
+
when Array then hash[name] << this
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
hash
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def to_slim(deep=0, ns=Set.new)
|
|
131
|
+
slim = "#{' ' * deep}#{name}"
|
|
132
|
+
|
|
133
|
+
# attributes and namespaces
|
|
134
|
+
atts = []
|
|
135
|
+
list = ns.dup
|
|
136
|
+
attributes.map do |name, attr|
|
|
137
|
+
atts << "#{attr.namespace&.prefix&.concat(':')}#{name}=\"#{attr.value}\""
|
|
138
|
+
end
|
|
139
|
+
namespaces.map do |pref, href|
|
|
140
|
+
pair = "#{pref}=\"#{href}\""
|
|
141
|
+
atts << pair if list.add?(pair)
|
|
142
|
+
end
|
|
143
|
+
slim << "(#{atts.join(" ")})" unless atts.empty?
|
|
144
|
+
|
|
145
|
+
# terminals and children
|
|
146
|
+
if (kids = element_children).empty? && (info = text&.strip)
|
|
147
|
+
info.empty? ? slim : (slim << " #{info}")
|
|
148
|
+
else
|
|
149
|
+
kids.inject([slim]) {|a, k| a << k.to_slim(deep + 1, list) }.join("\n")
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# ==[ Extend Slim ]==
|
|
156
|
+
|
|
157
|
+
module Slim
|
|
158
|
+
class Template
|
|
159
|
+
def result
|
|
160
|
+
binding.of_caller(2).eval(@src)
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
class String
|
|
166
|
+
def slim(scope=nil)
|
|
167
|
+
Slim::Template.new(pretty: true, strip: true, format: :xml) { self }.result
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def xml_to_hash
|
|
171
|
+
Nokogiri.XML(self) {|o| o.default_xml.noblanks}.to_hash rescue {}
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def xml_to_slim
|
|
175
|
+
Nokogiri.XML(self) {|o| o.default_xml.noblanks }.root.to_slim # rescue ""
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def xml_to_xml
|
|
179
|
+
Nokogiri.XML(self) {|o| o.default_xml.noblanks}.to_xml(indent:2) rescue self
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
def slim(str="")
|
|
184
|
+
Slim::Template.new(pretty: true, strip: true, format: :xml) { str }.result
|
|
185
|
+
end
|
data/shady.gemspec
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |gem|
|
|
4
|
+
gem.name = "shady"
|
|
5
|
+
gem.version = `grep -m 1 '^\s*@version' bin/shady | head -1 | cut -f 2 -d '"'`
|
|
6
|
+
gem.author = "Steve Shreeve"
|
|
7
|
+
gem.email = "steve.shreeve@gmail.com"
|
|
8
|
+
gem.summary = "This gem allows easy conversion between Slim, XML, and hashes"
|
|
9
|
+
gem.description = "Ruby gem to work with Slim, XML, and hashes"
|
|
10
|
+
gem.homepage = "https://github.com/shreeve/shady"
|
|
11
|
+
gem.license = "MIT"
|
|
12
|
+
gem.platform = Gem::Platform::RUBY
|
|
13
|
+
gem.files = `git ls-files`.split("\n") - %w[.gitignore]
|
|
14
|
+
gem.executables = `cd bin && git ls-files .`.split("\n")
|
|
15
|
+
gem.required_ruby_version = Gem::Requirement.new(">= 3.0") if gem.respond_to? :required_ruby_version=
|
|
16
|
+
|
|
17
|
+
gem.add_dependency "bindings", "~>1.0.0"
|
|
18
|
+
gem.add_dependency "nokogiri", "~>1.16.7"
|
|
19
|
+
gem.add_dependency "slim" , "~>5.2.1"
|
|
20
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: shady
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Steve Shreeve
|
|
@@ -54,12 +54,17 @@ dependencies:
|
|
|
54
54
|
version: 5.2.1
|
|
55
55
|
description: Ruby gem to work with Slim, XML, and hashes
|
|
56
56
|
email: steve.shreeve@gmail.com
|
|
57
|
-
executables:
|
|
57
|
+
executables:
|
|
58
|
+
- shady
|
|
58
59
|
extensions: []
|
|
59
60
|
extra_rdoc_files: []
|
|
60
61
|
files:
|
|
62
|
+
- Gemfile
|
|
61
63
|
- LICENSE
|
|
62
64
|
- README.md
|
|
65
|
+
- bin/shady
|
|
66
|
+
- lib/shady.rb
|
|
67
|
+
- shady.gemspec
|
|
63
68
|
homepage: https://github.com/shreeve/shady
|
|
64
69
|
licenses:
|
|
65
70
|
- MIT
|