notion_ruby_mapping 0.6.2 → 0.6.5

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
  SHA256:
3
- metadata.gz: 20cd62581513083b2b99d6fad57d3486fc855bf390f96ba01da263fa47e16029
4
- data.tar.gz: da20deff5ce1e53cbbe7d2cda86550676eea088fe1fee890385490b07cfa55ad
3
+ metadata.gz: 1cd3d5401bef9a8567f2e316a14ae4e2833d55eb5f22456afe74714133a2b970
4
+ data.tar.gz: b795baa4b02b38080ecce9381efad566c304548bb3e90116d71fa687e371e7de
5
5
  SHA512:
6
- metadata.gz: 68fd906fb46c5957d5eb48a41f41752c61a8fb40a7283dfa9ec3e4a95173c38401a966f24707893bdc1ded47ed76606990031488e2100eac234e4e53197de005
7
- data.tar.gz: d79d601217f69788eee522185868e5ded0e44598a60e9a4e4173c98b4df96c262c7516893e8f3829ac5d202491cad6c1548bb4de53d1646a691dacc6e8d3a6c8
6
+ metadata.gz: 761538aea8d3bfce6e577c2f6681a84e1ec94cf1a3ef2ccc15b284867303f4d6235b7d90c1806ed0beb2292fa4e5391a62e3b8a6e7c27f1741eb6a7f7f3fd275
7
+ data.tar.gz: d9faffa54de1a2379f75ad4add80cd08d38b450553d2e7b0fad340ee718a7a73e02275b085f54c140810dbb3c5013e375407cae39ae500bc1f34963ba0c61638
data/README.md CHANGED
@@ -75,14 +75,16 @@ NotionCache.instance.create_client ENV["NOTION_API_TOKEN"] # from environment
75
75
  ### 2.3 Sample codes
76
76
 
77
77
  1. [Database and page access sample](https://www.notion.so/hkob/Database-and-page-access-sample-d30033e707194faf995741167eb2b6f8)
78
- 1. [Append block children sample](https://www.notion.so/hkob/Append-block-children-sample-3867910a437340be931cf7f2c06443c6)
79
- 1. [Update block sample](https://www.notion.so/hkob/update-block-sample-5568c1c36fe84f12b83edfe2dda83028)
78
+ 2. [Append block children sample](https://www.notion.so/hkob/Append-block-children-sample-3867910a437340be931cf7f2c06443c6)
79
+ 3. [Update block sample](https://www.notion.so/hkob/update-block-sample-5568c1c36fe84f12b83edfe2dda83028)
80
80
 
81
81
  ### 2.4. Another example code (Use case)
82
82
 
83
83
  1. [Set icon to all icon unsettled pages](examples/set_icon_to_all_icon_unsettled_pages.md)
84
- 1. [Renumbering pages](examples/renumbering_pages.md)
85
- 1. [Change title](examples/change_title.md)
84
+ 2. [Renumbering pages](examples/renumbering_pages.md)
85
+ 3. [Change title](examples/change_title.md)
86
+ 4. [Create ER Diagram from Notion database](https://www.notion.so/hkob/notionErDiagram-Sample-1720c2199c534ca08138cde38f31f710)
87
+ 5. [Create Sitemap from Notion pages](https://www.notion.so/hkob/NotionSitemap-sample-14e195c83d024c5382aab09210916c87)
86
88
 
87
89
  ### 2.5 API reference
88
90
 
@@ -90,6 +92,11 @@ NotionCache.instance.create_client ENV["NOTION_API_TOKEN"] # from environment
90
92
 
91
93
  ## 3. ChangeLog
92
94
 
95
+ - 2022/8/10 [v0.6.5] add notionSiteMap.rb, rename createErDiagram to notionErDiagram, and move them to exe directory
96
+ - 2022/8/9 [v0.6.4] url can be entered instead of page_id, block_id and database_id
97
+ - 2022/8/9 [v0.6.3] update createErDiagram.rb (Fixed a bug with non-ASCII database titles)
98
+ - 2022/8/7 [v0.6.2] add comment_object support
99
+ - 2022/7/28 [v0.6.1] added createErDiagram.rb
93
100
  - 2022/7/22 [v0.6.0] updates for Notion-Version 2022-06-28 (lazy retrieve property values, retrieve page/database/block parent, single_property/dual_property for RelationProperty)
94
101
  - 2022/6/24 [v0.5.5] add file_names= to FileProperty
95
102
  - 2022/6/23 [v0.5.4] add update 'is_inline' and 'description' for database object
@@ -0,0 +1,70 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "notion_ruby_mapping"
4
+ include NotionRubyMapping
5
+
6
+ def append_database(text, db, db_titles)
7
+ base_title = db_title db
8
+ normalize_db_title(db, db_titles) if db_titles[db].nil?
9
+ text << " #{db_titles[db]} {"
10
+ text << %( Database title "#{base_title}") unless base_title == db_titles[db]
11
+ db.properties.reject { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
12
+ class_name = p.class.name.split("::").last.sub /Property/, ""
13
+ text << %( #{class_name} p#{i} "#{p.name}")
14
+ end
15
+ text << " }\n"
16
+ end
17
+
18
+ def normalize_db_title(db, db_titles)
19
+ base_title = db_title db
20
+ db_titles[db] = base_title.gsub(/[\w\d\-_]+/, "").empty? ? base_title : "d#{db_titles.count}"
21
+ end
22
+
23
+ def db_title(db)
24
+ db.database_title.full_text.gsub " ", "_"
25
+ end
26
+
27
+ if ARGV.length < 2
28
+ print "Usage: notionErDiagram.rb top_database_id code_block_id"
29
+ exit
30
+ end
31
+ database_id, code_block_id = ARGV
32
+ NotionCache.instance.create_client ENV["NOTION_API_KEY"]
33
+ block = Block.find code_block_id
34
+ unless block.is_a? CodeBlock
35
+ print "#{code_block_id} is not CodeBlock's id"
36
+ exit
37
+ end
38
+ dbs = [Database.find(database_id)]
39
+ text = %w[erDiagram]
40
+
41
+ finished = {}
42
+ db_titles = {}
43
+ until dbs.empty?
44
+ db = dbs.shift
45
+ finished[db] = true
46
+ append_database(text, db, db_titles)
47
+ db.properties.select { |pp| pp.is_a? RelationProperty }.each_with_index do |pp, i|
48
+ new_db = Database.find pp.relation_database_id
49
+ normalize_db_title(new_db, db_titles) if db_titles[new_db].nil?
50
+ text << " #{db_titles[db]} |o--o{ #{db_titles[new_db]} : r#{i}"
51
+ dbs << new_db unless finished[new_db]
52
+ end
53
+ text << ""
54
+ end
55
+
56
+ text_objects = text.each_with_object([]) do |str, ans|
57
+ strn = "#{str}\n"
58
+ if (last = ans.last)
59
+ if last.length + strn.length > 1999
60
+ ans << strn
61
+ else
62
+ ans[-1] += strn
63
+ end
64
+ else
65
+ ans << strn
66
+ end
67
+ end
68
+ block.rich_text_array.rich_text_objects = text_objects
69
+ block.language = "mermaid"
70
+ block.save
@@ -0,0 +1,130 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require "notion_ruby_mapping"
4
+ include NotionRubyMapping
5
+
6
+ # see https://zenn.dev/kinkinbeer135ml/articles/f08ce790091aca
7
+ def escape_title(title)
8
+ title.gsub /[(){}\[\]!”#$%&’()=^〜|¥1234567890@`「」{};:+*<>、。・?_]/, ""
9
+ end
10
+
11
+ class Sitemap
12
+ def initialize(top_page, orient, nolink)
13
+ @top_page = top_page
14
+ @code = {@top_page => "p0"}
15
+ @queue = [@top_page]
16
+ @finished = {}
17
+ @page_links = Hash.new { |h, k| h[k] = [] }
18
+ @text = ["flowchart #{orient}"]
19
+ @nolink = nolink
20
+ end
21
+ attr_reader :text
22
+
23
+ def dig_pages
24
+ until @queue.empty?
25
+ page = @queue.shift
26
+ @finished[page] = true
27
+ search_blocks(page)
28
+ @text << %(click #{@code[page]} "#{page["url"]}")
29
+ end
30
+ end
31
+
32
+ def code_with_title(page)
33
+ "#{@code[page]}(#{escape_title page.title})"
34
+ end
35
+
36
+ def search_blocks(page)
37
+ print "#{page.title}\n"
38
+ @children = []
39
+ @block_queue = page.children.to_a
40
+ search_block page, @block_queue.shift until @block_queue.empty?
41
+ unless @children.empty?
42
+ title = page == @top_page ? code_with_title(page) : @code[page]
43
+ @text << [title, @children.map { |p| code_with_title p }.join(" & ")].join(" --> ")
44
+ end
45
+ print "\n"
46
+ end
47
+
48
+ def search_block(page, block)
49
+ case block
50
+ when ChildPageBlock
51
+ add_child block.children.first.parent
52
+ when LinkToPageBlock
53
+ add_link page, block.page_id unless @nolink
54
+ else
55
+ @block_queue += block.children.to_a if block.has_children
56
+ search_link_in_rta(page, block.rich_text_array) if !@nolink && block.is_a?(TextSubBlockColorBaseBlock)
57
+ end
58
+ end
59
+
60
+ def add_child(child)
61
+ @queue << child
62
+ @children << child
63
+ set_code child
64
+ print " --> #{child.title}\n"
65
+ end
66
+
67
+ def set_code(page)
68
+ @code[page] ||= "p#{@code.length}"
69
+ end
70
+
71
+ def add_link(page, link_page_id)
72
+ return unless link_page_id
73
+
74
+ begin
75
+ link_page = Page.find link_page_id
76
+ rescue
77
+ print "\n#{link_page_id} can not read by this integration\n"
78
+ return
79
+ end
80
+ set_code link_page
81
+ @page_links[page] << link_page
82
+ print " -.-> #{link_page.title} "
83
+ end
84
+
85
+ def search_link_in_rta(page, rta)
86
+ rta.each { |to| add_link page, to.page_id if to.is_a?(MentionObject) && to.page_id }
87
+ end
88
+
89
+ def link_pages
90
+ @page_links.each do |org, array|
91
+ link_finished = {}
92
+ array.each do |lp|
93
+ link_finished[lp] = @finished[lp] ? @code[lp] : code_with_title(lp)
94
+ end
95
+ @text << [@code[org], link_finished.values.join(" & ")].join(" -.-> ")
96
+ end
97
+ end
98
+ end
99
+
100
+ if ARGV.length < 3
101
+ print "Usage: notionSitemap.rb top_page_id code_block_id orient(LR or TD) [--nolink]"
102
+ exit
103
+ end
104
+ top_page_id, code_block_id, orient, nolink = ARGV
105
+ NotionCache.instance.create_client ENV["NOTION_API_KEY"]
106
+ code_block = Block.find code_block_id
107
+ unless code_block.is_a? CodeBlock
108
+ print "#{code_block_id} is not CodeBlock's id"
109
+ exit
110
+ end
111
+
112
+ top_page = Page.find top_page_id
113
+ sm = Sitemap.new top_page, orient, nolink == "--nolink"
114
+ sm.dig_pages
115
+ sm.link_pages unless nolink
116
+ text_objects = sm.text.each_with_object([]) do |str, ans|
117
+ strn = "#{str}\n"
118
+ if (last = ans.last)
119
+ if last.length + strn.length > 1999
120
+ ans << strn
121
+ else
122
+ ans[-1] += strn
123
+ end
124
+ else
125
+ ans << strn
126
+ end
127
+ end
128
+ code_block.rich_text_array.rich_text_objects = text_objects
129
+ code_block.language = "mermaid"
130
+ code_block.save
@@ -25,7 +25,7 @@ module NotionRubyMapping
25
25
  assign.each_slice(2) { |(klass, key)| assign_property(klass, key) }
26
26
  @json ||= {}
27
27
  end
28
- attr_reader :json, :id, :archived
28
+ attr_reader :json, :id, :archived, :has_children
29
29
 
30
30
  # @param [Hash, Notion::Messages] json
31
31
  # @return [NotionRubyMapping::Base]
@@ -44,6 +44,22 @@ module NotionRubyMapping
44
44
  end
45
45
  end
46
46
 
47
+ def self.block_id(str)
48
+ if /^http/.match str
49
+ /#([\da-f]{32})/.match(str)[1]
50
+ else
51
+ NotionCache.instance.hex_id str
52
+ end
53
+ end
54
+
55
+ def self.database_id(str)
56
+ if /^http/.match str
57
+ /([\da-f]{32})\?/.match(str)[1]
58
+ else
59
+ NotionCache.instance.hex_id str
60
+ end
61
+ end
62
+
47
63
  # @param [Object] method
48
64
  # @param [Object] path
49
65
  # @param [nil] json
@@ -58,21 +74,29 @@ module NotionRubyMapping
58
74
  shell.join(" \\\n")
59
75
  end
60
76
 
77
+ def self.page_id(str)
78
+ if /^http/.match str
79
+ /([\da-f]{32})$/.match(str)[1]
80
+ else
81
+ NotionCache.instance.hex_id str
82
+ end
83
+ end
84
+
61
85
  def comments(query = nil, dry_run: false)
62
- if page? || block?
63
- if dry_run
64
- self.class.dry_run_script :get, @nc.retrieve_comments_path(@id)
65
- else
66
- ans = {}
67
- List.new(comment_parent: self,
68
- json: @nc.retrieve_comments_request(@id, query),
69
- query: query).each do |comment|
70
- dt_id = comment.discussion_id
71
- dt = ans[dt_id] ||= DiscussionThread.new dt_id
72
- dt.comments << comment
73
- end
74
- ans
86
+ return unless page? || block?
87
+
88
+ if dry_run
89
+ self.class.dry_run_script :get, @nc.retrieve_comments_path(@id)
90
+ else
91
+ ans = {}
92
+ List.new(comment_parent: self,
93
+ json: @nc.retrieve_comments_request(@id, query),
94
+ query: query).each do |comment|
95
+ dt_id = comment.discussion_id
96
+ dt = ans[dt_id] ||= DiscussionThread.new dt_id
97
+ dt.comments << comment
75
98
  end
99
+ ans
76
100
  end
77
101
  end
78
102
 
@@ -67,10 +67,11 @@ module NotionRubyMapping
67
67
  # @return [NotionRubyMapping::Block]
68
68
  def self.find(id, dry_run: false)
69
69
  nc = NotionCache.instance
70
+ block_id = Base.block_id id
70
71
  if dry_run
71
- Base.dry_run_script :get, nc.block_path(id)
72
+ Base.dry_run_script :get, nc.block_path(block_id)
72
73
  else
73
- nc.block id
74
+ nc.block block_id
74
75
  end
75
76
  end
76
77
 
@@ -10,10 +10,11 @@ module NotionRubyMapping
10
10
  # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#58ba9190fd544432a9e2a5823d6c33b7
11
11
  def self.find(id, dry_run: false)
12
12
  nc = NotionCache.instance
13
+ database_id = Base.database_id id
13
14
  if dry_run
14
- dry_run_script :get, nc.database_path(id)
15
+ dry_run_script :get, nc.database_path(database_id)
15
16
  else
16
- nc.database id
17
+ nc.database database_id
17
18
  end
18
19
  end
19
20
 
@@ -25,9 +25,9 @@ module NotionRubyMapping
25
25
  def block_json(not_update: true)
26
26
  ans = super
27
27
  ans[type] = if @page_id
28
- {"type" => "page_id", "page_id" => @page_id}
28
+ {"type" => "page_id", "page_id" => Base.page_id(@page_id)}
29
29
  else
30
- {"type" => "database_id", "database_id" => @database_id}
30
+ {"type" => "database_id", "database_id" => Base.database_id(@database_id)}
31
31
  end
32
32
  ans
33
33
  end
@@ -10,10 +10,11 @@ module NotionRubyMapping
10
10
  # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#7d868b8b81c3473082bbdc7370813a4a
11
11
  def self.find(id, dry_run: false)
12
12
  nc = NotionCache.instance
13
+ page_id = Base.page_id id
13
14
  if dry_run
14
- Base.dry_run_script :get, nc.page_path(id)
15
+ Base.dry_run_script :get, nc.page_path(page_id)
15
16
  else
16
- nc.page id
17
+ nc.page page_id
17
18
  end
18
19
  end
19
20
 
@@ -11,7 +11,7 @@ module NotionRubyMapping
11
11
  synced_from = @json[type]["synced_from"]
12
12
  @block_id = synced_from && @nc.hex_id(synced_from["block_id"])
13
13
  else
14
- @block_id = block_id
14
+ @block_id = Base.block_id block_id
15
15
  add_sub_blocks sub_blocks
16
16
  end
17
17
  @can_have_children = @block_id.nil?
@@ -31,6 +31,11 @@ module NotionRubyMapping
31
31
  @will_update = true
32
32
  end
33
33
 
34
+ # @return [String (frozen)]
35
+ def text
36
+ @expression
37
+ end
38
+
34
39
  protected
35
40
 
36
41
  # @return [Hash{String (frozen)->String}]
@@ -12,6 +12,11 @@ module NotionRubyMapping
12
12
  @options["plain_text"] = url
13
13
  end
14
14
 
15
+ # @return [String, NilClass]
16
+ def page_id
17
+ @options["page_id"]
18
+ end
19
+
15
20
  # @return [String (frozen)]
16
21
  def text
17
22
  ""
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NotionRubyMapping
4
- VERSION = "0.6.2"
4
+ VERSION = "0.6.5"
5
5
  NOTION_VERSION = "2022-06-28"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notion_ruby_mapping
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroyuki KOBAYASHI
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-07 00:00:00.000000000 Z
11
+ date: 2022-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -125,7 +125,9 @@ dependencies:
125
125
  description: Mapping tool from Notion Database/Page/Block to Ruby Objects.
126
126
  email:
127
127
  - hkob@metro-cit.ac.jp
128
- executables: []
128
+ executables:
129
+ - notionErDiagram.rb
130
+ - notionSitemap.rb
129
131
  extensions: []
130
132
  extra_rdoc_files: []
131
133
  files:
@@ -144,6 +146,8 @@ files:
144
146
  - examples/change_title.md
145
147
  - examples/renumbering_pages.md
146
148
  - examples/set_icon_to_all_icon_unsettled_pages.md
149
+ - exe/notionErDiagram.rb
150
+ - exe/notionSitemap.rb
147
151
  - images/post_set_icon.png
148
152
  - images/pre_set_icon.png
149
153
  - images/serial_number.png
@@ -234,7 +238,6 @@ files:
234
238
  - notion_ruby_mapping.gemspec
235
239
  - sig/notion_ruby_mapping.rbs
236
240
  - tools/an
237
- - tools/createErDiagram.rb
238
241
  homepage: https://github.com/hkob/notion_ruby_mapping.git
239
242
  licenses:
240
243
  - MIT
@@ -1,46 +0,0 @@
1
- #! /usr/bin/env ruby
2
-
3
- require "notion_ruby_mapping"
4
- include NotionRubyMapping
5
-
6
- def append_database(text, db)
7
- text << "#{db_title db} {"
8
- db.properties.reject { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
9
- class_name = p.class.name.split("::").last.sub /Property/, ""
10
- text << %Q[ #{class_name} p#{i} "#{p.name}"]
11
- end
12
- text << "}\n"
13
- end
14
-
15
- def db_title(db)
16
- db.database_title.full_text.gsub " ", "_"
17
- end
18
-
19
- if ARGV.length < 2
20
- print "Usage: createErDiagram.rb top_database_id code_block_id"
21
- exit
22
- end
23
- database_id, code_block_id = ARGV
24
- NotionCache.instance.create_client ENV["NOTION_API_KEY"]
25
- block = Block.find code_block_id
26
- unless block.is_a? CodeBlock
27
- print "#{code_block_id} is not CodeBlock's id"
28
- end
29
- dbs = [Database.find(database_id)]
30
- text = %w[erDiagram]
31
-
32
- finished = {}
33
- until dbs.empty?
34
- db = dbs.shift
35
- finished[db] = true
36
- append_database(text, db)
37
- db.properties.select { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
38
- new_db = Database.find p.relation_database_id
39
- text << "#{db_title db} |o--o{ #{db_title new_db} : r#{i}"
40
- dbs << new_db unless finished[new_db]
41
- end
42
- text << ""
43
- end
44
- block.rich_text_array.rich_text_objects = text.join("\n ")
45
- block.language = "mermaid"
46
- block.save