html-renderer 0.1.0 → 0.1.3

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: ff38e106acb336902c8cfbfec5dff664be97cd9363741a610fc9ffe619d9bd02
4
- data.tar.gz: d3a8b5ce270e4229d7821062696df741765f0911ba2f428dbc705ec6611b1322
3
+ metadata.gz: d3c17a79e5d484dfafbdf2161683d28c8ee9d24b3ec099c7e392236fe167f547
4
+ data.tar.gz: 4c0cc61cc44691ab2e35b2c289f352fe17aec1d499cd01d86b6a01f4a3a9dab6
5
5
  SHA512:
6
- metadata.gz: 5d370198a4ae836fb62b37030cba865d00d08cfd87c5bf37ea24b26e1c34be827bf36c2f5f3f10333d9ad81e1f2bcc3559491025b80c8fa14a08cd073d1f4e59
7
- data.tar.gz: cd0d53f4d19c32073d519d83b3023aba9c5228c8ecac590943d9a2364c2092e201f49aff59fbfb1a2dfc50616b4f27ed697e470d5377454aec170f13d50f37af
6
+ metadata.gz: 52f7781a488c72b42398dad8a8a10393a3ccc1eca7147696e94398d28633d57956133a05c026364c8125b5dfcdf333a6772ec44ee786ced02ceb130e2c6c1ce2
7
+ data.tar.gz: 228891adf55ef0e77c958399071c0eb2a4acbc2620e3681aa59b65b56bd47dc308e5cee5b968043603388cd92e44443b7906acf93963c69513d94cceafa3f0b4
data/.gemspec CHANGED
@@ -14,8 +14,8 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.files = `git ls`.lines.map(&:strip)
16
16
  s.extra_rdoc_files = ["README.md", "LICENSE"]
17
-
18
- s.add_dependency "oga", "~> 2"
17
+
18
+ s.add_dependency "oga"
19
19
  s.add_dependency "terminal-table", "~> 1.8"
20
20
  s.add_dependency "term-ansicolor", "~> 1.7"
21
21
  end
@@ -0,0 +1 @@
1
+ /pkg/
data/README.md CHANGED
@@ -2,11 +2,29 @@
2
2
 
3
3
  ## Overview
4
4
 
5
- Allows you to subclass `HTMLRenderer::Base`, add some methods, and then parse HTML to generate custom output.
5
+ An extensible HTML renderer.
6
6
 
7
- (Uses a similar API to the [RedCarpet](https://github.com/vmg/redcarpet) gem.)
7
+ Comes with two built-in renderers:
8
+ * `HTMLRenderer::ANSI` (outputs colored text to the termnial)
9
+ * `HTMLRenderer::Text` (outputs plain text)
8
10
 
9
- ## Examples
11
+ ## Usage
10
12
 
11
- * [HTML to Plain Text Renderer](https://github.com/epitron/html-renderer/blob/master/examples/plain_text_renderer.rb)
12
- * [HTML to ANSI Renderer](https://github.com/epitron/html-renderer/blob/master/examples/ansi_renderer.rb)
13
+ Render to ANSI:
14
+ ```
15
+ ansi_text = HTMLRenderer::ANSI.render("<b>hello html</b>")
16
+ ansi_text = HTMLRenderer::ANSI.render(open("file.html"))
17
+ ```
18
+
19
+ Render to plain text:
20
+ ```
21
+ plain_text = HTMLRenderer::Text.render(open("file.html"))
22
+ ```
23
+
24
+ ## Extending it
25
+
26
+ The API design uses the same philosophy as [RedCarpet](https://github.com/vmg/redcarpet).
27
+
28
+ To create a new renderer, subclass `HTMLRenderer::Base`, then add a method to handle each type of element. Whatever the method returns is output by the renderer.
29
+
30
+ Example renderer: [HTMLRenderer::ANSI](https://github.com/epitron/html-renderer/blob/master/lib/html-renderer/ansi.rb)
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ task :build do
7
7
  system "mkdir pkg/" unless File.directory? "pkg"
8
8
  system "mv #{gemfile} pkg/"
9
9
  end
10
-
10
+
11
11
  task :release => :build do
12
12
  system "gem push pkg/#{gemfile}"
13
13
  end
@@ -17,3 +17,9 @@ task :gem => :build
17
17
  task :install => :build do
18
18
  system "gem install pkg/#{gemfile}"
19
19
  end
20
+
21
+ task :test do
22
+ Dir.chdir "test"
23
+ $: << "../lib"
24
+ load "test.rb"
25
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.3
@@ -1,15 +1,21 @@
1
- # require 'html-renderer'
1
+ require 'html-renderer'
2
2
  require 'ansi/mixin'
3
3
  require 'terminal-table'
4
- require 'coderay'
4
+
5
+ ###########################################################################
6
+
7
+ module ANSI::Mixin
8
+ def grey; ANSI::Code.bold { ANSI::Code.black { to_s } }; end
9
+ end
5
10
 
6
11
  module HTMLRenderer::ANSIStrings
7
12
  refine String do
8
13
  include ANSI::Mixin
9
- def grey; self.black.bold; end
10
14
  end
11
15
  end
12
16
 
17
+ ###########################################################################
18
+
13
19
  class HTMLRenderer::ANSI < HTMLRenderer::Base
14
20
 
15
21
  using HTMLRenderer::ANSIStrings
@@ -34,6 +40,9 @@ private
34
40
  s&.downcase&.scan(/\w+/)&.join
35
41
  end
36
42
 
43
+ def subscript(s)
44
+ "[#{s}]"
45
+ end
37
46
 
38
47
  public
39
48
 
@@ -80,13 +89,7 @@ public
80
89
  end
81
90
 
82
91
  def block_code(code, language)
83
- language ||= :ruby
84
-
85
- language = language[1..-1] if language[0] == "." # strip leading "."
86
- language = :cpp if language == "C++"
87
-
88
- require 'coderay'
89
- "#{indent CodeRay.scan(code, language).term, 4}\n"
92
+ code.bold.cyan + "\n"
90
93
  end
91
94
 
92
95
  def block_quote(text)
@@ -176,6 +179,7 @@ public
176
179
 
177
180
  end
178
181
 
182
+ ###########################################################################
179
183
 
180
184
  if __FILE__ == $0
181
185
  puts HTMLRenderer::ANSI.render(open(ARGV.first || "test.html"))
@@ -1,30 +1,25 @@
1
1
  ######################################################################################
2
2
  require 'oga'
3
3
  ######################################################################################
4
- #
5
- # TODOs:
6
- # - Streaming output (yield every paragraph/div/header)
7
- # - Embed into 'c' tool (for rendering raw HTML blocks)
8
- #
9
- ######################################################################################
10
-
11
- class String
12
4
 
13
- def tighten
14
- gsub(/\s+/, ' ').strip
15
- end
5
+ module HTMLRenderer
16
6
 
17
- def blank?; !!self[/[^\s]/]; end
7
+ module STDLIBRefinements
8
+ refine String do
9
+ def tighten
10
+ gsub(/\s+/, ' ').strip
11
+ end
18
12
 
19
- end
13
+ def blank?; !!self[/[^\s]/]; end
14
+ end
20
15
 
21
- class NilClass
22
- def blank?; true; end
23
- end
16
+ refine NilClass do
17
+ def blank?; true; end
18
+ end
19
+ end
24
20
 
25
- ######################################################################################
21
+ using STDLIBRefinements
26
22
 
27
- module HTMLRenderer
28
23
 
29
24
  class State
30
25
  attr_accessor :list_order
@@ -72,7 +67,9 @@ private
72
67
  name = node["name"]
73
68
  content = render_children(node, state)
74
69
 
75
- if name and not url
70
+ if title.blank? and url.blank? and name.blank?
71
+ content
72
+ elsif name and not url
76
73
  anchor(name, title, content)
77
74
  else
78
75
  link(url, title, content)
@@ -157,8 +154,9 @@ private
157
154
  render_children(node, state)
158
155
 
159
156
  when "head", "script", "link", "style"
160
- # skip it
161
-
157
+ #
158
+ # don't render anything
159
+ #
162
160
  else
163
161
  # raise "Unrecognized HTML tag: #{node.name} -> #{node.inspect}"
164
162
  $stderr.puts "Unrecognized HTML tag: #{node.name} -> #{node.inspect}"
@@ -0,0 +1,9 @@
1
+ require 'html-renderer'
2
+
3
+ class HTMLRenderer::DebugRenderer < HTMLRenderer::Base
4
+
5
+ def method_missing(meth, *args)
6
+ "#{meth}(#{args})"
7
+ end
8
+
9
+ end
@@ -1,9 +1,10 @@
1
- # require 'html-renderer'
1
+ require 'html-renderer'
2
2
 
3
3
  #
4
- # Everything-stripping renderer.
4
+ # Strips out everything but the plain text.
5
5
  #
6
6
  class HTMLRenderer::Text < HTMLRenderer::Base
7
+
7
8
  # Methods where the first argument is the text content
8
9
  [
9
10
  # block-level calls
@@ -65,8 +66,3 @@ class HTMLRenderer::Text < HTMLRenderer::Base
65
66
  content + "\t"
66
67
  end
67
68
  end
68
-
69
-
70
- if __FILE__ == $0
71
- puts HTMLRenderer::Text.render(open("test.html"))
72
- end
File without changes
@@ -0,0 +1,15 @@
1
+ require 'html-renderer'
2
+ require 'html-renderer/debug'
3
+
4
+ [
5
+ HTMLRenderer::ANSI,
6
+ HTMLRenderer::Text,
7
+ HTMLRenderer::DebugRenderer,
8
+ ].each do |renderer|
9
+ puts
10
+ puts "=== #{renderer} =============================="
11
+ puts
12
+ puts renderer.render(open("test.html"))
13
+ puts
14
+ puts
15
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html-renderer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - epitron
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-02 00:00:00.000000000 Z
11
+ date: 2020-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '2'
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '2'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: terminal-table
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -63,25 +63,25 @@ extra_rdoc_files:
63
63
  - LICENSE
64
64
  files:
65
65
  - ".gemspec"
66
+ - ".gitignore"
66
67
  - LICENSE
67
68
  - README.md
68
69
  - Rakefile
69
70
  - TODO.md
70
71
  - VERSION
71
- - examples/ansi_renderer.rb
72
- - examples/debug_renderer.rb
73
- - examples/plain_text_renderer.rb
74
- - examples/test.html
75
72
  - lib/html-renderer.rb
76
73
  - lib/html-renderer/ansi.rb
77
74
  - lib/html-renderer/base.rb
75
+ - lib/html-renderer/debug.rb
78
76
  - lib/html-renderer/html_parser.rb
79
77
  - lib/html-renderer/text.rb
78
+ - test/test.html
79
+ - test/test.rb
80
80
  homepage: http://github.com/epitron/html-renderer/
81
81
  licenses:
82
82
  - WTFPL
83
83
  metadata: {}
84
- post_install_message:
84
+ post_install_message:
85
85
  rdoc_options: []
86
86
  require_paths:
87
87
  - lib
@@ -96,8 +96,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  requirements: []
99
- rubygems_version: 3.1.2
100
- signing_key:
99
+ rubygems_version: 3.1.3
100
+ signing_key:
101
101
  specification_version: 4
102
102
  summary: HTML Renderer
103
103
  test_files: []
@@ -1,180 +0,0 @@
1
- require 'html-renderer'
2
- require 'ansi/mixin'
3
- require 'terminal-table'
4
- require 'coderay'
5
-
6
- class String
7
- include ANSI::Mixin
8
-
9
- def grey; self.black.bold; end
10
- end
11
-
12
-
13
- class ANSIRenderer < HTMLRenderer::Base
14
-
15
- private
16
-
17
- def indented?(text)
18
- indent_sizes = text.lines.map{ |line| if line =~ /^(\s+)/ then $1 else '' end }.map(&:size)
19
- indent_sizes.all? {|dent| dent > 0 }
20
- end
21
-
22
- def unwrap(text)
23
- return text unless indented? text
24
- text.lines.to_a.map(&:strip).join ' '
25
- end
26
-
27
- def indent(text,amount=2)
28
- text.lines.map{|line| " "*amount + line }.join
29
- end
30
-
31
- def smash(s)
32
- s&.downcase&.scan(/\w+/)&.join
33
- end
34
-
35
-
36
- public
37
-
38
- def normal_text(text)
39
- text
40
- end
41
-
42
- def underline(content)
43
- content.magenta.bold
44
- end
45
-
46
- def superscript(content)
47
- "^(#{content})"
48
- end
49
-
50
- def link(link, title, content)
51
- unless content&.[] /^Back /
52
- str = ""
53
- # str += "<15>#{content}</15>" if content
54
- str += content.white.bold if content
55
- if smash(link) != smash(content)
56
- # str += " <8>(</8><11>#{link}</11><8>)</8>"
57
- str += " #{"(".grey}#{link.cyan.bold}#{")".grey}"
58
- end
59
-
60
- str
61
- end
62
- end
63
-
64
- def anchor(name, title=nil, content=nil)
65
- result = "Anchor: ##{name}"
66
- result << " (#{title})" if title
67
- result << "\n"
68
- result << "#{content}\n" if content
69
- result
70
- end
71
-
72
- def image(link, title, content)
73
- link(link, nil, title)
74
- end
75
-
76
- def italic(text)
77
- text.yellow.bold
78
- end
79
-
80
- def block_code(code, language)
81
- language ||= :ruby
82
-
83
- language = language[1..-1] if language[0] == "." # strip leading "."
84
- language = :cpp if language == "C++"
85
-
86
- require 'coderay'
87
- "#{indent CodeRay.scan(code, language).term, 4}\n"
88
- end
89
-
90
- def block_quote(text)
91
- indent paragraph(text)
92
- end
93
-
94
- def codespan(code)
95
- code.cyan
96
- end
97
-
98
- def header(title, level, anchor=nil)
99
- bar = ("-"*(title.size+4)).grey
100
-
101
- title = case level
102
- when 1 then title.bold.yellow
103
- when 2 then title.bold.cyan
104
- when 3 then title.bold.blue
105
- else title.magenta
106
- end
107
-
108
- "#{bar}\n #{title}\n#{bar}\n\n"
109
- end
110
-
111
- def double_emphasis(text)
112
- text.bold.green
113
- end
114
-
115
- def emphasis(text)
116
- text.green
117
- end
118
-
119
- def linebreak
120
- "\n"
121
- end
122
-
123
- def paragraph(text)
124
- div(text) + "\n"
125
- end
126
-
127
- def div(text)
128
- "#{indented?(text) ? text : unwrap(text)}\n"
129
- end
130
-
131
- def list(content, list_type)
132
- case list_type
133
- when :ordered
134
- @counter = 0
135
- "#{content}\n"
136
- when :unordered
137
- "#{content}\n"
138
- end
139
- end
140
-
141
- def list_item(content, list_type)
142
- case list_type
143
- when :ordered
144
- @counter ||= 0
145
- @counter += 1
146
- # " <8>#{@counter}.</8> #{content.strip}\n".colorize
147
- " #{@counter.to_s.grey}. #{content.strip}\n"
148
- when :unordered
149
- # " <8>*</8> #{content.strip}\n".colorize
150
- " #{"*".grey} #{content.strip}\n"
151
- end
152
- end
153
-
154
- def definition_list(defs)
155
- defs.each do |dt, dd|
156
- puts "<15>#{dt}<7>:".colorize
157
- puts " #{dd}"
158
- puts
159
- end
160
- end
161
-
162
- def table(header, rows)
163
- if header
164
- table = Terminal::Table.new(headings: header, rows: rows)
165
- else
166
- table = Terminal::Table.new(rows: rows)
167
- end
168
- "#{table}\n\n"
169
- end
170
-
171
- def separator
172
- "_____________________________\n\n"
173
- end
174
-
175
- end
176
-
177
-
178
- if __FILE__ == $0
179
- puts ANSIRenderer.render(open(ARGV.first || "test.html"))
180
- end
@@ -1,14 +0,0 @@
1
- require 'html-renderer'
2
-
3
- class DebugRenderer < HTMLRenderer::Base
4
-
5
- def method_missing(meth, *args)
6
- "#{meth}(#{args})"
7
- end
8
-
9
- end
10
-
11
-
12
- if __FILE__ == $0
13
- puts DebugRenderer.render(open("test.html"))
14
- end
@@ -1,72 +0,0 @@
1
- require 'html-renderer'
2
-
3
- #
4
- # Everything-stripping renderer.
5
- #
6
- class PlainTextRenderer < HTMLRenderer::Base
7
- # Methods where the first argument is the text content
8
- [
9
- # block-level calls
10
- :block_code, :block_quote,
11
- :block_html, :list, :list_item,
12
-
13
- # span-level calls
14
- :autolink, :codespan, :double_emphasis,
15
- :emphasis, :underline, :raw_html,
16
- :triple_emphasis, :strikethrough,
17
- :superscript, :highlight,
18
-
19
- # footnotes
20
- :footnotes, :footnote_def, :footnote_ref,
21
-
22
- # low level rendering
23
- :entity, :normal_text
24
- ].each do |method|
25
- define_method method do |*args|
26
- args.first
27
- end
28
- end
29
-
30
- # Other methods where we don't return only a specific argument
31
- def link(link, title, content)
32
- "#{content} (#{link})"
33
- end
34
-
35
- def image(link, title, content)
36
- content &&= content + " "
37
- "#{content}#{link}"
38
- end
39
-
40
- def div(text)
41
- text + "\n"
42
- end
43
-
44
- def paragraph(text)
45
- div(text) + "\n"
46
- end
47
-
48
- def separator
49
- "______________________\n\n"
50
- end
51
-
52
- def header(text, header_level)
53
- text + "\n"
54
- end
55
-
56
- def table(header, body)
57
- "#{header}#{body}"
58
- end
59
-
60
- def table_row(content)
61
- content + "\n"
62
- end
63
-
64
- def table_cell(content, alignment)
65
- content + "\t"
66
- end
67
- end
68
-
69
-
70
- if __FILE__ == $0
71
- puts PlainTextRenderer.render(open("test.html"))
72
- end