codes21 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/codes21 +28 -4
  3. data/lib/codes21.rb +15 -10
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59be62698687bfb3685a65c7249b1def4625f3c2
4
- data.tar.gz: 8ef2fe9f0d837e7df644c9ce73c6ca287f3fe7c4
3
+ metadata.gz: 555c03e7f89ac0070403ba3dbf63feabe0b00517
4
+ data.tar.gz: f7c2a11c2519b46dab69d540e5b3ba87089c7736
5
5
  SHA512:
6
- metadata.gz: 09edf309e6c207cd210fe73a71663c7191aa10665cae18aef1739e978cccea517abee37cdf50b0b6711da9bd26bd13f145aac4133a663a4e995b0a89b92c855e
7
- data.tar.gz: 0185fdba1a7ac70b93d9b22ddc3061fb6aecb5be477eb21e02ec42cdbbce78c0fd8aaab1afca6d38f0d4d99fd7b9942ab8babaebcb719ab2ce84faad1c910606
6
+ metadata.gz: 859345aca9cdac4a6968cfb2e99eafb633d197d9ea041b4c5671d4d616d86c134b9097beef0e0232422a79a24a71867ccedfa232cc3563cef18f6fd3658fd203
7
+ data.tar.gz: ec14c060a36f175971f7db0f7d2c767eafb8e9336f45766825a62f730cd581a964aafcf992aed15a78d5111682288e8704f157bc7ae4f6ba6bf4d6e1e5464dd2
data/bin/codes21 CHANGED
@@ -1,10 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'optparse'
3
4
  require_relative '../lib/codes21'
4
5
 
5
- if ARGV.count != 3
6
- puts "With the given suffixes merge multiple files code into one file."
7
- puts "usage: codes21 /path/to/project rb,js result.txt"
6
+ help = <<-HELP
7
+ With the given suffixes merge multiple files code into one file.
8
+ usage: codes21 /path/to/project rb,js result.txt [-i UTF-8] [-o UTF-8]
9
+ HELP
10
+
11
+ if ARGV.count < 3
12
+ puts help
8
13
  exit
9
14
  end
10
15
 
@@ -12,4 +17,23 @@ path = ARGV[0]
12
17
  suffixes = ARGV[1]
13
18
  output = ARGV[2]
14
19
 
15
- Codes21.run path, suffixes, output
20
+ input_coding = "UTF-8"
21
+ output_coding = "UTF-8"
22
+
23
+ OptionParser.new do |opts|
24
+ opts.banner = "usage: codes21 /path/to/project rb,js result.txt [-i=UTF-8] [-o=UTF-8]"
25
+ opts.on("-iENCODE", "Input file coding") do |coding|
26
+ input_coding = coding
27
+ end
28
+
29
+ opts.on("-oENCODE", "Output file coding") do |coding|
30
+ output_coding = coding
31
+ end
32
+
33
+ opts.on("-h") do
34
+ exit
35
+ end
36
+
37
+ end.parse!
38
+
39
+ Codes21.run path, suffixes, output, input_coding, output_coding
data/lib/codes21.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  class Codes21
2
- def get_codes(path, suffixes)
2
+ def get_codes(path, suffixes, coding="UTF-8")
3
3
  @items ||= []
4
4
  Dir.glob("#{path}/**/*.{#{suffixes}}").each do |file|
5
- title = file.split('/').last
5
+ title = file.split('/').last.force_encoding(coding)
6
6
  puts file
7
7
 
8
8
  puts '*' * 70
9
- codes = File.read(file)
9
+ codes = File.read(file).force_encoding(coding)
10
10
  # sb的软件著作权要求不能有空行
11
11
  codes = codes.squeeze("\n")
12
12
  puts codes
@@ -17,12 +17,17 @@ class Codes21
17
17
  @items
18
18
  end
19
19
 
20
- def write_result(result_file_path)
20
+ def write_result(result_file_path, coding="UTF-8")
21
21
  File.open(result_file_path, 'w') do |f|
22
22
  @items.each do |item|
23
- f.puts "File:" + item[:title]
24
- f.puts "Code:"
25
- f.puts item[:codes]
23
+ title = "File:" + item[:title]
24
+ code_label = "Code:"
25
+ codes = item[:codes]
26
+
27
+ content = title + "\n" + code_label + "\n" + codes + "\n"
28
+ content = content.squeeze("\n")
29
+
30
+ f.print content.encode(coding)
26
31
  end
27
32
  end
28
33
 
@@ -30,10 +35,10 @@ class Codes21
30
35
  puts "codes files count: #{@items.size}"
31
36
  end
32
37
 
33
- def self.run(path, suffixes, result_file_path)
38
+ def self.run(path, suffixes, result_file_path, input_coding="UTF-8", output_coding="UTF-8")
34
39
  codes21 = Codes21.new
35
- codes21.get_codes(path, suffixes)
36
- codes21.write_result(result_file_path)
40
+ codes21.get_codes(path, suffixes, input_coding)
41
+ codes21.write_result(result_file_path, output_coding)
37
42
  end
38
43
 
39
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codes21
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aston Fu