pnote_client 2.2.5 → 2.3.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
  SHA256:
3
- metadata.gz: 60c919494ece6eb84dca42ee0ba300980294f031d403682dc250c369397b12ec
4
- data.tar.gz: e6e5f9786206a3c65136089529878a976df0180e83873c19db55590b8bac164e
3
+ metadata.gz: 5f55161957eb41b02e736937e17854bb20f353f5d67296aac4c3d71ba720462b
4
+ data.tar.gz: '05397c7bfa6567dbde1ba88d5cb086143be08a590a9f79f8bf0dc4410d9089d4'
5
5
  SHA512:
6
- metadata.gz: 0de7f247dddb1ffb5f0267354fba0543c5a347c99827cac16f2cfa659f0222c50e5e4cbc897849d5b8c6e367d568de35e4e1dda86b5bd53e273b56ca7c4ee7da
7
- data.tar.gz: 74caa57768f34f3e00248908de7e35271685412696a341859e47bfb5725ad828617b946ff165ca4979b970b68a887335e3a78dcc04fb869005ba35762a9e7321
6
+ metadata.gz: c714b9c5cf37cd7dd00a75c97154a8ba1ccfdc46d1bba1e85e7f651abe3f329910ff07bcc9d2dab5abe0b7dc39996b2cf48137dfa55c41f1d71cd4a6e28b8f78
7
+ data.tar.gz: 18198bc923ee330a8289ba21a79f238ac6b844ecb695f540fc666f0b03c517f5705d25b28dfa323769b8cf96971366a22ac09654440d1a7c9668b223c88c36d2
data/exe/pnote_clean ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+ ## Private Note 파일(.hml)에서 필요없는 텍스트들을 삭제시켜줍니다.
3
+ require 'optparse'
4
+ require 'fileutils'
5
+
6
+ def read_file(filepath)
7
+ f = File.open(filepath, 'r')
8
+ dir_path = File.expand_path(File.dirname(f))
9
+ filename = File.basename(f, File.extname(f))
10
+ content = f.read
11
+ f.close
12
+ return filename, dir_path, content
13
+ end
14
+
15
+ def write_file(content, filepath)
16
+ f = File.open(filepath, 'w')
17
+ f.write(content)
18
+ f.close
19
+ end
20
+
21
+ options = {}
22
+ optparser = OptionParser.new do |opt|
23
+ opt.banner = 'Usage: pnote_clean [options]'
24
+
25
+ opt.on('-f', '--file [FILEPATH]', String, 'Input file path(.hml)') do |filepath|
26
+ options[:input_filepath] = filepath
27
+ end
28
+ end
29
+ optparser.parse!
30
+
31
+ if options[:input_filepath].nil?
32
+ abort(optparser.help)
33
+ end
34
+
35
+ filename, input_dir_path, hml_content = read_file(options[:input_filepath])
36
+ output_dirpath = File.join(input_dir_path, "pnote_clean", "#{filename}_output")
37
+ FileUtils.mkdir_p(output_dirpath)
38
+ output_filepath = File.join(output_dirpath, "#{filename}.hml")
39
+
40
+ junk_regex = %r(from\s*=+(?:족보닷컴.*?)=+)
41
+ clean_content = hml_content.gsub(junk_regex, '')
42
+ write_file(clean_content, output_filepath)
43
+
44
+ before_bytes = hml_content.length
45
+ before_kb = (before_bytes / 1024.0).round(2)
46
+ before_mb = (before_bytes / 1024.0 / 1024.0).round(2)
47
+
48
+ after_bytes = clean_content.length
49
+ after_kb = (after_bytes / 1024.0).round(2)
50
+ after_mb = (after_bytes / 1024.0 / 1024.0).round(2)
51
+
52
+ puts "Before Size: #{before_bytes} bytes, #{before_kb} kb, #{before_mb} mb"
53
+ puts "After Size: #{clean_content.length} bytes, #{after_kb} kb, #{after_mb} mb"
54
+ puts "Done\n"
data/exe/pnote_to_json CHANGED
@@ -8,12 +8,15 @@ require 'pnote_client/converters/hml_to_pnote_converter'
8
8
  require 'pnote_client/converters/pnote_to_json_converter'
9
9
  require 'pnote_client/documents/hml'
10
10
  require 'pnote_client/display/console_display'
11
+ require 'pnote_client/validators/pnote_validator'
11
12
 
12
13
  def read_file(filepath)
13
14
  f = File.open(filepath, 'r')
15
+ dir_path = File.expand_path(File.dirname(f))
16
+ filename = File.basename(f, File.extname(f))
14
17
  content = f.read
15
18
  f.close
16
- return content
19
+ return filename, dir_path, content
17
20
  end
18
21
 
19
22
  def write_file(content, filepath)
@@ -22,6 +25,25 @@ def write_file(content, filepath)
22
25
  f.close
23
26
  end
24
27
 
28
+ def save_validation_log_file(result, log_filepath)
29
+ log_content = ""
30
+ log_content += "==========[Warnings]=========="
31
+ log_content += "\n"
32
+ result[:warnings].each_with_index do |warning, index|
33
+ log_content += "#{index + 1}.\n[경고 메시지]\n#{warning[:message]}\n[상세내용]\n#{warning[:detail]}\n\n\n"
34
+ end
35
+ log_content += "\n\n"
36
+ log_content += "==========[Errors]=========="
37
+ log_content += "\n"
38
+ result[:errors].each_with_index do |error, index|
39
+ log_content += "#{index + 1}.\n[에러 메시지]\n#{error[:message]}\n[상세내용]\n#{error[:detail]}\n\n\n"
40
+ end
41
+
42
+ write_file(log_content, log_filepath)
43
+ puts "로그 파일이 저장되었습니다. (#{log_filepath})"
44
+ end
45
+
46
+ # 커맨드라인 argument로 옵션 입력받기
25
47
  options = {
26
48
  config_filepath: File.join(File.dirname(__FILE__), '..', 'config.json'),
27
49
  include_image_data: false,
@@ -35,10 +57,6 @@ optparser = OptionParser.new do |opt|
35
57
  options[:input_filepath] = filepath
36
58
  end
37
59
 
38
- opt.on('-o', '--output [FILEPATH]', String, 'Output file path') do |filepath|
39
- options[:output_filepath] = filepath
40
- end
41
-
42
60
  opt.on('--[no-]image', 'Include image raw data (Default: false)') do |flag|
43
61
  options[:include_image_data] = flag
44
62
  end
@@ -53,17 +71,17 @@ optparser = OptionParser.new do |opt|
53
71
  end
54
72
  optparser.parse!
55
73
 
56
- if options[:input_filepath].nil? || options[:output_filepath].nil?
74
+ if options[:input_filepath].nil?
57
75
  abort(optparser.help)
58
76
  end
59
77
 
78
+ # 입력된 옵션 값 확인받기
60
79
  notice = <<-NOTICE
61
80
  [PnoteClient Version]
62
81
  #{PnoteClient::VERSION}
63
82
 
64
83
  [변환설정]
65
84
  입력파일 경로: #{options[:input_filepath]}
66
- 출력파일 경로: #{options[:output_filepath]}
67
85
  이미지: #{options[:include_image_data] ? '포함' : '제외'}
68
86
  설정파일 경로: #{options[:config_filepath]}
69
87
  코멘트 노출시간 단위: #{options[:show_time]}초
@@ -77,14 +95,37 @@ if response != 'y'
77
95
  abort
78
96
  end
79
97
 
98
+ # 변환 준비 작업
80
99
  display = PnoteClient::ConsoleDisplay.new
81
- config_content = read_file(options[:config_filepath])
100
+ _, _, config_content = read_file(options[:config_filepath])
82
101
  config = JSON.parse(config_content, symbolize_names: true)
83
102
  type_style_mapper = config[:hml_pnote_styles]
103
+ input_filename, input_dir_path, hml_content = read_file(options[:input_filepath])
104
+ output_filepath = File.join(input_dir_path, input_filename + ".json")
105
+ log_filepath = File.join(input_dir_path, input_filename + ".log")
84
106
 
85
- hml_document = PnoteClient::Documents::Hml.parse_from_file(options[:input_filepath])
107
+ # 파일 읽어들인 뒤 PnoteDocument로 변환
108
+ hml_document = PnoteClient::Documents::Hml.parse(hml_content)
86
109
  hml_to_pnote_converter = PnoteClient::Converters::HmlToPnoteConverter.new(hml_document, show_time: options[:show_time], type_style_mapper: type_style_mapper, include_image_data: options[:include_image_data])
87
110
  hml_to_pnote_converter.delegate = display
88
111
  pnote_document = hml_to_pnote_converter.convert
112
+
113
+ # PnoteDocument 상태 검증
114
+ validator = PnoteClient::Validators::PnoteValidator.new(pnote_document, display)
115
+ result = validator.validate
116
+ if validator.has_error?
117
+ save_validation_log_file(result, log_filepath)
118
+ puts "변환 에러가 발생했습니다. 로그파일을 확인해주세요."
119
+ abort
120
+ elsif validator.has_warning?
121
+ save_validation_log_file(result, log_filepath)
122
+ puts "#{validator.warning_count}개의 경고가 있습니다. 계속 진행하시겠습니까? (y/n)"
123
+ response = gets.chomp.downcase
124
+ if response != 'y'
125
+ abort
126
+ end
127
+ end
128
+
129
+ # PnoteDocument를 JSON 파일로 저장
89
130
  json = PnoteClient::Converters::PnoteToJsonConverter.new(pnote_document).convert
90
- write_file(json, options[:output_filepath])
131
+ write_file(json, output_filepath)
@@ -33,6 +33,7 @@ module PnoteClient
33
33
  def convert
34
34
  pnote_document = Documents::Pnote.new
35
35
 
36
+ # 한 문단(Paragraph)씩 변환
36
37
  paragraph_length = @hml_document.paragraphs.length
37
38
  paragraph_reader = Documents::Hml::ParagraphReader.new(@hml_document.paragraphs)
38
39
  paragraph_reader.next_paragraph do |paragraph, prev_paragraph, is_continuous, index|
@@ -119,7 +120,7 @@ module PnoteClient
119
120
  @delegate.paragraph_converted(count: paragraph_length, current: index) if @delegate
120
121
  end
121
122
 
122
-
123
+ # 바이너리(이미지) 변환
123
124
  binary_count = @hml_document.bin_items.length
124
125
  @hml_document.bin_items.each_with_index do |bin_item, index|
125
126
  raw_data = @include_image_data ? bin_item.raw_data : nil
@@ -6,8 +6,12 @@ module PnoteClient
6
6
  def initialize
7
7
  end
8
8
 
9
+ def print_validation(result)
10
+ puts "검증 결과: 경고 #{result[:warnings].length}개, 에러 #{result[:errors].length}개"
11
+ end
12
+
9
13
  def paragraph_converted(count:, current:)
10
- print_progress(count, current, "Converting paragraphs")
14
+ print_progress(count, current, "문단 변환 중")
11
15
 
12
16
  # Print newline when task is done
13
17
  if count == (current + 1)
@@ -16,7 +20,7 @@ module PnoteClient
16
20
  end
17
21
 
18
22
  def binary_converted(count:, current:)
19
- print_progress(count, current, "Converting binaries")
23
+ print_progress(count, current, "바이너리 파일 변환 중")
20
24
 
21
25
  # Print newline when task is done
22
26
  if count == (current + 1)
@@ -10,14 +10,6 @@ module PnoteClient
10
10
  class Hml
11
11
  attr_reader :styles, :paragraphs, :bin_items
12
12
 
13
- def self.parse_from_file(file_path)
14
- file = File.open(file_path)
15
- hml_content = file.read
16
- file.close
17
-
18
- return self.parse(hml_content)
19
- end
20
-
21
13
  def self.parse(hml_content)
22
14
  styles = []
23
15
  paragraphs = []
@@ -0,0 +1,119 @@
1
+ #
2
+ # Author: osh
3
+ # Created: 2019-05-20
4
+ # Last modified: 2019-05-20
5
+
6
+ module PnoteClient
7
+ module Validators
8
+ class PnoteValidator
9
+ def initialize(pnote_document, display = nil)
10
+ @pnote_document = pnote_document
11
+ @display = display
12
+ @result = {
13
+ errors: [],
14
+ warnings: []
15
+ }
16
+ end
17
+
18
+ def validate
19
+ reset_result
20
+ validate_pnote_document
21
+ print_validation_result
22
+ return @result
23
+ end
24
+
25
+ def has_error?
26
+ return error_count > 0
27
+ end
28
+
29
+ def has_warning?
30
+ return warning_count > 0
31
+ end
32
+
33
+ def error_count
34
+ return @result[:errors].length
35
+ end
36
+
37
+ def warning_count
38
+ return @result[:warnings].length
39
+ end
40
+
41
+ private
42
+
43
+ def reset_result
44
+ @result = {
45
+ errors: [],
46
+ warnings: []
47
+ }
48
+ end
49
+
50
+ def validate_pnote_document
51
+ @pnote_document.chapters.each do |chapter|
52
+ chapter.sub_chapters.each do |sub_chapter|
53
+ chapter.practices.each do |practice|
54
+ validation_result = validate_problem(practice)
55
+ add_result(validation_result)
56
+ end
57
+
58
+ sub_chapter.exercises.each do |exercise|
59
+ validation_result = validate_problem(exercise)
60
+ add_result(validation_result)
61
+ end
62
+
63
+ sub_chapter.practices.each do |practice|
64
+ validation_result = validate_problem(practice)
65
+ add_result(validation_result)
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ def validate_problem(problem)
72
+ if problem.answer
73
+ if problem.answer.length > 255
74
+ return {
75
+ valid: false,
76
+ type: :error,
77
+ message: "문제 정답의 길이가 255자보다 큽니다.",
78
+ detail: "문제 질문: #{problem.question}\n문제 정답: #{problem.answer}"
79
+ }
80
+ end
81
+
82
+ if problem.answer.length >= 20
83
+ return {
84
+ valid: false,
85
+ type: :warning,
86
+ message: "문제 정답의 길이가 20자 이상입니다.",
87
+ detail: "문제 질문: #{problem.question}\n문제 정답: #{problem.answer}"
88
+ }
89
+ end
90
+ end
91
+
92
+ return {
93
+ valid: true,
94
+ type: :success
95
+ }
96
+ end
97
+
98
+ def add_result(validation_result)
99
+ return if validation_result[:valid]
100
+
101
+ if validation_result[:type] == :error
102
+ @result[:errors] << {
103
+ message: validation_result[:message],
104
+ detail: validation_result[:detail]
105
+ }
106
+ elsif validation_result[:type] == :warning
107
+ @result[:warnings] << {
108
+ message: validation_result[:message],
109
+ detail: validation_result[:detail]
110
+ }
111
+ end
112
+ end
113
+
114
+ def print_validation_result
115
+ @display&.print_validation(@result)
116
+ end
117
+ end
118
+ end
119
+ end
@@ -1,3 +1,3 @@
1
1
  module PnoteClient
2
- VERSION = "2.2.5"
2
+ VERSION = "2.3.0"
3
3
  end
data/pnote_client.gemspec CHANGED
@@ -31,5 +31,5 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "rspec", "~> 3.0"
32
32
 
33
33
  spec.add_dependency "nokogiri"
34
- spec.add_dependency "hwp_script_to_latex", '~> 1.0.4'
34
+ spec.add_dependency "hwp_script_to_latex", '~> 1.1.0'
35
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pnote_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.5
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - bluesh55
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-13 00:00:00.000000000 Z
11
+ date: 2019-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,18 +72,19 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 1.0.4
75
+ version: 1.1.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 1.0.4
82
+ version: 1.1.0
83
83
  description:
84
84
  email:
85
85
  - bluesh55@naver.com
86
86
  executables:
87
+ - pnote_clean
87
88
  - pnote_to_json
88
89
  extensions: []
89
90
  extra_rdoc_files: []
@@ -98,6 +99,7 @@ files:
98
99
  - bin/console
99
100
  - bin/setup
100
101
  - config.json
102
+ - exe/pnote_clean
101
103
  - exe/pnote_to_json
102
104
  - lib/pnote_client.rb
103
105
  - lib/pnote_client/converters/hml_to_pnote_converter.rb
@@ -124,6 +126,7 @@ files:
124
126
  - lib/pnote_client/documents/pnote/sub_chapter.rb
125
127
  - lib/pnote_client/documents/pnote/teacher_comment.rb
126
128
  - lib/pnote_client/utils/string_util.rb
129
+ - lib/pnote_client/validators/pnote_validator.rb
127
130
  - lib/pnote_client/version.rb
128
131
  - pnote_client.gemspec
129
132
  homepage: https://github.com/PrivateCoach/pnote_client