togglate 0.1.1 → 0.1.2

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: ae98309acb4d7d77bf8ee8310f90a1975882fd2f
4
- data.tar.gz: b1acea0eb9c4e0faf137065341509582c8cbfd05
3
+ metadata.gz: f3e882d0a7b6b8494adb72255373853b954d24bf
4
+ data.tar.gz: 2dfa813070babdae9203d5b53075a4eb788ab8dd
5
5
  SHA512:
6
- metadata.gz: 91fc7c49ecf28eda7d9f4c85a7a2a9482d0f40de30c8be3d2f7c53409e0a84b059fc25cc057cae9de752cc29830a1a0784ecbf51a77e55c4a1b0c7ac825a960b
7
- data.tar.gz: 793719ac0f7a4cb35c90b2f0bdeba1508d003e4f8a2d95e7f91ec0062b798411f526a079fd74769099c11d409eeacf699674cefdbf4bb4f53f961d0c1bd8947d
6
+ metadata.gz: 3e1034613d6c3c779ea614d79caf82c82c76fdbf34aa944574169d7dce51d68e621567802faeeee62d7f5b0bd6ca7bb640f2a83fbb3a875779d0fead09c82929
7
+ data.tar.gz: f72edcd08d81e63d99566ecb0e24ae7dfafe00dc6c5bcc24f86db20a4991d6016543422eb15370cd4d47c51595fa41336253848773dd1e198354f128ff07e821
data/lib/togglate/cli.rb CHANGED
@@ -10,10 +10,14 @@ module Togglate
10
10
  option :translate, aliases:'-t', type: :hash, default:{}, desc:"Embed machine translated text. ex.-t=to:ja"
11
11
  option :email, desc:"Passing a valid email extends a limit of Mymemory anonymous usage from 100 to 1000 requests/day"
12
12
  def create(file)
13
+ text = File.read(file)
13
14
  opts = symbolize_keys(options)
14
15
  opts.update(wrap_exceptions:[/^```/, /^ {4}/]) if opts[:code_block]
15
16
  opts.update(translate:nil) if opts[:translate].empty?
16
- puts Togglate.create(file, opts)
17
+ puts Togglate.create(text, opts)
18
+ rescue => e
19
+ STDERR.puts "something go wrong. #{e}"
20
+ exit
17
21
  end
18
22
 
19
23
  desc "append_code FILE", "Append a hover or toggle code to a FILE"
@@ -30,6 +34,18 @@ module Togglate
30
34
  exit
31
35
  end
32
36
 
37
+ desc "commentout FILE", "Extract commented contents from a FILE"
38
+ option :remains, aliases:'-r', default:false, type: :boolean, desc:"Output remaining text after extraction of comments"
39
+ option :tag, aliases:'-t', default:'original', desc:"Specify comment tag name"
40
+ def commentout(file)
41
+ text = File.read(file)
42
+ comments, remains = Togglate.commentout(text, tag:options['tag'])
43
+ puts options['remains'] ? remains : comments
44
+ rescue => e
45
+ STDERR.puts "something go wrong. #{e}"
46
+ exit
47
+ end
48
+
33
49
  desc "version", "Show Togglate version"
34
50
  def version
35
51
  puts "Togglate #{Togglate::VERSION} (c) 2014 kyoendo"
@@ -1,3 +1,3 @@
1
1
  module Togglate
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/togglate.rb CHANGED
@@ -6,26 +6,32 @@ require "togglate/block_wrapper"
6
6
  require "togglate/cli"
7
7
 
8
8
  module Togglate
9
- def self.create(file, opts={})
10
- text = File.read(file)
11
- wrapped = BlockWrapper.new(text, opts).run
12
- if opts[:embed_code]
13
- code = append_code(opts[:method], opts)
14
- "#{wrapped}\n#{code}"
15
- else
16
- wrapped
9
+ class << self
10
+ def create(text, opts={})
11
+ wrapped = BlockWrapper.new(text, opts).run
12
+ if opts[:embed_code]
13
+ code = append_code(opts[:method], opts)
14
+ "#{wrapped}\n#{code}"
15
+ else
16
+ wrapped
17
+ end
17
18
  end
18
- rescue => e
19
- STDERR.puts "something go wrong. #{e}"
20
- exit
21
- end
22
19
 
23
- def self.append_code(method, opts)
24
- send("#{method}_code", opts)
25
- end
20
+ def commentout(text, tag:'original')
21
+ comments = []
22
+ comment_re = /\n?^<!--#{tag}\n(.*?)^-->\n?/m
26
23
 
27
- def self.toggle_code(name:'original', toggle_link_text:["*", "hide"], **opts)
28
- <<-"CODE"
24
+ remains = text.gsub(comment_re) { |m| comments << $1; '' }
25
+ return comments*"\n", remains
26
+ end
27
+ alias :comment_out :commentout
28
+
29
+ def append_code(method, opts)
30
+ send("#{method}_code", opts)
31
+ end
32
+
33
+ def toggle_code(name:'original', toggle_link_text:["*", "hide"], **opts)
34
+ <<-"CODE"
29
35
  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
30
36
  <script>
31
37
  $(function() {
@@ -51,10 +57,10 @@ $(function() {
51
57
  });
52
58
  </script>
53
59
  CODE
54
- end
60
+ end
55
61
 
56
- def self.hover_code(name:'original', **opts)
57
- <<-"CODE"
62
+ def hover_code(name:'original', **opts)
63
+ <<-"CODE"
58
64
  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
59
65
  <script>
60
66
  $(function() {
@@ -67,5 +73,6 @@ $(function() {
67
73
  });
68
74
  </script>
69
75
  CODE
76
+ end
70
77
  end
71
78
  end
@@ -0,0 +1,31 @@
1
+ # タイトル
2
+
3
+ <!--original
4
+ # Title
5
+ -->
6
+
7
+ プログラミングは楽しい。
8
+
9
+ <!--original
10
+ Programming is fun.
11
+ -->
12
+
13
+ % ruby title.rb
14
+
15
+ <!--original
16
+ % ruby title.rb
17
+ -->
18
+
19
+ ```ruby
20
+ def hello
21
+ puts :hello
22
+ end
23
+ ```
24
+
25
+ <!--original
26
+ ```ruby
27
+ def hello
28
+ puts :hello
29
+ end
30
+ ```
31
+ -->
@@ -4,7 +4,6 @@ Programming is fun.
4
4
 
5
5
  % ruby title.rb
6
6
 
7
-
8
7
  ```ruby
9
8
  def hello
10
9
  puts :hello
@@ -80,4 +80,20 @@ describe Togglate::CLI do
80
80
  expect($stdout.string).to match(/<script.*showme.*<\/script>/m)
81
81
  end
82
82
  end
83
+
84
+ describe "#commentout" do
85
+ it "extract comments from given file" do
86
+ Togglate::CLI.start(['commentout', 'README.ja.md'])
87
+ expect($stdout.string).to match(/# Title.*Programming/m)
88
+ expect($stdout.string).not_to match(/# タイトル.*プログラミングは楽しい/m)
89
+ end
90
+
91
+ context "set remains option to true" do
92
+ it "outputs remaining text" do
93
+ Togglate::CLI.start(['commentout', 'README.ja.md', '--remains'])
94
+ expect($stdout.string).not_to match(/# Title.*Programming/m)
95
+ expect($stdout.string).to match(/# タイトル.*プログラミングは楽しい/m)
96
+ end
97
+ end
98
+ end
83
99
  end
@@ -4,6 +4,21 @@ describe Togglate do
4
4
  it 'should have a version number' do
5
5
  Togglate::VERSION.should_not be_nil
6
6
  end
7
+
8
+ describe ".commentout" do
9
+ before do
10
+ @original, @translated = begin
11
+ %w(README.md README.ja.md).map do |f|
12
+ File.read File.join(source_root, f)
13
+ end
14
+ end
15
+ end
16
+
17
+ it "extract comments from a text" do
18
+ comments, remains = Togglate.commentout(@translated)
19
+ expect(@original == comments).to be_true
20
+ end
21
+ end
7
22
  end
8
23
 
9
24
  describe Togglate::BlockWrapper do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: togglate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kyoendo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-25 00:00:00.000000000 Z
11
+ date: 2014-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -115,6 +115,7 @@ files:
115
115
  - lib/togglate/cli.rb
116
116
  - lib/togglate/core_ext.rb
117
117
  - lib/togglate/version.rb
118
+ - spec/fixtures/README.ja.md
118
119
  - spec/fixtures/README.md
119
120
  - spec/fixtures/translated_text.json
120
121
  - spec/spec_helper.rb
@@ -146,6 +147,7 @@ signing_key:
146
147
  specification_version: 4
147
148
  summary: Create base text for translation, in which original is togglable
148
149
  test_files:
150
+ - spec/fixtures/README.ja.md
149
151
  - spec/fixtures/README.md
150
152
  - spec/fixtures/translated_text.json
151
153
  - spec/spec_helper.rb