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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eda63f3e809d3e097848a34b3fc9cec35cbe6ac8
4
- data.tar.gz: e2548f805540a40a648ea918d7d94cba84ce965b
3
+ metadata.gz: c0e2b4338740e69cba0f1310886ed85dd2f4a655
4
+ data.tar.gz: 5a2cc87c8d717a13ddb5a79e97b04ffab3477ff9
5
5
  SHA512:
6
- metadata.gz: 1661c4901e2cf8c4f929dd7560a7da24f91284ce7647cdde9e3f1fb8c9955e2cb94d84c7c19be2c10900044216bc983a9ae6abe415e34048288b70403f502b20
7
- data.tar.gz: 68ccb1be9b3b9847b087d05c2afa3f0242eb8b1a80715f4f0d6902f90c1661ffee16e648b0a695bf63c5ae80c950f4b1cda937680c769242c50beeaf671e07b4
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
- a = Agate::Parser.new("勉【べん】強【きょう】します")
15
- a.parse
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
- def initialize(text)
5
- @text = text
6
- end
4
+ # Default options
5
+ @@defaults = {
6
+ :delimiters => "【】"
7
+ }
8
+
9
+ # Regexp reserved characters to escape when matching
10
+ @@reserved = ["(", ")", "[", "]", "{", "}", ".", ",", "+", "*"]
7
11
 
8
- def text
9
- @text
12
+ def initialize(options = {})
13
+ @options = @@defaults.merge(options)
10
14
  end
11
15
 
12
- def parse
13
- @parsed_text ||= @text.gsub(/(\p{Han}+)(【)([\p{Hiragana}\p{Katakana}]+)(】)/, '<ruby>\1<rp>\2</rp><rt>\3</rt><rp>\4</rp></ruby>')
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
@@ -1,3 +1,3 @@
1
1
  module Agate
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -2,15 +2,35 @@
2
2
  require "spec_helper"
3
3
 
4
4
  describe Agate::Parser do
5
- let!(:text) { "勉【べん】強【きょう】します" }
6
- let!(:agate) { Agate::Parser.new(text) }
7
- let!(:parsed_text) { "<ruby>勉<rp>【</rp><rt>べん</rt><rp>】</rp></ruby><ruby>強<rp>【</rp><rt>きょう</rt><rp>】</rp></ruby>します" }
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
- it "saves text to an instance variable" do
10
- agate.text.should eql(text)
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
- it "parses demarcated text and turns it into HTML" do
14
- agate.parse.should eql(parsed_text)
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
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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse B. Hannah