togglate 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c7a17708de8171ece04a35fe3cb48503252f276d
4
- data.tar.gz: 041375d65bd90df01849fdc07724880e125f0583
3
+ metadata.gz: a67edbbcb3fc78c147b5d1a686f35091fa974d3a
4
+ data.tar.gz: 5ff0bdc8a25fcf43b6662f426fcff2d3ebea10b5
5
5
  SHA512:
6
- metadata.gz: 426662553eede9a73a7b0e1cd895db309eb7a5c7f8cbc7e59d39922df5efde19ba4de05009cc01e476b7a5d27815631a7a679d9c05d3050d6a0cde0f73da3cd1
7
- data.tar.gz: 3c47ff9fdaef373c6988a4abe30803b1d872dfd18bd9710e074e91ce1888643ffce7f64c0a002f02c2da5c119e7dfb3e324c8b7fd561c2bfa9b0583d0c361295
6
+ metadata.gz: 7a5ca7027d328ff4f1e597c2f729f01ffde8129d884328829663dbfb3b72cfd6334b6bd0df08265c984e0e329004b4edc92ef50d73bb0d65827679911d9839bd
7
+ data.tar.gz: 3101a22687385fe4b7bf891a125d97c1a37a84de565a476f9a1649ee9b1df2c7ba394b77f955a7c876d61f4b60c0341355275ccd4ab3c77411d40e09731b7a65
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Togglate
2
2
 
3
- TODO: Write a gem description
3
+ Togglate is a tool for creating a base text of translation, in which original texts are embeded in a form of comments.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ See `togglate` command.
22
22
 
23
23
  ## Contributing
24
24
 
@@ -1,12 +1,9 @@
1
1
  class Togglate::BlockWrapper
2
2
  def initialize(text,
3
- space_re:/^\s*$/,
4
- chunk_exceptions:[/^```/],
5
- wrapper:%W([translation\ here]\n```original ```),
6
- wrap_exceptions:[/^```/, /^\s{4}/], **opts)
3
+ wrapper:%W([translation\ here]\n\n<!--original -->),
4
+ wrap_exceptions:[],
5
+ **opts)
7
6
  @text = text
8
- @space_re = space_re
9
- @chunk_exceptions = chunk_exceptions
10
7
  @wrapper = wrapper
11
8
  @wrap_exceptions = wrap_exceptions
12
9
  end
@@ -16,11 +13,11 @@ class Togglate::BlockWrapper
16
13
  end
17
14
 
18
15
  private
19
- def chunk_by_space
16
+ def chunk_by_space(block_tags:[/^```/], space_re:/^\s*$/)
20
17
  in_block = false
21
18
  @text.each_line.chunk do |line|
22
- in_block = !in_block if @chunk_exceptions.any? { |ex| line.match ex }
23
- !line.match(@space_re).nil? && !in_block
19
+ in_block = !in_block if block_tags.any? { |ex| line.match ex }
20
+ !line.match(space_re).nil? && !in_block
24
21
  end
25
22
  end
26
23
 
data/lib/togglate/cli.rb CHANGED
@@ -3,18 +3,21 @@ require "thor"
3
3
  module Togglate
4
4
  class CLI < Thor
5
5
  desc "create FILE", "Create a base file for translation from a original file"
6
- option :toggle_code, aliases:'-t', default:true
7
- option :target, type: :string
8
- option :show_text, aliases:'-s', type: :string
9
- option :hide_text, aliases:'-h', type: :string
10
- option :wrapper, type: :array
6
+ option :method, aliases:'-m', default:'hover', desc:"Select a display method: 'hover' or 'toggle'"
7
+ option :embed_code, aliases:'-e', default:true, type: :boolean, desc:"Enable code embeding to false"
8
+ option :toggle_link_text, type: :array, default:["*", "hide"]
9
+ option :code_block, aliases:'-c', default:false, type: :boolean, desc:"Enable code blocks not to be wrapped"
11
10
  def create(file)
12
11
  opts = options.inject({}) { |h, (k,v)| h[k.intern] = v; h } # symbolize keys
13
- if wrapper = options[:wrapper]
14
- wrapper.map! { |wr| wr.gsub(/\\n/, "\n") }
15
- opts.update(wrapper:wrapper)
16
- end
12
+ opts.update(wrap_exceptions:[/^```/, /^ {4}/]) if opts[:code_block]
17
13
  puts Togglate.create(file, opts)
18
14
  end
15
+
16
+ desc "version", "Show Togglate version"
17
+ def version
18
+ puts "Togglate #{Togglate::VERSION} (c) 2014 kyoendo"
19
+ end
20
+ map "-v" => :version
21
+
19
22
  end
20
23
  end
@@ -1,3 +1,3 @@
1
1
  module Togglate
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/togglate.rb CHANGED
@@ -6,9 +6,9 @@ module Togglate
6
6
  def self.create(file, opts={})
7
7
  text = File.read(file)
8
8
  wrapped = BlockWrapper.new(text, opts).run
9
- if opts[:toggle_code]
10
- code = toggle_code(opts)
11
- [wrapped, code].join("\n")
9
+ if opts[:embed_code]
10
+ code = append_code(opts[:method], opts)
11
+ "#{wrapped}\n#{code}"
12
12
  else
13
13
  wrapped
14
14
  end
@@ -17,26 +17,51 @@ module Togglate
17
17
  exit
18
18
  end
19
19
 
20
- def self.toggle_code(target:%($("pre[lang='original']")), show_text:"*", hide_text:"hide", **opts)
20
+ def self.append_code(method, opts)
21
+ send("#{method}_code", opts)
22
+ end
23
+
24
+ def self.toggle_code(name:'original', toggle_link_text:["*", "hide"], **opts)
25
+ <<-"CODE"
26
+ <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
27
+ <script>
28
+ $(function() {
29
+ $("*").contents().filter(function() {
30
+ return this.nodeType==8 && this.nodeValue.match(/^#{name}/);
31
+ }).each(function(i, e) {
32
+ var tooltips = e.nodeValue.replace(/^#{name} *[\\n\\r]|[\\n\\r]$/g, '');
33
+ var link = "<span><a href='#' onclick='javascript:return false;' class='toggleLink'>" + "#{toggle_link_text[0]}" + "</a></span>";
34
+ $(this).prev().append(link);
35
+ $(this).prev().after("<pre style='display:none'>"+ tooltips + "</pre>");
36
+ });
37
+
38
+ $('.toggleLink').click(
39
+ function() {
40
+ if ($(this).text()=="#{toggle_link_text[0]}") {
41
+ $(this).parent().parent().next('pre').slideDown(200);
42
+ $(this).text("#{toggle_link_text[1]}");
43
+ } else {
44
+ $(this).parent().parent().next('pre').slideUp(200);
45
+ $(this).text("#{toggle_link_text[0]}");
46
+ };
47
+ });
48
+ });
49
+ </script>
50
+ CODE
51
+ end
52
+
53
+ def self.hover_code(name:'original', **opts)
21
54
  <<-"CODE"
22
55
  <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
23
56
  <script>
24
- function createToggleLinks(target, showText, hideText) {
25
- var link = "<span><a href='#' onclick='javascript:return false;' class='toggleLink'>" + showText + "</a></span>";
26
- target.hide().prev().append(link);
27
- $('.toggleLink').click(
28
- function() {
29
- if ($(this).text()==showText) {
30
- $(this).parent().parent().next(target).slideDown(200);
31
- $(this).text(hideText);
32
- } else {
33
- $(this).parent().parent().next(target).slideUp(200);
34
- $(this).text(showText);
35
- };
57
+ $(function() {
58
+ $("*").contents().filter(function() {
59
+ return this.nodeType==8 && this.nodeValue.match(/^#{name}/);
60
+ }).each(function(i, e) {
61
+ var tooltips = e.nodeValue.replace(/^#{name}\s*[\\n\\r]|[\\n\\r]$/g, '');
62
+ $(this).prev().attr('title', tooltips);
36
63
  });
37
- }
38
- var element = #{target};
39
- createToggleLinks(element, "#{show_text}", "#{hide_text}");
64
+ });
40
65
  </script>
41
66
  CODE
42
67
  end
@@ -0,0 +1,12 @@
1
+ # Title
2
+
3
+ Title is a tool.
4
+
5
+ % ruby title.rb
6
+
7
+
8
+ ```ruby
9
+ def hello
10
+ puts :hello
11
+ end
12
+ ```
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
  require 'togglate'
3
+ require "stringio"
3
4
 
4
5
  class String
5
6
  def ~
@@ -7,3 +8,13 @@ class String
7
8
  gsub(/^ {#{margin}}/, '')
8
9
  end
9
10
  end
11
+
12
+ module Helpers
13
+ def source_root
14
+ File.join(File.dirname(__FILE__), 'fixtures')
15
+ end
16
+ end
17
+
18
+ RSpec.configure do |c|
19
+ c.include Helpers
20
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Togglate::CLI do
4
+ before do
5
+ $stdout, $stderr = StringIO.new, StringIO.new
6
+ @original_dir = Dir.pwd
7
+ Dir.chdir(source_root)
8
+ end
9
+
10
+ after do
11
+ $stdout, $stderr = STDOUT, STDERR
12
+ Dir.chdir(@original_dir)
13
+ end
14
+
15
+ describe "#create" do
16
+ context "wrap_exceptions option" do
17
+ it "wraps sentences" do
18
+ Togglate::CLI.start(['create', 'README.md'])
19
+ expect($stdout.string).to match(/<!--original\n# Title\n-->/)
20
+ end
21
+
22
+ it "wraps code blocks" do
23
+ Togglate::CLI.start(['create', 'README.md'])
24
+ expect($stdout.string).to match(/<!--original\n\s{4}% ruby title\.rb\n-->/)
25
+ end
26
+
27
+ it "wraps gfm code blocks" do
28
+ Togglate::CLI.start(['create', 'README.md'])
29
+ expect($stdout.string).to match(/<!--original.*```ruby.*```\n-->/m)
30
+ end
31
+
32
+ it "wraps sentences except code blocks" do
33
+ Togglate::CLI.start(['create', 'README.md', '--code-block'])
34
+ expect($stdout.string).to match(/^\n {4}% ruby title\.rb\n$/)
35
+ expect($stdout.string).to match(/^\n```ruby.*```\n$/m)
36
+ end
37
+ end
38
+
39
+ context "code and method option" do
40
+ it "adds hover code to the output" do
41
+ Togglate::CLI.start(['create', 'README.md'])
42
+ expect($stdout.string).to match(/<script.*nodeType==8.*<\/script>/m)
43
+ end
44
+
45
+ it "adds toggle code to the output" do
46
+ Togglate::CLI.start(['create', 'README.md', '--method=toggle'])
47
+ expect($stdout.string).to match(/<script.*slideDown.*<\/script>/m)
48
+ end
49
+
50
+ it "not adds code to the output" do
51
+ Togglate::CLI.start(['create', 'README.md', '--embed-code=false'])
52
+ expect($stdout.string).not_to match(/<script.*nodeType==8.*<\/script>/m)
53
+ expect($stdout.string).not_to match(/<script.*createToggle.*<\/script>/m)
54
+ end
55
+ end
56
+
57
+ context "toggle_link_text option" do
58
+ it "sets texts for toggle link" do
59
+ Togglate::CLI.start(['create', 'README.md', '-m=toggle', '--toggle-link-text', 'showme', 'closeme'])
60
+ expect($stdout.string).to match(/<script.*showme.*<\/script>/m)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -23,7 +23,7 @@ describe Togglate::BlockWrapper do
23
23
  text = "#title\n\n\ntext\n"
24
24
  wrapper = Togglate::BlockWrapper.new(text)
25
25
  chunks = wrapper.send(:chunk_by_space)
26
- exp = "<translation here>\n```original\n#title\n```\n\n\n<translation here>\n```original\ntext\n```\n"
26
+ exp = "[translation here]\n\n<!--original\n#title\n-->\n\n\n[translation here]\n\n<!--original\ntext\n-->\n"
27
27
  expect(wrapper.send(:wrap_with, chunks)).to eq exp
28
28
  end
29
29
  end
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.0.1
4
+ version: 0.0.3
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-09 00:00:00.000000000 Z
11
+ date: 2014-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -86,7 +86,9 @@ files:
86
86
  - lib/togglate/block_wrapper.rb
87
87
  - lib/togglate/cli.rb
88
88
  - lib/togglate/version.rb
89
+ - spec/fixtures/README.md
89
90
  - spec/spec_helper.rb
91
+ - spec/togglate_cli_spec.rb
90
92
  - spec/togglate_spec.rb
91
93
  - togglate.gemspec
92
94
  homepage: https://github.com/melborne/togglate
@@ -114,6 +116,8 @@ signing_key:
114
116
  specification_version: 4
115
117
  summary: Create base text for translation, in which original is togglable
116
118
  test_files:
119
+ - spec/fixtures/README.md
117
120
  - spec/spec_helper.rb
121
+ - spec/togglate_cli_spec.rb
118
122
  - spec/togglate_spec.rb
119
123
  has_rdoc: