html-renderer 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gemspec +4 -2
- data/VERSION +1 -1
- data/bin/html2ansi +245 -0
- data/lib/html-renderer/base.rb +5 -3
- metadata +10 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bbe90456ebf54ed08a1d78e5056fa82a4ccc68e6487cd9fce72f807669464e8
|
4
|
+
data.tar.gz: 2106840d7e1b57aedaced18c6cf122d928f6ce6def84cf191c8c4d84373750da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca6fda95a4b8d7ae62310d2600486420f2d239e443474068e00953e86d3c61e20680651c2b59d688aa4e9e73884bd98db131bbf053597d993d508e37dba9fe00
|
7
|
+
data.tar.gz: 7bea3114914af7a5122adad1e74b0beb44a7d2cc5658f88b056aae1b1ac1b37180999b774151e7def45e4f6d6e2b276c05259009721d250a96aa4fcf21351f53
|
data/.gemspec
CHANGED
@@ -12,9 +12,11 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.email = "chris@ill-logic.com"
|
13
13
|
s.authors = ["epitron"]
|
14
14
|
|
15
|
-
s.files
|
15
|
+
s.files = `git ls`.lines.map(&:strip)
|
16
|
+
s.executables = ['html2ansi']
|
16
17
|
s.extra_rdoc_files = ["README.md", "LICENSE"]
|
17
18
|
|
18
19
|
s.add_dependency "oga", "~> 2"
|
19
|
-
s.
|
20
|
+
s.add_dependency "terminal-table", "~> 1.8"
|
21
|
+
# s.add_development "ansi", "~> 1"
|
20
22
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/bin/html2ansi
ADDED
@@ -0,0 +1,245 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#######################################################################################
|
3
|
+
require 'html-renderer'
|
4
|
+
require 'terminal-table'
|
5
|
+
require 'term/ansicolor'
|
6
|
+
require 'coderay'
|
7
|
+
#######################################################################################
|
8
|
+
|
9
|
+
class String
|
10
|
+
# include ANSI::Mixin
|
11
|
+
include Term::ANSIColor
|
12
|
+
|
13
|
+
def grey; self.black.bold; end
|
14
|
+
end
|
15
|
+
|
16
|
+
#######################################################################################
|
17
|
+
|
18
|
+
def lesspipe(*args)
|
19
|
+
if args.any? and args.last.is_a?(Hash)
|
20
|
+
options = args.pop
|
21
|
+
else
|
22
|
+
options = {}
|
23
|
+
end
|
24
|
+
|
25
|
+
output = args.first if args.any?
|
26
|
+
|
27
|
+
params = []
|
28
|
+
params << "-R" unless options[:color] == false
|
29
|
+
params << "-S" unless options[:wrap] == true
|
30
|
+
params << "-F" unless options[:always] == true
|
31
|
+
if options[:tail] == true
|
32
|
+
params << "+\\>"
|
33
|
+
$stderr.puts "Seeking to end of stream..."
|
34
|
+
end
|
35
|
+
params << "-X"
|
36
|
+
|
37
|
+
IO.popen("less #{params * ' '}", "w") do |less|
|
38
|
+
if output
|
39
|
+
less.puts output
|
40
|
+
else
|
41
|
+
yield less
|
42
|
+
end
|
43
|
+
end
|
44
|
+
rescue Errno::EPIPE, Interrupt
|
45
|
+
# less just quit -- eat the exception.
|
46
|
+
end
|
47
|
+
|
48
|
+
#######################################################################################
|
49
|
+
|
50
|
+
class ANSIRenderer < HTMLRenderer::Base
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def indented?(text)
|
55
|
+
indent_sizes = text.lines.map{ |line| if line =~ /^(\s+)/ then $1 else '' end }.map(&:size)
|
56
|
+
indent_sizes.all? {|dent| dent > 0 }
|
57
|
+
end
|
58
|
+
|
59
|
+
def unwrap(text)
|
60
|
+
return text unless indented? text
|
61
|
+
text.lines.to_a.map(&:strip).join ' '
|
62
|
+
end
|
63
|
+
|
64
|
+
def indent(text,amount=2)
|
65
|
+
text.lines.map{|line| " "*amount + line }.join
|
66
|
+
end
|
67
|
+
|
68
|
+
def smash(s)
|
69
|
+
s&.downcase&.scan(/\w+/)&.join
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
public
|
74
|
+
|
75
|
+
def normal_text(text)
|
76
|
+
text
|
77
|
+
end
|
78
|
+
|
79
|
+
def underline(content)
|
80
|
+
content.magenta.bold
|
81
|
+
end
|
82
|
+
|
83
|
+
def superscript(content)
|
84
|
+
"^(#{content})"
|
85
|
+
end
|
86
|
+
|
87
|
+
def link(link, title, content)
|
88
|
+
unless content&.[] /^Back /
|
89
|
+
str = ""
|
90
|
+
# str += "<15>#{content}</15>" if content
|
91
|
+
str += content.white.bold if content
|
92
|
+
if smash(link) != smash(content)
|
93
|
+
# str += " <8>(</8><11>#{link}</11><8>)</8>"
|
94
|
+
str += " #{"(".grey}#{link&.cyan&.bold}#{")".grey}"
|
95
|
+
end
|
96
|
+
|
97
|
+
str
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def anchor(name, title=nil, content=nil)
|
102
|
+
result = "Anchor: ##{name}"
|
103
|
+
result << " (#{title})" if title
|
104
|
+
result << "\n"
|
105
|
+
result << "#{content}\n" if content
|
106
|
+
result
|
107
|
+
end
|
108
|
+
|
109
|
+
def image(link, title, content)
|
110
|
+
link(link, nil, title)
|
111
|
+
end
|
112
|
+
|
113
|
+
def italic(text)
|
114
|
+
text.yellow.bold
|
115
|
+
end
|
116
|
+
|
117
|
+
def block_code(code, language)
|
118
|
+
language ||= :ruby
|
119
|
+
|
120
|
+
language = language[1..-1] if language[0] == "." # strip leading "."
|
121
|
+
language = :cpp if language == "C++"
|
122
|
+
|
123
|
+
require 'coderay'
|
124
|
+
"#{indent CodeRay.scan(code, language).term, 4}\n"
|
125
|
+
end
|
126
|
+
|
127
|
+
def block_quote(text)
|
128
|
+
indent paragraph(text)
|
129
|
+
end
|
130
|
+
|
131
|
+
def codespan(code)
|
132
|
+
code.cyan
|
133
|
+
end
|
134
|
+
|
135
|
+
def header(title, level, anchor=nil)
|
136
|
+
bar = ("-"*(title.size+4)).grey
|
137
|
+
|
138
|
+
title = case level
|
139
|
+
when 1 then title.bold.yellow
|
140
|
+
when 2 then title.bold.cyan
|
141
|
+
when 3 then title.bold.blue
|
142
|
+
else title.purple
|
143
|
+
end
|
144
|
+
|
145
|
+
"#{bar}\n #{title}\n#{bar}\n\n"
|
146
|
+
end
|
147
|
+
|
148
|
+
def double_emphasis(text)
|
149
|
+
text.bold.green
|
150
|
+
end
|
151
|
+
|
152
|
+
def emphasis(text)
|
153
|
+
text.green
|
154
|
+
end
|
155
|
+
|
156
|
+
def linebreak
|
157
|
+
"\n"
|
158
|
+
end
|
159
|
+
|
160
|
+
def paragraph(text)
|
161
|
+
div(text) + "\n"
|
162
|
+
end
|
163
|
+
|
164
|
+
def div(text)
|
165
|
+
"#{indented?(text) ? text : unwrap(text)}\n"
|
166
|
+
end
|
167
|
+
|
168
|
+
def list(content, list_type)
|
169
|
+
case list_type
|
170
|
+
when :ordered
|
171
|
+
@counter = 0
|
172
|
+
"#{content}\n"
|
173
|
+
when :unordered
|
174
|
+
"#{content}\n"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def list_item(content, list_type)
|
179
|
+
case list_type
|
180
|
+
when :ordered
|
181
|
+
@counter ||= 0
|
182
|
+
@counter += 1
|
183
|
+
# " <8>#{@counter}.</8> #{content.strip}\n".colorize
|
184
|
+
" #{@counter.to_s.grey}. #{content.strip}\n"
|
185
|
+
when :unordered
|
186
|
+
# " <8>*</8> #{content.strip}\n".colorize
|
187
|
+
" #{"*".grey} #{content.strip}\n"
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def definition_list(defs)
|
192
|
+
defs.each do |dt, dd|
|
193
|
+
puts "<15>#{dt}<7>:".colorize
|
194
|
+
puts " #{dd}"
|
195
|
+
puts
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
def table(header, rows)
|
200
|
+
if header
|
201
|
+
table = Terminal::Table.new(headings: header, rows: rows)
|
202
|
+
else
|
203
|
+
table = Terminal::Table.new(rows: rows)
|
204
|
+
end
|
205
|
+
"#{table}\n\n"
|
206
|
+
end
|
207
|
+
|
208
|
+
def separator
|
209
|
+
"_____________________________\n\n"
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
#######################################################################################
|
215
|
+
|
216
|
+
def render(stream, paged=false)
|
217
|
+
output = ANSIRenderer.render(stream)
|
218
|
+
if paged
|
219
|
+
lesspipe { |less| less.puts output }
|
220
|
+
else
|
221
|
+
puts output
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
#######################################################################################
|
226
|
+
|
227
|
+
opts, args = ARGV.partition { |arg| arg[/^--?\w/] }
|
228
|
+
|
229
|
+
if opts.include?("-h") or opts.include?("--help")
|
230
|
+
puts "usage:"
|
231
|
+
puts " html2ansi [options] <file.html>"
|
232
|
+
puts " or"
|
233
|
+
puts " curl http://host/path.html | html2ansi [options]"
|
234
|
+
puts
|
235
|
+
puts "options:"
|
236
|
+
puts " -p Paged (redirect output to less)"
|
237
|
+
puts
|
238
|
+
else
|
239
|
+
paged = opts.include?("-p") or opts.include?("--paged")
|
240
|
+
if args.empty?
|
241
|
+
render($stdin, paged)
|
242
|
+
else
|
243
|
+
args.each { |arg| render(open(arg), paged) }
|
244
|
+
end
|
245
|
+
end
|
data/lib/html-renderer/base.rb
CHANGED
@@ -150,15 +150,17 @@ private
|
|
150
150
|
|
151
151
|
when "html", "body", "nav", "span", "form", "label", "input", "button", "section", "fieldset",
|
152
152
|
"menu", "article", "header", "time", "aside", "footer", "nobr", "wbr",
|
153
|
-
"table", "tr", "td", "th", "thead", "tbody", "noscript", "select",
|
154
|
-
"address"
|
153
|
+
"table", "tr", "td", "th", "tt", "thead", "tbody", "noscript", "select",
|
154
|
+
"address", "center"
|
155
155
|
render_children(node, state)
|
156
156
|
|
157
157
|
when "head", "script", "link", "style"
|
158
158
|
# skip it
|
159
159
|
|
160
160
|
else
|
161
|
-
raise "Unrecognized HTML tag: #{node.name} -> #{node.inspect}"
|
161
|
+
# raise "Unrecognized HTML tag: #{node.name} -> #{node.inspect}"
|
162
|
+
$stderr.puts "Unrecognized HTML tag: #{node.name} -> #{node.inspect}"
|
163
|
+
render_children(node, state)
|
162
164
|
end
|
163
165
|
|
164
166
|
when Oga::XML::Comment
|
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.3
|
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-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oga
|
@@ -25,24 +25,25 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: terminal-table
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '1'
|
34
|
-
type: :
|
33
|
+
version: '1.8'
|
34
|
+
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '1'
|
40
|
+
version: '1.8'
|
41
41
|
description: Easily implement an HTML renderer by creating a subclass and adding some
|
42
42
|
methods, similar to RedCarpet. (Examples are included for rendering HTML to ANSI
|
43
43
|
and plain text.)
|
44
44
|
email: chris@ill-logic.com
|
45
|
-
executables:
|
45
|
+
executables:
|
46
|
+
- html2ansi
|
46
47
|
extensions: []
|
47
48
|
extra_rdoc_files:
|
48
49
|
- README.md
|
@@ -53,6 +54,7 @@ files:
|
|
53
54
|
- README.md
|
54
55
|
- Rakefile
|
55
56
|
- VERSION
|
57
|
+
- bin/html2ansi
|
56
58
|
- examples/ansi_renderer.rb
|
57
59
|
- examples/debug_renderer.rb
|
58
60
|
- examples/plain_text_renderer.rb
|
@@ -79,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
81
|
version: '0'
|
80
82
|
requirements: []
|
81
83
|
rubyforge_project:
|
82
|
-
rubygems_version: 2.7.
|
84
|
+
rubygems_version: 2.7.7
|
83
85
|
signing_key:
|
84
86
|
specification_version: 4
|
85
87
|
summary: HTML Renderer
|