notion_ruby_mapping 0.6.4 → 0.6.5

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: 728ebd46ad3c0492c2454e60fec7c3ce271cc325ea0bfce9894f509c9cdcd809
4
- data.tar.gz: 9d1670b5f6dc8a7b366eb84e3c7bbb99bf65007ee208d437f472cc64e5f861c8
3
+ metadata.gz: 1cd3d5401bef9a8567f2e316a14ae4e2833d55eb5f22456afe74714133a2b970
4
+ data.tar.gz: b795baa4b02b38080ecce9381efad566c304548bb3e90116d71fa687e371e7de
5
5
  SHA512:
6
- metadata.gz: c8474a85e8cf7b59cc7a0c60681f7a193234a6d6435136a9078872ed54057a39171de01c49232b6bd012ea3f804c4efddd54c02a71ff32324cedbacfd339e5de
7
- data.tar.gz: 209a96f0365eb4d0c8f02283610e484d2a1018c2d453fd4ee3e7f08abd6c0e63077aea43ec5ef2c2aeecf7e69d492d6685c7b32848743498d4b3a9f08fb4e981
6
+ metadata.gz: 761538aea8d3bfce6e577c2f6681a84e1ec94cf1a3ef2ccc15b284867303f4d6235b7d90c1806ed0beb2292fa4e5391a62e3b8a6e7c27f1741eb6a7f7f3fd275
7
+ data.tar.gz: d9faffa54de1a2379f75ad4add80cd08d38b450553d2e7b0fad340ee718a7a73e02275b085f54c140810dbb3c5013e375407cae39ae500bc1f34963ba0c61638
data/README.md CHANGED
@@ -75,21 +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)
86
- 1. [Create erDiagram from Notion database](tools/createErDiagram.rb)
87
-
88
- ```shell
89
- Usage:
90
- ruby tools/createErDiagram.rb database_id code_block_id
91
- ruby tools/createErDiagram.rb "database_url" "code_block_url"
92
- ```
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)
93
88
 
94
89
  ### 2.5 API reference
95
90
 
@@ -97,6 +92,7 @@ Usage:
97
92
 
98
93
  ## 3. ChangeLog
99
94
 
95
+ - 2022/8/10 [v0.6.5] add notionSiteMap.rb, rename createErDiagram to notionErDiagram, and move them to exe directory
100
96
  - 2022/8/9 [v0.6.4] url can be entered instead of page_id, block_id and database_id
101
97
  - 2022/8/9 [v0.6.3] update createErDiagram.rb (Fixed a bug with non-ASCII database titles)
102
98
  - 2022/8/7 [v0.6.2] add comment_object support
@@ -6,13 +6,13 @@ include NotionRubyMapping
6
6
  def append_database(text, db, db_titles)
7
7
  base_title = db_title db
8
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]
9
+ text << " #{db_titles[db]} {"
10
+ text << %( Database title "#{base_title}") unless base_title == db_titles[db]
11
11
  db.properties.reject { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
12
12
  class_name = p.class.name.split("::").last.sub /Property/, ""
13
- text << %( #{class_name} p#{i} "#{p.name}")
13
+ text << %( #{class_name} p#{i} "#{p.name}")
14
14
  end
15
- text << "}\n"
15
+ text << " }\n"
16
16
  end
17
17
 
18
18
  def normalize_db_title(db, db_titles)
@@ -25,7 +25,7 @@ def db_title(db)
25
25
  end
26
26
 
27
27
  if ARGV.length < 2
28
- print "Usage: createErDiagram.rb top_database_id code_block_id"
28
+ print "Usage: notionErDiagram.rb top_database_id code_block_id"
29
29
  exit
30
30
  end
31
31
  database_id, code_block_id = ARGV
@@ -47,11 +47,24 @@ until dbs.empty?
47
47
  db.properties.select { |pp| pp.is_a? RelationProperty }.each_with_index do |pp, i|
48
48
  new_db = Database.find pp.relation_database_id
49
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}"
50
+ text << " #{db_titles[db]} |o--o{ #{db_titles[new_db]} : r#{i}"
51
51
  dbs << new_db unless finished[new_db]
52
52
  end
53
53
  text << ""
54
54
  end
55
- block.rich_text_array.rich_text_objects = text.join("\n ")
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
56
69
  block.language = "mermaid"
57
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]
@@ -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.4"
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.4
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-09 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