qiita_org 0.1.21 → 0.1.35

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/.yardopts +4 -0
  4. data/Gemfile +7 -0
  5. data/Gemfile.lock +40 -1
  6. data/lib/qiita_org.rb +41 -52
  7. data/lib/qiita_org/.qiita.conf +7 -6
  8. data/lib/qiita_org/access_qiita.rb +29 -0
  9. data/lib/qiita_org/all.rb +50 -0
  10. data/lib/qiita_org/base.rb +98 -0
  11. data/lib/qiita_org/config.json +1 -0
  12. data/lib/qiita_org/config.rb +1 -2
  13. data/lib/qiita_org/error_message.rb +99 -0
  14. data/lib/qiita_org/get.rb +15 -53
  15. data/lib/qiita_org/get_multiple_files.rb +36 -0
  16. data/lib/qiita_org/get_template.rb +24 -48
  17. data/lib/qiita_org/list.rb +9 -39
  18. data/lib/qiita_org/md_converter_for_image.rb +7 -7
  19. data/lib/qiita_org/{check_pc_os.rb → old_programs/check_pc_os.rb} +0 -0
  20. data/lib/qiita_org/{decide_option.rb → old_programs/decide_option.rb} +6 -2
  21. data/lib/qiita_org/old_programs/file_open.rb +15 -0
  22. data/lib/qiita_org/{get_file_path.rb → old_programs/get_file_path.rb} +0 -0
  23. data/lib/qiita_org/{get_file_url.rb → old_programs/get_file_url.rb} +12 -14
  24. data/lib/qiita_org/{search_conf_path.rb → old_programs/search_conf_path.rb} +1 -1
  25. data/lib/qiita_org/old_programs/select_path.rb +16 -0
  26. data/lib/qiita_org/old_programs/set_config.rb +30 -0
  27. data/lib/qiita_org/{show_file_and_url.rb → old_programs/show_file_and_url.rb} +45 -36
  28. data/lib/qiita_org/post.rb +68 -50
  29. data/lib/qiita_org/template.org +1 -0
  30. data/lib/qiita_org/upload.rb +110 -0
  31. data/lib/qiita_org/version.rb +1 -1
  32. data/tests/test.org +10 -3
  33. data/tests/test2.org +3 -1
  34. data/tests/test3.org +2 -1
  35. data/tests/test4.org +16 -0
  36. data/tests/twitter_test.org +14 -0
  37. metadata +1614 -10
  38. data/lib/qiita_org/hoge.txt +0 -3
  39. data/tests/test2.md +0 -6
@@ -1,25 +1,25 @@
1
1
  class MdConverter
2
- def initialize(lines)
3
- @lines = lines
2
+ def initialize()
3
+
4
4
  end
5
5
 
6
- def convert_for_image()
7
- @lines.each_with_index do |line, i|
6
+ def convert_for_image(lines)
7
+ lines.each_with_index do |line, i|
8
8
  m = []
9
9
  if m = line.match(/\[\!\[img\]\((.+) "(.+)"\)\]\((.+)\)/)
10
10
  path = File.basename(m[1])
11
11
  url = m[3]
12
- @lines[i] = "![#{path}](#{url})\n"
12
+ lines[i] = "![#{path}](#{url})\n"
13
13
  elsif m = line.match(/\[\!\[img\]\((.+)\)\]\((.+)\)/)
14
14
  path = File.basename(m[1])
15
15
  url = m[2]
16
- @lines[i] = "![#{path}](#{url})\n"
16
+ lines[i] = "![#{path}](#{url})\n"
17
17
  else
18
18
  next
19
19
  end
20
20
  end
21
21
 
22
- return @lines
22
+ return lines
23
23
  end
24
24
  end
25
25
 
@@ -7,8 +7,12 @@ class DecideOption
7
7
  lines = File.readlines(@src)
8
8
 
9
9
  lines.each do |line|
10
- if line.match(/\#\+qiita_(.+): (.+)/)
11
- option = line.match(/\#\+qiita_(.+): (.+)/)[1]
10
+ m = []
11
+ if m = line.match(/\#\+qiita_(.+): (.+)/)
12
+ option = m[1] #line.match(/\#\+qiita_(.+): (.+)/)[1]
13
+ unless option == "public" || option == "teams" || option == "private"
14
+ next
15
+ end
12
16
  return option
13
17
  end
14
18
  end
@@ -0,0 +1,15 @@
1
+ class FileOpen
2
+ def initialize(os)
3
+ @os = os
4
+ end
5
+
6
+ def file_open(order)
7
+ if @os == "mac"
8
+ system "open #{order}"
9
+ elsif @os == "windows"
10
+ system "explorer.exe #{order}"
11
+ else
12
+ system "xdg-open #{order}"
13
+ end
14
+ end
15
+ end
@@ -3,30 +3,26 @@ require "json"
3
3
  require "open-uri"
4
4
  require "io/console"
5
5
  require "colorize"
6
- require "qiita_org/search_conf_path.rb"
6
+ require "qiita_org/set_config.rb"
7
+ require "qiita_org/error_message.rb"
8
+ require "qiita_org/access_qiita.rb"
7
9
 
8
10
  class GetFileUrl
9
11
  def initialize(id, file, mode)
10
12
  @id = id
11
13
  @file = file
12
14
  @mode = (mode == "qiita" || mode == "open")? "public" : mode
13
- search = SearchConfPath.new(Dir.pwd, Dir.home)
14
- @conf_dir = search.search_conf_path()
15
- set_config()
16
- end
17
-
18
- def set_config()
19
- conf_path = File.join(@conf_dir, ".qiita.conf")
20
- @conf = JSON.load(File.read(conf_path))
21
- @access_token = @conf["access_token"]
22
- @teams_url = @conf["teams_url"]
15
+ @access_token, @teams_url, @display, @ox_qmd_load_path = SetConfig.new().set_config()
16
+ if @mode == "teams"
17
+ ErrorMessage.new().teams_url_error(@teams_url)
18
+ end
23
19
  end
24
20
 
25
21
  def get_file_url()
26
22
  qiita = (@mode == "teams")? @teams_url : "https://qiita.com/"
27
23
  path = "api/v2/items/#{@id}"
28
24
 
29
- items = access_qiita(@access_token, qiita, path)
25
+ items = AccessQiita.new(@access_token, qiita, path).access_qiita()
30
26
 
31
27
  file_url = items["body"].match(/\!\[#{@file}\]\(((.+))\)/)[2]
32
28
  return file_url
@@ -34,6 +30,7 @@ class GetFileUrl
34
30
  #File.write("url_text.md", items["body"])
35
31
  end
36
32
 
33
+ =begin
37
34
  def access_qiita(access_token, qiita, path)
38
35
  uri = URI.parse(qiita + path)
39
36
 
@@ -41,10 +38,11 @@ class GetFileUrl
41
38
  "Content-Type" => "application/json" }
42
39
 
43
40
  response = URI.open(
44
- "#{uri}",
45
- "Authorization" => "#{headers["Authorization"]}",
41
+ "# {uri}",
42
+ "Authorization" => "# {headers["Authorization"]}",
46
43
  )
47
44
  items = JSON.parse(response.read)
48
45
  return items
49
46
  end
47
+ =end
50
48
  end
@@ -2,7 +2,7 @@ class SearchConfPath
2
2
  def initialize(dir, home)
3
3
  @dir = dir
4
4
  @home = home
5
- search_conf_path()
5
+ #search_conf_path()
6
6
  end
7
7
 
8
8
  def search_conf_path()
@@ -0,0 +1,16 @@
1
+ class SelectPath
2
+ def initialize()
3
+ end
4
+
5
+ def select_path(mode, teams_url)
6
+ case mode
7
+ when "teams"
8
+ qiita = teams_url
9
+ path = "api/v2/items?page=1&per_page=100"
10
+ else
11
+ qiita = "https://qiita.com/"
12
+ path = "api/v2/authenticated_user/items?page=1&per_page=100"
13
+ end
14
+ return qiita, path
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ require "qiita_org/search_conf_path.rb"
2
+ require "qiita_org/error_message.rb"
3
+
4
+ class SetConfig
5
+ def initialize()
6
+ search = SearchConfPath.new(Dir.pwd, Dir.home)
7
+ @lib = File.expand_path("../../../lib", __FILE__)
8
+ @conf_dir = search.search_conf_path()
9
+ if @conf_dir != Dir.home
10
+ puts "config file path: #{@conf_dir.gsub(Dir.home, "~")}".green
11
+ else
12
+ puts "config file path: #{@conf_dir}"
13
+ end
14
+ end
15
+
16
+ def set_config()
17
+ conf_path = File.join(@conf_dir, ".qiita.conf")
18
+ conf = JSON.load(File.read(conf_path))
19
+ access_token = conf["access_token"]
20
+ teams_url = conf["teams_url"]
21
+ display = conf["display"]
22
+ ox_qmd_load_path = File.join(@lib, "qiita_org", "ox-qmd", "ox-qmd")
23
+
24
+ ErrorMessage.new().access_token_error(access_token) #== false
25
+ # puts "Please setting ACCESS_TOKEN".red
26
+ # exit
27
+
28
+ return access_token, teams_url, display, ox_qmd_load_path
29
+ end
30
+ end
@@ -1,47 +1,48 @@
1
1
  require "colorize"
2
2
  require "qiita_org/get_file_url.rb"
3
+ require "qiita_org/set_config.rb"
4
+ require "qiita_org/access_qiita.rb"
5
+ require "qiita_org/file_open.rb"
6
+ require "qiita_org/error_message.rb"
3
7
 
4
8
  class ShowFile
5
9
  def initialize(paths, src, mode, os)
6
10
  @paths = paths
7
11
  @src = src
8
- @mode = (mode == "qiita" || mode == "open")? "public" : mode
12
+ @mode = (mode == "qiita" || mode == "open") ? "public" : mode
9
13
  @os = os
10
- search = SearchConfPath.new(Dir.pwd, Dir.home)
11
- @conf_dir = search.search_conf_path()
12
- end
13
-
14
- def set_config()
15
- conf_path = File.join(@conf_dir, ".qiita.conf")
16
- @conf = JSON.load(File.read(conf_path))
17
- @access_token = @conf["access_token"]
18
- @teams_url = @conf["teams_url"]
14
+ @fileopen = FileOpen.new(@os)
19
15
  end
20
16
 
21
17
  def open_file_dir()
22
18
  previous_paths = []
23
19
  previous_paths << File.join(@paths[0].split("/")[0..-2])
24
- if @os == "mac"
25
- system "open #{File.join(@paths[0].split("/")[0..-2])}"
26
- elsif @os == "windows"
27
- system "explorer.exe #{File.join(@paths[0].split("/")[0..-2])}"
28
- else
29
- system "open #{File.join(@paths[0].split("/")[0..-2])}"
30
- system "xdg-open #{File.join(@paths[0].split("/")[0..-2])}"
31
- end
20
+ @fileopen.file_open(File.join(@paths[0].split("/")[0..-2]))
21
+
22
+ # if @os == "mac"
23
+ # system "open # {File.join(@paths[0].split("/")[0..-2])}"
24
+ # elsif @os == "windows"
25
+ # system "explorer.exe # {File.join(@paths[0].split("/")[0..-2])}"
26
+ # else
27
+ # system "open # {File.join(@paths[0].split("/")[0..-2])}"
28
+ # system "xdg-open # {File.join(@paths[0].split("/")[0..-2])}"
29
+ # end
30
+
32
31
  @paths.each do |path|
33
32
  dir_path = File.join(path.split("/")[0..-2])
34
33
  unless previous_paths.include?(dir_path)
35
34
  previous_paths << dir_path
36
- #system "open #{dir_path}"
37
- if @os == "mac"
38
- system "open #{dir_path}"
39
- elsif @os == "windows"
40
- system "explorer.exe #{dir_path}"
41
- else
42
- system "open #{dir_path}"
43
- system "xdg-open #{dir_path}"
44
- end
35
+ #system "open # {dir_path}"
36
+ @fileopen.file_open(dir_path)
37
+
38
+ # if @os == "mac"
39
+ # system "open # {dir_path}"
40
+ # elsif @os == "windows"
41
+ # system "explorer.exe # {dir_path}"
42
+ # else
43
+ # system "open # {dir_path}"
44
+ # system "xdg-open # {dir_path}"
45
+ # end
45
46
  end
46
47
  end
47
48
  end
@@ -50,21 +51,27 @@ class ShowFile
50
51
  conts = File.read(@src)
51
52
  id = conts.match(/\#\+qiita_#{@mode}: (.+)/)[1]
52
53
 
53
- set_config()
54
+ @access_token, @teams_url, @display, @ox_qmd_load_path = SetConfig.new().set_config()
55
+ if @mode == "teams"
56
+ ErrorMessage.new().teams_url_error(@teams_url)
57
+ end
54
58
 
55
- qiita = (@mode == "teams")? @teams_url : "https://qiita.com/"
59
+ qiita = (@mode == "teams") ? @teams_url : "https://qiita.com/"
56
60
  path = "api/v2/items/#{id}"
57
61
 
58
- items = access_qiita(@access_token, qiita, path)
62
+ items = AccessQiita.new(@access_token, qiita, path).access_qiita()
59
63
 
64
+ @fileopen.file_open(items["url"])
65
+ =begin
60
66
  if @os == "mac"
61
- system "open #{items["url"]}"
67
+ system "open # {items["url"]}"
62
68
  elsif @os == "windows"
63
- system "explorer.exe #{items["url"]}"
69
+ system "explorer.exe # {items["url"]}"
64
70
  else
65
- system "open #{items["url"]}"
66
- system "xdg-open #{items["url"]}"
71
+ system "open # {items["url"]}"
72
+ system "xdg-open # {items["url"]}"
67
73
  end
74
+ =end
68
75
  end
69
76
 
70
77
  def show_file_url()
@@ -100,6 +107,7 @@ class ShowFile
100
107
  File.write(@src, lines.join)
101
108
  end
102
109
 
110
+ =begin
103
111
  def access_qiita(access_token, qiita, path)
104
112
  uri = URI.parse(qiita + path)
105
113
 
@@ -107,10 +115,11 @@ class ShowFile
107
115
  "Content-Type" => "application/json" }
108
116
 
109
117
  response = URI.open(
110
- "#{uri}",
111
- "Authorization" => "#{headers["Authorization"]}",
118
+ "# {uri}",
119
+ "Authorization" => "# {headers["Authorization"]}",
112
120
  )
113
121
  items = JSON.parse(response.read)
114
122
  return items
115
123
  end
124
+ =end
116
125
  end
@@ -4,29 +4,24 @@ require "net/https"
4
4
  require "json"
5
5
  require "command_line/global"
6
6
  require "colorize"
7
- require "qiita_org/search_conf_path.rb"
8
7
  require "qiita_org/md_converter_for_image"
8
+ require "qiita_org/error_message"
9
+ require "qiita_org/access_qiita.rb"
9
10
 
10
11
  class QiitaPost
11
12
  def initialize(file, option, os)
12
13
  @src = file
13
14
  @option = (option == "qiita" || option == "open")? "public" : option
14
15
  @os = os
15
- search = SearchConfPath.new(Dir.pwd, Dir.home)
16
- @conf_dir = search.search_conf_path()
17
- p @conf_dir
16
+ @base = QiitaBase.new
18
17
  end
19
18
 
20
19
  public
21
- def get_title_tags()
22
- @conts = File.read(@src)
23
- m = @conts.match(/\#\+(TITLE|title|Title): (.+)/)
20
+ def get_title_tags(conts)
21
+ m = conts.match(/\#\+(TITLE|title|Title): (.+)/)
24
22
  @title = m ? m[2] : "テスト"
25
- @tags = if m = @conts.match(/\#\+(TAG|tag|Tag|tags|TAGS|Tags): (.+)/)
26
- if m[2].count(",") >= 5
27
- puts "The maximum number of tag is five. Please delete some tags.".red
28
- exit
29
- end
23
+ @tags = if m = conts.match(/\#\+(TAG|tag|Tag|tags|TAGS|Tags): (.+)/)
24
+ ErrorMessage.new().many_tags_error(m[2])
30
25
  m[2].split(",").inject([]) do |l, c|
31
26
  l << { name: c.strip } #, versions: []}
32
27
  end
@@ -34,22 +29,14 @@ class QiitaPost
34
29
  [{ name: "hoge" }] #, versions: [] }]
35
30
  end
36
31
  p @tags
37
- end
38
-
39
- def set_config()
40
- conf_path = File.join(@conf_dir, ".qiita.conf")
41
- @conf = JSON.load(File.read(conf_path))
42
- @access_token = @conf["access_token"]
43
- @teams_url = @conf["teams_url"]
44
- lib = File.expand_path("../../../lib", __FILE__)
45
- @ox_qmd_load_path = File.join(lib, "qiita_org", "ox-qmd", "ox-qmd") # @conf["ox_qmd_load_path"]
32
+ return @title, @tags
46
33
  end
47
34
 
48
35
  # src.org -> src.md
49
36
  def convert_org_to_md()
50
37
  command = "emacs #{@src} --batch -l #{@ox_qmd_load_path} -f org-qmd-export-to-markdown --kill"
51
38
  res = command_line command
52
- p res
39
+ ErrorMessage.new().md_file_exists?(@src, res)
53
40
  end
54
41
 
55
42
  # add source path in md
@@ -60,21 +47,47 @@ class QiitaPost
60
47
  end
61
48
 
62
49
  # patch or post selector by qiita_id
63
- def select_patch_or_post()
64
- m = []
65
- @patch = false
66
- if m = @conts.match(/\#\+qiita_#{@option}: (.+)/)
67
- @qiita_id = m[1]
68
- @patch = true
50
+ def select_patch_or_post(src, option)
51
+ #m = []
52
+ patch = false
53
+ qiita_id = @base.get_report_id(src, option)
54
+ patch = true if qiita_id != nil
55
+ #if m = conts.match(/\#\+qiita_#{option}: (.+)/)
56
+ # qiita_id = m[1]
57
+ #patch = true
58
+ #else
59
+ # qiita_id = ""
60
+ #end
61
+ return qiita_id, patch
62
+ end
63
+
64
+ def check_change_public(conts, option, id)
65
+ qiita = "https://qiita.com/"
66
+ path = "api/v2/items/#{id}"
67
+ items = AccessQiita.new(@access_token, qiita, path).access_qiita()
68
+
69
+ if items["private"]
70
+ return conts, option
69
71
  else
70
- @qiita_id = ""
72
+ option = "public"
73
+ lines = File.readlines(@src)
74
+ file = File.open(@src, "w")
75
+ lines.each_with_index do |line, i|
76
+ lines[i] = "#+qiita_#{option}: #{id}\n" if line.match(/\#\+qiita_private: (.+)/)
77
+ file.print(lines[i])
78
+ end
79
+ conts = File.read(@src)
80
+ return conts, option
71
81
  end
72
82
  end
73
83
 
84
+ # check twitter post
85
+ def select_twitter(conts, option)
86
+ option == "public" && conts.match?(/^\#\+twitter:\s*on$/i)
87
+ end
88
+
74
89
  def select_option(option)
75
90
  qiita = (option == "teams")? @teams_url : "https://qiita.com/"
76
- #qiita = (option == "teams")? "https://nishitani.qiita.com/" :
77
- # "https://qiita.com/"
78
91
  case option
79
92
  when "teams", "qiita", "public", "open"
80
93
  private = false
@@ -93,6 +106,7 @@ class QiitaPost
93
106
  "private": @private,
94
107
  "title": @title,
95
108
  "tags": @tags,
109
+ "tweet": @twitter,
96
110
  }
97
111
 
98
112
  if @patch
@@ -110,11 +124,16 @@ class QiitaPost
110
124
 
111
125
  headers = { "Authorization" => "Bearer #{@access_token}",
112
126
  "Content-Type" => "application/json" }
127
+
113
128
  if @patch
114
- @res = http_req.patch(uri.path, params.to_json, headers)
129
+ res = http_req.patch(uri.path, params.to_json, headers)
115
130
  else
116
- @res = http_req.post(uri.path, params.to_json, headers)
131
+ res = http_req.post(uri.path, params.to_json, headers)
117
132
  end
133
+
134
+ ErrorMessage.new().qiita_post_error(res, @src.gsub(".org", ".md"))
135
+
136
+ return res
118
137
  end
119
138
 
120
139
  # qiita return
@@ -129,6 +148,9 @@ class QiitaPost
129
148
  end
130
149
  print "%20s %s\n" % [key, cont]
131
150
  end
151
+ #if @display == "suppress"
152
+ # puts @res_body["url"].green
153
+ #end
132
154
  end
133
155
 
134
156
  # add qiita_id on src.org
@@ -139,30 +161,26 @@ class QiitaPost
139
161
  end
140
162
  end
141
163
 
142
- # open qiita
143
- def open_qiita()
144
- if @os == "mac"
145
- system "open #{@res_body["url"]}"
146
- elsif @os == "windows"
147
- system "explorer.exe #{@res_body["url"]}"
148
- else
149
- system "open #{@res_body["url"]}"
150
- system "xdg-open #{@res_body["url"]}"
164
+ def run()
165
+ @conts = File.read(@src)
166
+ @title, @tags = get_title_tags(@conts)
167
+ @access_token, @teams_url, @display, @ox_qmd_load_path = @base.set_config()
168
+
169
+ if @option == "teams"
170
+ ErrorMessage.new().teams_url_error(@teams_url)
151
171
  end
152
- end
153
172
 
154
- def run()
155
- get_title_tags()
156
- set_config()
157
173
  convert_org_to_md()
158
174
  add_source_path_in_md()
159
- @lines = MdConverter.new(@lines).convert_for_image()
160
- select_patch_or_post()
175
+ @lines = MdConverter.new().convert_for_image(@lines)
176
+ @qiita_id, @patch = select_patch_or_post(@src, @option)
177
+ @conts, @option = check_change_public(@conts, @option, @qiita_id) if (@patch and @option == "private")
178
+ @twitter = select_twitter(@conts, @option)
161
179
  @qiita, @private = select_option(@option)
162
- qiita_post()
180
+ @res = qiita_post()
163
181
  get_and_print_qiita_return()
164
182
 
165
- open_qiita()
183
+ @base.file_open(@os, @res_body["url"]) if @display != "suppress"
166
184
 
167
185
  add_qiita_id_on_org()
168
186