html-renderer 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/bin/html2ansi +5 -2
- data/examples/ansi_renderer.rb +1 -1
- data/lib/html-renderer/base.rb +5 -3
- data/lib/html-renderer/html_parser.rb +105 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef89dd4c00345deca8c54a7ac62823dd8a506a0f941186cdd6d82ebd51ae7d47
|
4
|
+
data.tar.gz: 4a2fd1e8b670ff87fd63b7e94a7e50af7bd66e73f94bb48b197b9f8374b8d196
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0dd0122febdc3018b1d2d1f38cd6fc19c23e2495989b6b8ccca1f5e7cf096d159a38aa85fdb600f48767d75827ce6f025282a9b0a206e74aa93c60e5c02bedc
|
7
|
+
data.tar.gz: 8164fb8d54a3f2895bb288d32eb9e03716ff8bd3a40b68382385c499cae363c1d0ff685f77abeac96a83c0b313edfd9c9c3b013173d878ac46b155b28b84466c
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/bin/html2ansi
CHANGED
@@ -69,6 +69,9 @@ private
|
|
69
69
|
s&.downcase&.scan(/\w+/)&.join
|
70
70
|
end
|
71
71
|
|
72
|
+
def subscript(s)
|
73
|
+
"[#{s}]"
|
74
|
+
end
|
72
75
|
|
73
76
|
public
|
74
77
|
|
@@ -139,7 +142,7 @@ public
|
|
139
142
|
when 1 then title.bold.yellow
|
140
143
|
when 2 then title.bold.cyan
|
141
144
|
when 3 then title.bold.blue
|
142
|
-
else title.
|
145
|
+
else title.magenta
|
143
146
|
end
|
144
147
|
|
145
148
|
"#{bar}\n #{title}\n#{bar}\n\n"
|
@@ -219,7 +222,7 @@ def render(stream, paged=false)
|
|
219
222
|
lesspipe { |less| less.puts output }
|
220
223
|
else
|
221
224
|
puts output
|
222
|
-
end
|
225
|
+
end
|
223
226
|
end
|
224
227
|
|
225
228
|
#######################################################################################
|
data/examples/ansi_renderer.rb
CHANGED
data/lib/html-renderer/base.rb
CHANGED
@@ -96,6 +96,8 @@ private
|
|
96
96
|
emphasis(render_children(node, state))
|
97
97
|
when "sup"
|
98
98
|
superscript(render_children(node, state))
|
99
|
+
when "sub"
|
100
|
+
subscript(render_children(node, state))
|
99
101
|
when "u"
|
100
102
|
underline(render_children(node, state))
|
101
103
|
when "br"
|
@@ -118,7 +120,7 @@ private
|
|
118
120
|
list_item(render_children(node, state), state.list_order)
|
119
121
|
|
120
122
|
when "code"
|
121
|
-
block_code(render_children(node, state))
|
123
|
+
block_code(render_children(node, state), nil)
|
122
124
|
when "blockquote"
|
123
125
|
block_quote(render_children(node, state))
|
124
126
|
|
@@ -149,9 +151,9 @@ private
|
|
149
151
|
table(header, rows)
|
150
152
|
|
151
153
|
when "html", "body", "nav", "span", "form", "label", "input", "button", "section", "fieldset",
|
152
|
-
"menu", "article", "header", "time", "aside", "footer", "nobr", "wbr",
|
154
|
+
"menu", "article", "header", "time", "aside", "footer", "nobr", "wbr",
|
153
155
|
"table", "tr", "td", "th", "tt", "thead", "tbody", "noscript", "select",
|
154
|
-
"address", "center"
|
156
|
+
"address", "center", "small"
|
155
157
|
render_children(node, state)
|
156
158
|
|
157
159
|
when "head", "script", "link", "style"
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
|
3
|
+
class Stack < Array
|
4
|
+
alias_method :top, :last
|
5
|
+
alias_method :peek, :last
|
6
|
+
end
|
7
|
+
|
8
|
+
class String
|
9
|
+
def recursive_inspect(depth)
|
10
|
+
(" "*depth)+inspect
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class HTMLParser
|
15
|
+
|
16
|
+
OPEN_TAG_RE = %r{<([^>]+)>}
|
17
|
+
CLOSE_TAG_RE = %r{</([^>]+)>}
|
18
|
+
TEXT_RE = %r{[^<]+}
|
19
|
+
ATTR_RE = %r{(\w+)=(?:"([^"]+)"|'([^']+)'|(\w+))}
|
20
|
+
|
21
|
+
class Tag
|
22
|
+
|
23
|
+
attr_accessor :name, :attrs, :children
|
24
|
+
|
25
|
+
def self.from_str(s)
|
26
|
+
name, rest = s.split(/\s+/, 2)
|
27
|
+
|
28
|
+
if rest
|
29
|
+
attrs = rest.scan(HTMLParser::ATTR_RE).flatten.compact.each_slice(2).to_h
|
30
|
+
else
|
31
|
+
attrs = {}
|
32
|
+
end
|
33
|
+
new(name, attrs)
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(name, attrs={}, children=[])
|
37
|
+
@name = name
|
38
|
+
@attrs = attrs
|
39
|
+
@children = children
|
40
|
+
end
|
41
|
+
|
42
|
+
def recursive_inspect(depth=0)
|
43
|
+
curdent = " "*depth
|
44
|
+
indent = " "*(depth+1)
|
45
|
+
"#{curdent}<#{name} #{attrs}>\n#{indent}#{children.map{|c| c.recursive_inspect(depth+1)}}\n#{curdent}</#{name}>"
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
def initialize(html)
|
51
|
+
@s = StringScanner.new(html)
|
52
|
+
# @s = html
|
53
|
+
end
|
54
|
+
|
55
|
+
def each_tag
|
56
|
+
until @s.eos?
|
57
|
+
if @s.scan(CLOSE_TAG_RE)
|
58
|
+
yield [:close_tag, @s.captures.first]
|
59
|
+
elsif @s.scan(OPEN_TAG_RE)
|
60
|
+
tag = Tag.from_str(@s.captures.first)
|
61
|
+
yield [:open_tag, tag]
|
62
|
+
elsif @s.scan(TEXT_RE)
|
63
|
+
yield [:text, @s.matched]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def as_tree
|
69
|
+
tree.map { |e| e.recursive_inspect }
|
70
|
+
end
|
71
|
+
|
72
|
+
def tree
|
73
|
+
stack = Stack.new
|
74
|
+
stack.push Tag.new("root")
|
75
|
+
|
76
|
+
each_tag do |type, elem|
|
77
|
+
case type
|
78
|
+
when :text
|
79
|
+
text = elem.strip
|
80
|
+
stack.top.children << text unless text.empty?
|
81
|
+
when :open_tag
|
82
|
+
stack.top.children << elem
|
83
|
+
stack.push elem
|
84
|
+
when :close_tag
|
85
|
+
stack.pop
|
86
|
+
else
|
87
|
+
raise "wat"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
stack
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
unless file = ARGV.first
|
97
|
+
file = "test.html"
|
98
|
+
end
|
99
|
+
|
100
|
+
html = File.read(file)
|
101
|
+
|
102
|
+
r = HTMLParser.new(html)
|
103
|
+
r.each_tag{|t| p t}
|
104
|
+
|
105
|
+
# puts r.as_tree
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html-renderer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- epitron
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oga
|
@@ -61,6 +61,7 @@ files:
|
|
61
61
|
- examples/test.html
|
62
62
|
- lib/html-renderer.rb
|
63
63
|
- lib/html-renderer/base.rb
|
64
|
+
- lib/html-renderer/html_parser.rb
|
64
65
|
homepage: http://github.com/epitron/html-renderer/
|
65
66
|
licenses:
|
66
67
|
- WTFPL
|