agate 0.1.0 → 0.2.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/parser.rb +19 -7
- data/lib/agate/version.rb +1 -1
- data/spec/lib/parser_spec.rb +27 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0e2b4338740e69cba0f1310886ed85dd2f4a655
|
4
|
+
data.tar.gz: 5a2cc87c8d717a13ddb5a79e97b04ffab3477ff9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e907a04663be0722250e0e0124d52c08449aa36f3cae7118535ec87c4b0bb93e84a7c66de599c2686cc789f4d90f5582171d135b054f6ed75102bb46e306102c
|
7
|
+
data.tar.gz: 94d95c408b798e117040ce2589cb0cc8d26cd4d76764558eda10e6d94be77c2527fa120b0fabde3ff12951145f898673a0626c4e0a081183516674d1ce5e8c44
|
data/README.md
CHANGED
@@ -11,14 +11,19 @@ Wrap ruby characters (e.g. furigana, Pinyin, Zhuyin) in text with the
|
|
11
11
|
## Usage
|
12
12
|
|
13
13
|
```ruby
|
14
|
-
|
15
|
-
|
14
|
+
options = {
|
15
|
+
# can be any single character or pair of characters which surround ruby characters in text to parse
|
16
|
+
:delimiters => "【】" # default delimiters
|
17
|
+
}
|
18
|
+
|
19
|
+
a = Agate::Parser.new(options) # call without arguments to use defaults
|
20
|
+
a.parse("勉【べん】強【きょう】します")
|
16
21
|
```
|
17
22
|
|
18
23
|
results in
|
19
24
|
|
20
25
|
```html
|
21
|
-
<ruby>勉<rt>べん</rt></ruby><ruby>強<rt>きょう</rt></ruby>します
|
26
|
+
<ruby>勉<rp>【</rp><rt>べん</rt><rp>】</rp></ruby><ruby>強<rp>【</rp><rt>きょう</rt><rp>】</rp></ruby>します
|
22
27
|
```
|
23
28
|
|
24
29
|
It's highly recommended to read the W3C's documentation on the [HTML
|
data/lib/agate/parser.rb
CHANGED
@@ -1,16 +1,28 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
module Agate
|
3
3
|
class Parser
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
# Default options
|
5
|
+
@@defaults = {
|
6
|
+
:delimiters => "【】"
|
7
|
+
}
|
8
|
+
|
9
|
+
# Regexp reserved characters to escape when matching
|
10
|
+
@@reserved = ["(", ")", "[", "]", "{", "}", ".", ",", "+", "*"]
|
7
11
|
|
8
|
-
def
|
9
|
-
@
|
12
|
+
def initialize(options = {})
|
13
|
+
@options = @@defaults.merge(options)
|
10
14
|
end
|
11
15
|
|
12
|
-
|
13
|
-
|
16
|
+
# Parse `text` and return it with ruby character markup
|
17
|
+
def parse(text)
|
18
|
+
first = @options[:delimiters].chars.first
|
19
|
+
last = @options[:delimiters].chars.last
|
20
|
+
|
21
|
+
first = /#{'\\' + first}/ if @@reserved.include? first
|
22
|
+
last = /#{'\\' + last}/ if @@reserved.include? last
|
23
|
+
|
24
|
+
text.gsub(/(\p{Han}+)(#{first})([\p{Hiragana}\p{Katakana}]+)(#{last})/,
|
25
|
+
'<ruby>\1<rp>\2</rp><rt>\3</rt><rp>\4</rp></ruby>')
|
14
26
|
end
|
15
27
|
end
|
16
28
|
end
|
data/lib/agate/version.rb
CHANGED
data/spec/lib/parser_spec.rb
CHANGED
@@ -2,15 +2,35 @@
|
|
2
2
|
require "spec_helper"
|
3
3
|
|
4
4
|
describe Agate::Parser do
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
context "with defaults" do
|
6
|
+
let(:agate) { Agate::Parser.new }
|
7
|
+
let(:text) { "勉【べん】強【きょう】します" }
|
8
|
+
let(:parsed_text) { "<ruby>勉<rp>【</rp><rt>べん</rt><rp>】</rp></ruby><ruby>強<rp>【</rp><rt>きょう</rt><rp>】</rp></ruby>します" }
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
it "parses delimited text and turns it into HTML" do
|
11
|
+
agate.parse(text).should eql(parsed_text)
|
12
|
+
end
|
11
13
|
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
+
context "with custom delimiters" do
|
16
|
+
context "from regexp-reserved character list" do
|
17
|
+
let(:agate) { Agate::Parser.new(:delimiters => "()") }
|
18
|
+
let(:text) { "勉(べん)強(きょう)します" }
|
19
|
+
let(:parsed_text) { "<ruby>勉<rp>(</rp><rt>べん</rt><rp>)</rp></ruby><ruby>強<rp>(</rp><rt>きょう</rt><rp>)</rp></ruby>します" }
|
20
|
+
|
21
|
+
it "parses delimited text and turns it into HTML" do
|
22
|
+
agate.parse(text).should eql(parsed_text)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "arbitrary" do
|
27
|
+
let(:agate) { Agate::Parser.new(:delimiters => "@") }
|
28
|
+
let(:text) { "勉@べん@強@きょう@します" }
|
29
|
+
let(:parsed_text) { "<ruby>勉<rp>@</rp><rt>べん</rt><rp>@</rp></ruby><ruby>強<rp>@</rp><rt>きょう</rt><rp>@</rp></ruby>します" }
|
30
|
+
|
31
|
+
it "parses delimited text and turns it into HTML" do
|
32
|
+
agate.parse(text).should eql(parsed_text)
|
33
|
+
end
|
34
|
+
end
|
15
35
|
end
|
16
36
|
end
|