burr 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/README.md +2 -2
- data/burr.gemspec +4 -4
- data/lib/burr/kramdown_ext/converter.rb +11 -7
- data/lib/burr/rouge_ext/html_formatter.rb +113 -0
- data/lib/burr/version.rb +1 -1
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f58fa5ed863368020b2943b77cae90a09bdc70ba
|
4
|
+
data.tar.gz: 17bb05a58a6e84a1bbcf7c84758bd3bd0611a084
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5313792ed6328c5190d8f725623f0fec04a3808d377a7360f6b89b9279da0f7b9e366b6ce512900949b902b420b260466cd08d22615bb910e68796ef1d92816e
|
7
|
+
data.tar.gz: dfbb7b83f60facec45fb897d44162161f942b18b2cb671fc9da275be74f7227d84f427cb25e87abeeda375e6d48ca6b8de59182b2e28f96db1ea87b3aca7f132
|
data/README.md
CHANGED
@@ -67,7 +67,7 @@ burr 是一个 Ruby gem,可以像其他 gem 一样安装。但是由于没有
|
|
67
67
|
在项目的 `Gemfile` 中加入以下代码:
|
68
68
|
|
69
69
|
```ruby
|
70
|
-
gem 'burr'
|
70
|
+
gem 'burr'
|
71
71
|
```
|
72
72
|
|
73
73
|
然后执行 `bundle` 命令安装。
|
@@ -159,7 +159,7 @@ kramdown 原生支持的代码块由 `~~~` 分隔,但我更习惯使用 GitHub
|
|
159
159
|
```
|
160
160
|
{:caption="Ruby 方法定义示例" file="/path/to/file.rb"}
|
161
161
|
|
162
|
-
代码高亮通过 [
|
162
|
+
代码高亮通过 [rouge](http://rubygems.org/gems/rouge) 实现。
|
163
163
|
|
164
164
|
### 图片题注
|
165
165
|
|
data/burr.gemspec
CHANGED
@@ -26,10 +26,10 @@ Gem::Specification.new do |s|
|
|
26
26
|
s.extra_rdoc_files = %w[README.md LICENSE.md]
|
27
27
|
|
28
28
|
s.add_runtime_dependency('nokogiri', '~> 1.6.0')
|
29
|
-
s.add_runtime_dependency('thor', '~> 0.
|
30
|
-
s.add_runtime_dependency('liquid', '~> 2.
|
31
|
-
s.add_runtime_dependency('kramdown', '~> 1.
|
32
|
-
s.add_runtime_dependency('
|
29
|
+
s.add_runtime_dependency('thor', '~> 0.19.0')
|
30
|
+
s.add_runtime_dependency('liquid', '~> 2.6.1')
|
31
|
+
s.add_runtime_dependency('kramdown', '~> 1.3.3')
|
32
|
+
s.add_runtime_dependency('rouge', '~> 1.3.3')
|
33
33
|
s.add_runtime_dependency('eeepub', '~> 0.8.1')
|
34
34
|
|
35
35
|
s.files = `git ls-files`.split($/)
|
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'burr/kramdown_ext/parser'
|
2
|
-
require '
|
2
|
+
require 'burr/rouge_ext/html_formatter'
|
3
3
|
|
4
4
|
module Kramdown
|
5
5
|
module Converter
|
@@ -27,15 +27,10 @@ module Kramdown
|
|
27
27
|
def convert_codeblock(el, indent)
|
28
28
|
attr = el.attr.dup
|
29
29
|
lang = extract_code_language!(attr)
|
30
|
-
lang = 'text' if lang.blank?
|
31
30
|
|
32
|
-
pyg_opts = {
|
33
|
-
:encoding => 'utf-8',
|
34
|
-
:cssclass => "highlight type-#{lang}"
|
35
|
-
}
|
36
31
|
caption = attr['caption']
|
37
32
|
file = attr['file']
|
38
|
-
code =
|
33
|
+
code = rouge_highlight(el.value, lang)
|
39
34
|
output = '<div class="codeblock'
|
40
35
|
output << ' has-caption' if caption
|
41
36
|
output << '">'
|
@@ -140,6 +135,15 @@ module Kramdown
|
|
140
135
|
"<div class=\"figure\">#{img_html}<p class=\"caption\">#{caption_html}</p></div>"
|
141
136
|
end
|
142
137
|
|
138
|
+
def rouge_highlight(text, lexer, &b)
|
139
|
+
lexer = ::Rouge::Lexer.find(lexer) unless lexer.respond_to? :lex
|
140
|
+
lexer = ::Rouge::Lexers::PlainText unless lexer
|
141
|
+
|
142
|
+
formatter = ::Rouge::Formatters::BHTML.new(css_class: "highlight type-#{lexer.tag}")
|
143
|
+
|
144
|
+
formatter.format(lexer.lex(text), &b)
|
145
|
+
end
|
146
|
+
|
143
147
|
end
|
144
148
|
end
|
145
149
|
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'rouge'
|
3
|
+
|
4
|
+
module Rouge
|
5
|
+
module Formatters
|
6
|
+
# Transforms a token stream into HTML output.
|
7
|
+
class BHTML < Formatter
|
8
|
+
tag 'bhtml'
|
9
|
+
|
10
|
+
# @option opts [String] :css_class ('highlight')
|
11
|
+
# @option opts [true/false] :line_numbers (false)
|
12
|
+
# @option opts [Rouge::CSSTheme] :inline_theme (nil)
|
13
|
+
# @option opts [true/false] :wrap (true)
|
14
|
+
#
|
15
|
+
# Initialize with options.
|
16
|
+
#
|
17
|
+
# If `:inline_theme` is given, then instead of rendering the
|
18
|
+
# tokens as <span> tags with CSS classes, the styles according to
|
19
|
+
# the given theme will be inlined in "style" attributes. This is
|
20
|
+
# useful for formats in which stylesheets are not available.
|
21
|
+
#
|
22
|
+
# Content will be wrapped in a tag
|
23
|
+
# with the given `:css_class` unless `:wrap` is set to `false`.
|
24
|
+
def initialize(opts={})
|
25
|
+
@css_class = opts.fetch(:css_class, 'highlight')
|
26
|
+
@line_numbers = opts.fetch(:line_numbers, false)
|
27
|
+
@inline_theme = opts.fetch(:inline_theme, nil)
|
28
|
+
@wrap = opts.fetch(:wrap, true)
|
29
|
+
end
|
30
|
+
|
31
|
+
# @yield the html output.
|
32
|
+
def stream(tokens, &b)
|
33
|
+
if @line_numbers
|
34
|
+
stream_tableized(tokens, &b)
|
35
|
+
else
|
36
|
+
stream_untableized(tokens, &b)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def stream_untableized(tokens, &b)
|
42
|
+
yield "<div class=#{@css_class.inspect}><pre>" if @wrap
|
43
|
+
tokens.each do |tok, val|
|
44
|
+
span(tok, val, &b)
|
45
|
+
end
|
46
|
+
yield "</pre></div>\n" if @wrap
|
47
|
+
end
|
48
|
+
|
49
|
+
def stream_tableized(tokens)
|
50
|
+
num_lines = 0
|
51
|
+
last_val = ''
|
52
|
+
formatted = ''
|
53
|
+
|
54
|
+
tokens.each do |tok, val|
|
55
|
+
last_val = val
|
56
|
+
num_lines += val.scan(/\n/).size
|
57
|
+
span(tok, val) { |str| formatted << str }
|
58
|
+
end
|
59
|
+
|
60
|
+
# add an extra line for non-newline-terminated strings
|
61
|
+
if last_val[-1] != "\n"
|
62
|
+
num_lines += 1
|
63
|
+
span(Token::Tokens::Text::Whitespace, "\n") { |str| formatted << str }
|
64
|
+
end
|
65
|
+
|
66
|
+
# generate a string of newline-separated line numbers for the gutter
|
67
|
+
numbers = num_lines.times.map do |x|
|
68
|
+
%<<pre class="lineno">#{x+1}</pre>>
|
69
|
+
end.join
|
70
|
+
|
71
|
+
yield "<div class=#{@css_class.inspect}>" if @wrap
|
72
|
+
yield '<table style="border-spacing: 0"><tbody><tr>'
|
73
|
+
|
74
|
+
# the "gl" class applies the style for Generic.Lineno
|
75
|
+
yield '<td class="gutter gl" style="text-align: right">'
|
76
|
+
yield numbers
|
77
|
+
yield '</td>'
|
78
|
+
|
79
|
+
yield '<td class="code">'
|
80
|
+
yield '<pre>'
|
81
|
+
yield formatted
|
82
|
+
yield '</pre>'
|
83
|
+
yield '</td>'
|
84
|
+
|
85
|
+
yield "</tr></tbody></table>\n"
|
86
|
+
yield "</div>\n" if @wrap
|
87
|
+
end
|
88
|
+
|
89
|
+
TABLE_FOR_ESCAPE_HTML = {
|
90
|
+
'&' => '&',
|
91
|
+
'<' => '<',
|
92
|
+
'>' => '>',
|
93
|
+
}
|
94
|
+
|
95
|
+
def span(tok, val)
|
96
|
+
val = val.gsub(/[&<>]/, TABLE_FOR_ESCAPE_HTML)
|
97
|
+
shortname = tok.shortname or raise "unknown token: #{tok.inspect} for #{val.inspect}"
|
98
|
+
|
99
|
+
if shortname.empty?
|
100
|
+
yield val
|
101
|
+
else
|
102
|
+
if @inline_theme
|
103
|
+
rules = @inline_theme.style_for(tok).rendered_rules
|
104
|
+
|
105
|
+
yield "<span style=\"#{rules.to_a.join(';')}\">#{val}</span>"
|
106
|
+
else
|
107
|
+
yield "<span class=\"#{shortname}\">#{val}</span>"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
data/lib/burr/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: burr
|
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
|
- Andor Chen
|
@@ -30,56 +30,56 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.19.0
|
34
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: 0.
|
40
|
+
version: 0.19.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: liquid
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 2.
|
47
|
+
version: 2.6.1
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 2.
|
54
|
+
version: 2.6.1
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: kramdown
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 1.
|
61
|
+
version: 1.3.3
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 1.
|
68
|
+
version: 1.3.3
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rouge
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 1.3.3
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 1.3.3
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: eeepub
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,6 +142,7 @@ files:
|
|
142
142
|
- lib/burr/plugins/parser_plugin.rb
|
143
143
|
- lib/burr/plugins/table.rb
|
144
144
|
- lib/burr/plugins/toc.rb
|
145
|
+
- lib/burr/rouge_ext/html_formatter.rb
|
145
146
|
- lib/burr/ruby_version_check.rb
|
146
147
|
- lib/burr/server.rb
|
147
148
|
- lib/burr/ui.rb
|