agate 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -3
- data/lib/agate/formatter/html.rb +20 -0
- data/lib/agate/parser.rb +15 -8
- data/lib/agate/version.rb +1 -1
- data/lib/agate.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9a0b380e4f94a1f320a0feff2b2ca0a2e7f9437
|
4
|
+
data.tar.gz: 305eb3e8ecbc8e1d14938560a993f89e961f8ed9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e19ede1a3bb96df2ed7ce649b3c5afd29217b8299a4a06bf70b1360e64fbdc947fa393c088df85b4156dff792b01312c0a8de2c9e6683c7be0a13dff0ba7705e
|
7
|
+
data.tar.gz: d2da48e19e9456b1a93cfee32c062a0f2d7eaed40d33bbdf3113369855def415e2590d01d4fcc01d833f27d7ec3d9f3cadc5dca17e64169ec05a1bbf732231ad
|
data/README.md
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
# [Agate](http://en.wikipedia.org/w/index.php?title=Ruby_character&oldid=540994629#History)
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/agate.png)](http://badge.fury.io/rb/agate)
|
3
4
|
[![Build Status](https://travis-ci.org/jbhannah/agate.png?branch=master)](https://travis-ci.org/jbhannah/agate)
|
4
5
|
[![Dependency Status](https://gemnasium.com/jbhannah/agate.png)](https://gemnasium.com/jbhannah/agate)
|
5
6
|
[![Code Climate](https://codeclimate.com/github/jbhannah/agate.png)](https://codeclimate.com/github/jbhannah/agate)
|
6
7
|
[![Coverage Status](https://coveralls.io/repos/jbhannah/agate/badge.png?branch=master)](https://coveralls.io/r/jbhannah/agate)
|
7
8
|
|
8
9
|
Wrap ruby characters (e.g. furigana, Pinyin, Zhuyin) in text with the
|
9
|
-
[
|
10
|
+
[HTML5 `ruby` element][].
|
10
11
|
|
11
12
|
## Usage
|
12
13
|
|
@@ -26,11 +27,15 @@ results in
|
|
26
27
|
<ruby>勉<rp>【</rp><rt>べん</rt><rp>】</rp></ruby><ruby>強<rp>【</rp><rt>きょう</rt><rp>】</rp></ruby>します
|
27
28
|
```
|
28
29
|
|
29
|
-
|
30
|
+
which (in your browser) looks like
|
31
|
+
|
32
|
+
> <ruby>勉<rp>【</rp><rt>べん</rt><rp>】</rp></ruby><ruby>強<rp>【</rp><rt>きょう</rt><rp>】</rp></ruby>します
|
33
|
+
|
34
|
+
It's highly recommended to read the W3C's documentation on the [HTML5
|
30
35
|
`ruby` element][] to understand the usage of this element. There is a
|
31
36
|
[CSS workaround][] available that enables the display of ruby characters
|
32
37
|
in all modern browsers, but it's up to you to test all cases where you
|
33
38
|
intend to use the `ruby` element.
|
34
39
|
|
35
|
-
[
|
40
|
+
[HTML5 `ruby` element]: http://www.w3.org/TR/html5/text-level-semantics.html#the-ruby-element
|
36
41
|
[CSS workaround]: http://web.nickshanks.com/stylesheets/ruby.css
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Agate
|
2
|
+
module Formatter
|
3
|
+
# Formats text using HTML5 ruby tags, turning
|
4
|
+
#
|
5
|
+
# > 勉【べん】強【きょう】します
|
6
|
+
#
|
7
|
+
# into
|
8
|
+
#
|
9
|
+
# <ruby>勉<rp>【</rp><rt>べん</rt><rp>】</rp></ruby><ruby>強<rp>【</rp><rt>きょう</rt><rp>】</rp></ruby>します
|
10
|
+
#
|
11
|
+
# which can then be rendered by some web browsers or formatted
|
12
|
+
# using CSS.
|
13
|
+
class HTML
|
14
|
+
# Turns a regexp match object into a formatted string with ruby characters
|
15
|
+
def format(match)
|
16
|
+
"<ruby>#{match[1]}<rp>#{match[2]}</rp><rt>#{match[3]}</rt><rp>#{match[4]}</rp></ruby>"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/agate/parser.rb
CHANGED
@@ -2,15 +2,23 @@
|
|
2
2
|
module Agate
|
3
3
|
class Parser
|
4
4
|
# Default options
|
5
|
-
|
6
|
-
:delimiters => "【】"
|
5
|
+
DEFAULTS = {
|
6
|
+
:delimiters => "【】",
|
7
|
+
:formatter => :html
|
7
8
|
}
|
8
9
|
|
9
10
|
# Regexp reserved characters to escape when matching
|
10
|
-
|
11
|
+
RESERVED = ["(", ")", "[", "]", "{", "}", ".", ",", "+", "*"]
|
11
12
|
|
12
13
|
def initialize(options = {})
|
13
|
-
@options =
|
14
|
+
@options = DEFAULTS.merge(options)
|
15
|
+
|
16
|
+
@formatter = case @options[:formatter]
|
17
|
+
when :html
|
18
|
+
Agate::Formatter::HTML.new
|
19
|
+
else
|
20
|
+
Agate::Formatter::HTML.new
|
21
|
+
end
|
14
22
|
end
|
15
23
|
|
16
24
|
# Parse `text` and return it with ruby character markup
|
@@ -18,11 +26,10 @@ module Agate
|
|
18
26
|
first = @options[:delimiters].chars.first
|
19
27
|
last = @options[:delimiters].chars.last
|
20
28
|
|
21
|
-
first = /#{'\\' + first}/ if
|
22
|
-
last = /#{'\\' + last}/ if
|
29
|
+
first = /#{'\\' + first}/ if RESERVED.include? first
|
30
|
+
last = /#{'\\' + last}/ if RESERVED.include? last
|
23
31
|
|
24
|
-
text.gsub(/(\p{Han}+)(#{first})([\p{Hiragana}\p{Katakana}]+)(#{last})
|
25
|
-
'<ruby>\1<rp>\2</rp><rt>\3</rt><rp>\4</rp></ruby>')
|
32
|
+
text.gsub(/(\p{Han}+)(#{first})([\p{Hiragana}\p{Katakana}]+)(#{last})/) { |match| @formatter.format($~) }
|
26
33
|
end
|
27
34
|
end
|
28
35
|
end
|
data/lib/agate/version.rb
CHANGED
data/lib/agate.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: agate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesse B. Hannah
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- agate.gemspec
|
86
86
|
- bin/agate
|
87
87
|
- lib/agate.rb
|
88
|
+
- lib/agate/formatter/html.rb
|
88
89
|
- lib/agate/parser.rb
|
89
90
|
- lib/agate/version.rb
|
90
91
|
- spec/lib/parser_spec.rb
|