html-renderer 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54c85f9f2a8247cdcbb871a115deaad908928b9fb11464fcf44264b4f1c42427
4
- data.tar.gz: 1123629514b87e11b368f8e752e0ec7f0c31ec22b7e0d190ab9e67818f9b2ba9
3
+ metadata.gz: ff38e106acb336902c8cfbfec5dff664be97cd9363741a610fc9ffe619d9bd02
4
+ data.tar.gz: d3a8b5ce270e4229d7821062696df741765f0911ba2f428dbc705ec6611b1322
5
5
  SHA512:
6
- metadata.gz: 9b1b284109ca1625e37d4b2806c518dc3de53d66bd69feca6c48017ab5b3c3c9a9fd0fc0f01073d048df2abf5474252398c08e3e9437a8e9b6c3f3cb76690d41
7
- data.tar.gz: e32329999296b0f1825f1e0a0ca683ae5504c16d4a07ed81b95a5386e425b31f8fab9ad4969fc219f024be5d77bac0a02a9ba5a583cb399a0ca13c680dce5828
6
+ metadata.gz: 5d370198a4ae836fb62b37030cba865d00d08cfd87c5bf37ea24b26e1c34be827bf36c2f5f3f10333d9ad81e1f2bcc3559491025b80c8fa14a08cd073d1f4e59
7
+ data.tar.gz: cd0d53f4d19c32073d519d83b3023aba9c5228c8ecac590943d9a2364c2092e201f49aff59fbfb1a2dfc50616b4f27ed697e470d5377454aec170f13d50f37af
data/.gemspec CHANGED
@@ -13,11 +13,9 @@ Gem::Specification.new do |s|
13
13
  s.authors = ["epitron"]
14
14
 
15
15
  s.files = `git ls`.lines.map(&:strip)
16
- s.executables = ['html2ansi']
17
16
  s.extra_rdoc_files = ["README.md", "LICENSE"]
18
17
 
19
18
  s.add_dependency "oga", "~> 2"
20
19
  s.add_dependency "terminal-table", "~> 1.8"
21
20
  s.add_dependency "term-ansicolor", "~> 1.7"
22
- # s.add_development "ansi", "~> 1"
23
21
  end
data/Rakefile CHANGED
@@ -1,7 +1,6 @@
1
- pkgname = "html-renderer"
2
- gem_version = File.read("VERSION").strip
1
+ gem = eval(File.read '.gemspec')
3
2
 
4
- gemfile = "#{pkgname}-#{gem_version}.gem"
3
+ gemfile = "#{gem.name}-#{gem.version}.gem"
5
4
 
6
5
  task :build do
7
6
  system "gem build .gemspec"
data/TODO.md CHANGED
@@ -2,11 +2,13 @@
2
2
 
3
3
  ## Base class
4
4
 
5
+ ! add HTMLRenderer.reflow_paragraph
5
6
  * #render takes options (and passes them to subclasses)
6
7
  * :debug option (hide parse errors unless 'true')
7
8
 
8
9
  ## ANSI renderer
9
10
 
11
+ * use HTMLRenderer.reflow_paragraph
10
12
  * :wrap option (with optional terminal width)
11
13
  * Better table renderer (with line-drawing)
12
14
  * Follow `<table>` style attributes when drawing tables (eg: borders)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.1.0
@@ -65,7 +65,7 @@ private
65
65
  content.blank? ? normal_text(content) : nil
66
66
 
67
67
  when Oga::XML::Element
68
- case node.name
68
+ case node.name.downcase
69
69
  when "a"
70
70
  url = node["href"]
71
71
  title = node["title"]
@@ -151,7 +151,7 @@ private
151
151
  table(header, rows)
152
152
 
153
153
  when "html", "body", "nav", "span", "form", "label", "input", "button", "section", "fieldset",
154
- "menu", "article", "header", "time", "aside", "footer", "nobr", "wbr",
154
+ "pre", "menu", "article", "header", "time", "aside", "footer", "nobr", "wbr",
155
155
  "table", "tr", "td", "th", "tt", "thead", "tbody", "noscript", "select",
156
156
  "address", "center", "small"
157
157
  render_children(node, state)
@@ -165,8 +165,7 @@ private
165
165
  render_children(node, state)
166
166
  end
167
167
 
168
- when Oga::XML::Comment
169
- # skip it
168
+ when Oga::XML::Comment, Oga::XML::Cdata
170
169
 
171
170
  else
172
171
  raise "Unhandled Oga node type: #{node.class}"
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.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - epitron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-10 00:00:00.000000000 Z
11
+ date: 2020-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga
@@ -56,8 +56,7 @@ description: Easily implement an HTML renderer by creating a subclass and adding
56
56
  methods, similar to RedCarpet. (Examples are included for rendering HTML to ANSI
57
57
  and plain text.)
58
58
  email: chris@ill-logic.com
59
- executables:
60
- - html2ansi
59
+ executables: []
61
60
  extensions: []
62
61
  extra_rdoc_files:
63
62
  - README.md
@@ -69,7 +68,6 @@ files:
69
68
  - Rakefile
70
69
  - TODO.md
71
70
  - VERSION
72
- - bin/html2ansi
73
71
  - examples/ansi_renderer.rb
74
72
  - examples/debug_renderer.rb
75
73
  - examples/plain_text_renderer.rb
@@ -98,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
96
  - !ruby/object:Gem::Version
99
97
  version: '0'
100
98
  requirements: []
101
- rubygems_version: 3.0.3
99
+ rubygems_version: 3.1.2
102
100
  signing_key:
103
101
  specification_version: 4
104
102
  summary: HTML Renderer
data/bin/html2ansi DELETED
@@ -1,248 +0,0 @@
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
- def subscript(s)
73
- "[#{s}]"
74
- end
75
-
76
- public
77
-
78
- def normal_text(text)
79
- text
80
- end
81
-
82
- def underline(content)
83
- content.magenta.bold
84
- end
85
-
86
- def superscript(content)
87
- "^(#{content})"
88
- end
89
-
90
- def link(link, title, content)
91
- unless content&.[] /^Back /
92
- str = ""
93
- # str += "<15>#{content}</15>" if content
94
- str += content.white.bold if content
95
- if smash(link) != smash(content)
96
- # str += " <8>(</8><11>#{link}</11><8>)</8>"
97
- str += " #{"(".grey}#{link&.cyan&.bold}#{")".grey}"
98
- end
99
-
100
- str
101
- end
102
- end
103
-
104
- def anchor(name, title=nil, content=nil)
105
- result = "Anchor: ##{name}"
106
- result << " (#{title})" if title
107
- result << "\n"
108
- result << "#{content}\n" if content
109
- result
110
- end
111
-
112
- def image(link, title, content)
113
- link(link, nil, title)
114
- end
115
-
116
- def italic(text)
117
- text.yellow.bold
118
- end
119
-
120
- def block_code(code, language)
121
- language ||= :ruby
122
-
123
- language = language[1..-1] if language[0] == "." # strip leading "."
124
- language = :cpp if language == "C++"
125
-
126
- require 'coderay'
127
- "#{indent CodeRay.scan(code, language).term, 4}\n"
128
- end
129
-
130
- def block_quote(text)
131
- indent paragraph(text)
132
- end
133
-
134
- def codespan(code)
135
- code.cyan
136
- end
137
-
138
- def header(title, level, anchor=nil)
139
- bar = ("-"*(title.size+4)).grey
140
-
141
- title = case level
142
- when 1 then title.bold.yellow
143
- when 2 then title.bold.cyan
144
- when 3 then title.bold.blue
145
- else title.magenta
146
- end
147
-
148
- "#{bar}\n #{title}\n#{bar}\n\n"
149
- end
150
-
151
- def double_emphasis(text)
152
- text.bold.green
153
- end
154
-
155
- def emphasis(text)
156
- text.green
157
- end
158
-
159
- def linebreak
160
- "\n"
161
- end
162
-
163
- def paragraph(text)
164
- div(text) + "\n"
165
- end
166
-
167
- def div(text)
168
- "#{indented?(text) ? text : unwrap(text)}\n"
169
- end
170
-
171
- def list(content, list_type)
172
- case list_type
173
- when :ordered
174
- @counter = 0
175
- "#{content}\n"
176
- when :unordered
177
- "#{content}\n"
178
- end
179
- end
180
-
181
- def list_item(content, list_type)
182
- case list_type
183
- when :ordered
184
- @counter ||= 0
185
- @counter += 1
186
- # " <8>#{@counter}.</8> #{content.strip}\n".colorize
187
- " #{@counter.to_s.grey}. #{content.strip}\n"
188
- when :unordered
189
- # " <8>*</8> #{content.strip}\n".colorize
190
- " #{"*".grey} #{content.strip}\n"
191
- end
192
- end
193
-
194
- def definition_list(defs)
195
- defs.each do |dt, dd|
196
- puts "<15>#{dt}<7>:".colorize
197
- puts " #{dd}"
198
- puts
199
- end
200
- end
201
-
202
- def table(header, rows)
203
- if header
204
- table = Terminal::Table.new(headings: header, rows: rows)
205
- else
206
- table = Terminal::Table.new(rows: rows)
207
- end
208
- "#{table}\n\n"
209
- end
210
-
211
- def separator
212
- "_____________________________\n\n"
213
- end
214
-
215
- end
216
-
217
- #######################################################################################
218
-
219
- def render(stream, paged=false)
220
- output = ANSIRenderer.render(stream)
221
- if paged
222
- lesspipe { |less| less.puts output }
223
- else
224
- puts output
225
- end
226
- end
227
-
228
- #######################################################################################
229
-
230
- opts, args = ARGV.partition { |arg| arg[/^--?\w/] }
231
-
232
- if opts.include?("-h") or opts.include?("--help")
233
- puts "usage:"
234
- puts " html2ansi [options] <file.html>"
235
- puts " or"
236
- puts " curl http://host/path.html | html2ansi [options]"
237
- puts
238
- puts "options:"
239
- puts " -p Paged (redirect output to less)"
240
- puts
241
- else
242
- paged = opts.include?("-p") or opts.include?("--paged")
243
- if args.empty?
244
- render($stdin, paged)
245
- else
246
- args.each { |arg| render(open(arg), paged) }
247
- end
248
- end