html-renderer 0.0.4 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gemspec +1 -0
- data/TODO.md +12 -0
- data/VERSION +1 -1
- data/lib/html-renderer.rb +5 -0
- data/lib/html-renderer/ansi.rb +182 -0
- data/lib/html-renderer/text.rb +72 -0
- metadata +20 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54c85f9f2a8247cdcbb871a115deaad908928b9fb11464fcf44264b4f1c42427
|
4
|
+
data.tar.gz: 1123629514b87e11b368f8e752e0ec7f0c31ec22b7e0d190ab9e67818f9b2ba9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b1b284109ca1625e37d4b2806c518dc3de53d66bd69feca6c48017ab5b3c3c9a9fd0fc0f01073d048df2abf5474252398c08e3e9437a8e9b6c3f3cb76690d41
|
7
|
+
data.tar.gz: e32329999296b0f1825f1e0a0ca683ae5504c16d4a07ed81b95a5386e425b31f8fab9ad4969fc219f024be5d77bac0a02a9ba5a583cb399a0ca13c680dce5828
|
data/.gemspec
CHANGED
data/TODO.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# TODO list
|
2
|
+
|
3
|
+
## Base class
|
4
|
+
|
5
|
+
* #render takes options (and passes them to subclasses)
|
6
|
+
* :debug option (hide parse errors unless 'true')
|
7
|
+
|
8
|
+
## ANSI renderer
|
9
|
+
|
10
|
+
* :wrap option (with optional terminal width)
|
11
|
+
* Better table renderer (with line-drawing)
|
12
|
+
* Follow `<table>` style attributes when drawing tables (eg: borders)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.6
|
data/lib/html-renderer.rb
CHANGED
@@ -0,0 +1,182 @@
|
|
1
|
+
# require 'html-renderer'
|
2
|
+
require 'ansi/mixin'
|
3
|
+
require 'terminal-table'
|
4
|
+
require 'coderay'
|
5
|
+
|
6
|
+
module HTMLRenderer::ANSIStrings
|
7
|
+
refine String do
|
8
|
+
include ANSI::Mixin
|
9
|
+
def grey; self.black.bold; end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class HTMLRenderer::ANSI < HTMLRenderer::Base
|
14
|
+
|
15
|
+
using HTMLRenderer::ANSIStrings
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def indented?(text)
|
20
|
+
indent_sizes = text.lines.map{ |line| if line =~ /^(\s+)/ then $1 else '' end }.map(&:size)
|
21
|
+
indent_sizes.all? {|dent| dent > 0 }
|
22
|
+
end
|
23
|
+
|
24
|
+
def unwrap(text)
|
25
|
+
return text unless indented? text
|
26
|
+
text.lines.to_a.map(&:strip).join ' '
|
27
|
+
end
|
28
|
+
|
29
|
+
def indent(text,amount=2)
|
30
|
+
text.lines.map{|line| " "*amount + line }.join
|
31
|
+
end
|
32
|
+
|
33
|
+
def smash(s)
|
34
|
+
s&.downcase&.scan(/\w+/)&.join
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
public
|
39
|
+
|
40
|
+
def normal_text(text)
|
41
|
+
text
|
42
|
+
end
|
43
|
+
|
44
|
+
def underline(content)
|
45
|
+
content.magenta.bold
|
46
|
+
end
|
47
|
+
|
48
|
+
def superscript(content)
|
49
|
+
"^(#{content})"
|
50
|
+
end
|
51
|
+
|
52
|
+
def link(link, title, content)
|
53
|
+
unless content&.[] /^Back /
|
54
|
+
str = ""
|
55
|
+
# str += "<15>#{content}</15>" if content
|
56
|
+
str += content.white.bold if content
|
57
|
+
if smash(link) != smash(content)
|
58
|
+
# str += " <8>(</8><11>#{link}</11><8>)</8>"
|
59
|
+
str += " #{"(".grey}#{link.cyan.bold}#{")".grey}"
|
60
|
+
end
|
61
|
+
|
62
|
+
str
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def anchor(name, title=nil, content=nil)
|
67
|
+
result = "Anchor: ##{name}"
|
68
|
+
result << " (#{title})" if title
|
69
|
+
result << "\n"
|
70
|
+
result << "#{content}\n" if content
|
71
|
+
result
|
72
|
+
end
|
73
|
+
|
74
|
+
def image(link, title, content)
|
75
|
+
link(link, nil, title)
|
76
|
+
end
|
77
|
+
|
78
|
+
def italic(text)
|
79
|
+
text.yellow.bold
|
80
|
+
end
|
81
|
+
|
82
|
+
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"
|
90
|
+
end
|
91
|
+
|
92
|
+
def block_quote(text)
|
93
|
+
indent paragraph(text)
|
94
|
+
end
|
95
|
+
|
96
|
+
def codespan(code)
|
97
|
+
code.cyan
|
98
|
+
end
|
99
|
+
|
100
|
+
def header(title, level, anchor=nil)
|
101
|
+
bar = ("-"*(title.size+4)).grey
|
102
|
+
|
103
|
+
title = case level
|
104
|
+
when 1 then title.bold.yellow
|
105
|
+
when 2 then title.bold.cyan
|
106
|
+
when 3 then title.bold.blue
|
107
|
+
else title.magenta
|
108
|
+
end
|
109
|
+
|
110
|
+
"#{bar}\n #{title}\n#{bar}\n\n"
|
111
|
+
end
|
112
|
+
|
113
|
+
def double_emphasis(text)
|
114
|
+
text.bold.green
|
115
|
+
end
|
116
|
+
|
117
|
+
def emphasis(text)
|
118
|
+
text.green
|
119
|
+
end
|
120
|
+
|
121
|
+
def linebreak
|
122
|
+
"\n"
|
123
|
+
end
|
124
|
+
|
125
|
+
def paragraph(text)
|
126
|
+
div(text) + "\n"
|
127
|
+
end
|
128
|
+
|
129
|
+
def div(text)
|
130
|
+
"#{indented?(text) ? text : unwrap(text)}\n"
|
131
|
+
end
|
132
|
+
|
133
|
+
def list(content, list_type)
|
134
|
+
case list_type
|
135
|
+
when :ordered
|
136
|
+
@counter = 0
|
137
|
+
"#{content}\n"
|
138
|
+
when :unordered
|
139
|
+
"#{content}\n"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def list_item(content, list_type)
|
144
|
+
case list_type
|
145
|
+
when :ordered
|
146
|
+
@counter ||= 0
|
147
|
+
@counter += 1
|
148
|
+
# " <8>#{@counter}.</8> #{content.strip}\n".colorize
|
149
|
+
" #{@counter.to_s.grey}. #{content.strip}\n"
|
150
|
+
when :unordered
|
151
|
+
# " <8>*</8> #{content.strip}\n".colorize
|
152
|
+
" #{"*".grey} #{content.strip}\n"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def definition_list(defs)
|
157
|
+
defs.each do |dt, dd|
|
158
|
+
puts "<15>#{dt}<7>:".colorize
|
159
|
+
puts " #{dd}"
|
160
|
+
puts
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def table(header, rows)
|
165
|
+
if header
|
166
|
+
table = Terminal::Table.new(headings: header, rows: rows)
|
167
|
+
else
|
168
|
+
table = Terminal::Table.new(rows: rows)
|
169
|
+
end
|
170
|
+
"#{table}\n\n"
|
171
|
+
end
|
172
|
+
|
173
|
+
def separator
|
174
|
+
"_____________________________\n\n"
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
if __FILE__ == $0
|
181
|
+
puts HTMLRenderer::ANSI.render(open(ARGV.first || "test.html"))
|
182
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# require 'html-renderer'
|
2
|
+
|
3
|
+
#
|
4
|
+
# Everything-stripping renderer.
|
5
|
+
#
|
6
|
+
class HTMLRenderer::Text < 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 HTMLRenderer::Text.render(open("test.html"))
|
72
|
+
end
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- epitron
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oga
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: term-ansicolor
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
41
55
|
description: Easily implement an HTML renderer by creating a subclass and adding some
|
42
56
|
methods, similar to RedCarpet. (Examples are included for rendering HTML to ANSI
|
43
57
|
and plain text.)
|
@@ -53,6 +67,7 @@ files:
|
|
53
67
|
- LICENSE
|
54
68
|
- README.md
|
55
69
|
- Rakefile
|
70
|
+
- TODO.md
|
56
71
|
- VERSION
|
57
72
|
- bin/html2ansi
|
58
73
|
- examples/ansi_renderer.rb
|
@@ -60,8 +75,10 @@ files:
|
|
60
75
|
- examples/plain_text_renderer.rb
|
61
76
|
- examples/test.html
|
62
77
|
- lib/html-renderer.rb
|
78
|
+
- lib/html-renderer/ansi.rb
|
63
79
|
- lib/html-renderer/base.rb
|
64
80
|
- lib/html-renderer/html_parser.rb
|
81
|
+
- lib/html-renderer/text.rb
|
65
82
|
homepage: http://github.com/epitron/html-renderer/
|
66
83
|
licenses:
|
67
84
|
- WTFPL
|
@@ -81,8 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
98
|
- !ruby/object:Gem::Version
|
82
99
|
version: '0'
|
83
100
|
requirements: []
|
84
|
-
|
85
|
-
rubygems_version: 2.7.7
|
101
|
+
rubygems_version: 3.0.3
|
86
102
|
signing_key:
|
87
103
|
specification_version: 4
|
88
104
|
summary: HTML Renderer
|