jekyll-rpg 0.0.1 → 0.0.2

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: c8401558390fb5a2caea3e00dcda596df06996e18faa86de3882376369ad17a1
4
- data.tar.gz: c6311b8c8c669eb490fb7dc1d5b5eac426abb2580ce7ed88de561917fa0739a2
3
+ metadata.gz: ca2f5fd2ef2b7076cd7b216e32afddfbd82f42f6623d210e18261fa8bd63d6b2
4
+ data.tar.gz: af02fd64b839d1f2f195e89b6e5e643ddccc6b80ba104a0e96fee9f30759f18a
5
5
  SHA512:
6
- metadata.gz: 31280e6dc53b72c9817de32a737cdc4da39f9378bd2af186aeb86fd0c480ecc2c4dabac9f909348f347709e7574c2faf2729cf794e92ee516ac2f0abee08d2bc
7
- data.tar.gz: d3f33f666a3e6c02094081af9f5be9fad47131af223ddd72fe54d8835a783dbae1aa1e5c3a29b897777f7999d8391dddd22f9f1573cc0754c6cb4f6ee3ac70c0
6
+ metadata.gz: f33eb4bb05143ae686511f768675f34264039750315e727b5783c03cc227c8f03c82074eba0bdbe28427d2cf74309c2240ca6fe716d180c4c8fcdeea8030de06
7
+ data.tar.gz: 27ea0217d2a7ed24c9cce3aa67bc2318801a6095e3e443e4dde033a73382b096f2b5363f48e46c336cf7fcf1a58531633a2c52678b87282f3df8fb237ae622a5
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllRPG
4
+ class CollectionPage
5
+ attr_accessor :name, :collection, :slug, :written
6
+
7
+ def initialize(name, collection, slug, written)
8
+ @name = name
9
+ @collection = collection
10
+ @slug = slug
11
+ # boolean
12
+ @written = written
13
+ end
14
+
15
+ def markdown_link
16
+ "[#{@name}](/#{@collection}/#{@slug})"
17
+ end
18
+ end
19
+ end
@@ -1,147 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'jekyll'
4
+ require_relative 'references'
4
5
 
5
6
  module JekyllRPG
6
- class ReferenceTracker
7
- def initialize(site)
8
- @site = site
9
- @references = {}
10
- @broken_links = []
11
- @collection_keys = @site.collections.keys - ["posts"]
12
-
13
- # Parse references from markdown links
14
- @collection_keys.each do |collection|
15
- @site.collections[collection].docs.each do |doc|
16
- unless doc.data['dm'] && !@site.data['dm_mode']
17
- doc.to_s.scan(/(?<=[^!])\[.*?\]\(\/.*?\/.*?\)/).each do |reference|
18
- self.add_reference(doc, reference)
19
- end
20
- else
21
- doc.data['published'] = false
22
- end
23
- end
24
- end
25
-
26
- # For each collection page, add where it is referenced
27
- @collection_keys.each do |collection|
28
- site.collections[collection].docs.each do |doc|
29
- slugs = {}
30
- @collection_keys.each do |add_keys|
31
- slugs[add_keys] = []
32
- end
33
- unless @references[collection].nil? || @references[collection][doc.data['slug']].nil?
34
- @references[collection][doc.data['slug']].each do |referent|
35
- referent_collection = referent[0]
36
- referent[1].each do |x|
37
- slugs[referent_collection].push("[#{x[1]}](/#{referent_collection}/#{x[0]})")
38
- end
39
- end
40
- end
41
- doc.data['referenced_by'] = slugs
42
- if doc.data.key?('refs')
43
- refs_table_required = doc.data['refs']
44
- elsif site.config['collections'][doc.collection.label].key?('refs')
45
- refs_table_required = site.config['collections'][doc.collection.label]['refs']
46
- elsif site.config.key?('refs')
47
- refs_table_required = site.config['refs']
48
- end
49
- if refs_table_required
50
- doc.content = doc.content + refs_table(doc.data['referenced_by'])
51
- end
52
- end
53
- end
54
-
55
- # Create list of broken links
56
- @references.each do |collection|
57
- collection_name = collection[0]
58
- collection[1].each do |item|
59
- item_hash = {
60
- 'url' => "#{site.config['url']}/#{collection_name}/#{item[0]}",
61
- 'collection' => collection_name,
62
- 'slug' => item[0],
63
- 'referenced_by' => item[1]
64
- }
65
- if site.collections[collection_name].nil? || site.collections[collection_name].docs.find { |doc| doc.data['slug'] == item[0] }.nil?
66
- @broken_links.push(item_hash)
67
- end
68
- end
69
- end
70
- end
71
-
72
- def collection_keys
73
- @collection_keys
74
- end
75
-
76
- def references
77
- @references
78
- end
79
-
80
- def broken_links
81
- @broken_links
82
- end
83
-
84
- def add_reference(doc, reference)
85
- referent_collection = doc.collection.label
86
- referent_page = doc.data['slug']
87
-
88
- # Find part of markdown link that represents the collection and item
89
- referenced_collection = reference[/(?<=\/).*(?=\/)/]
90
- referenced_page = reference[/(?<=\/)(?:(?!\/).)*?(?=\))/]
91
-
92
- @references[referenced_collection] = {} unless @references.key?(referenced_collection)
93
- @references[referenced_collection][referenced_page] = {} unless @references[referenced_collection].key?(referenced_page)
94
- @references[referenced_collection][referenced_page][referent_collection] = {} unless @references[referenced_collection][referenced_page].key?(referent_collection)
95
-
96
- @references[referenced_collection][referenced_page][referent_collection][referent_page] = doc.data['name']
97
- end
98
-
99
- def refs_table(refs)
100
- table = <<~TABLE
101
- # Referenced By:
102
- <table>
103
- <thead>
104
- <tr>
105
- <th>Collection</th>
106
- <th>Links</th>
107
- </tr>
108
- </thead>
109
- <tbody>
110
- #{refs_rows(refs)}
111
- </tbody>
112
- </table>
113
- TABLE
114
- table
115
- end
116
-
117
- def refs_rows(refs)
118
- row = ''
119
- refs.each do |reference|
120
- # Don't add the collection name unless there's a reference
121
- unless reference[1].count == 0
122
- row += <<~ROW
123
- <tr>
124
- <td markdown="span"><b> #{reference[0].capitalize} </b></td>
125
- <td markdown="span"> #{refs_links(reference)} </td>
126
- </tr>
127
- ROW
128
- end
129
- end
130
- row
131
- end
132
-
133
- def refs_links(reference)
134
- links = ''
135
- reference[1].each do |link|
136
- links += " - #{link} <br>"
137
- end
138
- links
139
- end
140
- end
141
-
142
7
  # Bi-directional page links
143
8
  Jekyll::Hooks.register :site, :post_read do |site|
144
- ref = ReferenceTracker.new(site)
9
+ ref = References.new(site)
145
10
 
146
11
  site.data['graph'] = ref.references
147
12
 
@@ -0,0 +1,193 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'collection_page'
4
+
5
+ module JekyllRPG
6
+ class References
7
+ attr_accessor :collection_keys, :references, :broken_links, :graph
8
+
9
+ def initialize(site)
10
+ @site = site
11
+ @references = {}
12
+ @graph = []
13
+ @broken_links = []
14
+ @collection_keys = @site.collections.keys - ['posts']
15
+
16
+ # Parse references from markdown links
17
+ @collection_keys.each do |collection|
18
+ @site.collections[collection].docs.each do |doc|
19
+ if doc.data['dm'] && !@site.data['dm_mode']
20
+ doc.data['published'] = false
21
+ else
22
+ referent = CollectionPage.new(
23
+ doc.data['name'],
24
+ doc.collection.label,
25
+ doc.data['slug'],
26
+ true
27
+ )
28
+ markdown_links(doc).each do |reference|
29
+ @graph.push(edge(referent, reference))
30
+ add_reference(doc, reference)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ # For each collection page, add where it is referenced
37
+ @collection_keys.each do |collection|
38
+ site.collections[collection].docs.each do |doc|
39
+ page_refs = {}
40
+
41
+ # Get the information for every page the current doc is referenced in
42
+ # And push links to an array that represents the collections of those pages
43
+ referenced_in(collection, doc.data['slug'] ).each do |reference|
44
+ page_refs[reference.collection] = [] unless page_refs.key?(reference.collection)
45
+ page_refs[reference.collection].push(reference.markdown_link)
46
+ end
47
+
48
+ # Make sure links in collections are unique
49
+ page_refs.each do |k,v|
50
+ page_refs[k] = v.uniq
51
+ end
52
+
53
+ # Put the reference data on the doc
54
+ doc.data['referenced_by'] = page_refs
55
+
56
+ # If the references table option is configured, append the table
57
+ if refs_table_required(doc)
58
+ doc.content = doc.content + refs_table(doc.data['referenced_by'])
59
+ end
60
+ end
61
+ end
62
+
63
+ #print broken_links
64
+
65
+ # Create list of broken links
66
+ unwritten_pages.each do |edge|
67
+ @broken_links.push({
68
+ 'reference_name' => edge['reference'].name,
69
+ 'reference_collection' => edge['reference'].collection,
70
+ 'reference_slug' => edge['reference'].slug,
71
+ 'reference_link' => edge['reference'].markdown_link,
72
+ 'referent_name' => edge['referent'].name,
73
+ 'referent_collection' => edge['referent'].collection,
74
+ 'referent_slug' => edge['referent'].slug,
75
+ 'referent_link' => edge['referent'].markdown_link,
76
+ })
77
+ end
78
+ end
79
+
80
+ def markdown_links(doc)
81
+ doc.to_s.scan(%r{(?<=[^!])\[.*?\]\(/.*?/.*?\)})
82
+ end
83
+
84
+ # returns link text, collection and slug
85
+ # [0](/1/2)
86
+ def link_components(link)
87
+ [link[%r{(?<=\[).*?(?=\])}], link[%r{(?<=/).*(?=/)}], link[%r{(?<=/)(?:(?!/).)*?(?=\))}]]
88
+ end
89
+
90
+ def find_page(collection, slug)
91
+ @site.collections[collection].docs.find { |doc| doc.data['slug'] == slug }
92
+ end
93
+
94
+ def page_missing(collection, slug)
95
+ @site.collections[collection].nil? || find_page(collection, slug).nil?
96
+ end
97
+
98
+ def edge(referent, reference)
99
+ referenced_name, referenced_collection, referenced_slug = link_components(reference)
100
+ if page_missing(referenced_collection, referenced_slug)
101
+ written = false
102
+ name = referenced_name
103
+ else
104
+ written = true
105
+ name = find_page(referenced_collection, referenced_slug).data['name']
106
+ end
107
+ {
108
+ 'referent' => referent,
109
+ 'reference' => CollectionPage.new(
110
+ name,
111
+ referenced_collection,
112
+ referenced_slug,
113
+ written
114
+ )
115
+ }
116
+ end
117
+
118
+ def referenced_in(collection, slug)
119
+ @graph.select {
120
+ |edge| edge['reference'].collection == collection && edge['reference'].slug == slug
121
+ }.map { |edge| edge['referent'] }
122
+ end
123
+
124
+ def unwritten_pages
125
+ @graph.select {
126
+ |edge| !edge['reference'].written
127
+ }
128
+ end
129
+
130
+ def refs_table_required(doc)
131
+ if doc.data.key?('refs')
132
+ doc.data['refs']
133
+ elsif @site.config['collections'][doc.collection.label].key?('refs')
134
+ @site.config['collections'][doc.collection.label]['refs']
135
+ elsif @site.config.key?('refs')
136
+ @site.config['refs']
137
+ end
138
+ end
139
+
140
+ def add_reference(doc, reference)
141
+ referent_collection = doc.collection.label
142
+ referent_page = doc.data['slug']
143
+
144
+ # Find part of markdown link that represents the collection and item
145
+ referenced_name, referenced_collection, referenced_page = link_components(reference)
146
+
147
+ @references[referenced_collection] = {} unless @references.key?(referenced_collection)
148
+ @references[referenced_collection][referenced_page] = {} unless @references[referenced_collection].key?(referenced_page)
149
+ @references[referenced_collection][referenced_page][referent_collection] = {} unless @references[referenced_collection][referenced_page].key?(referent_collection)
150
+
151
+ @references[referenced_collection][referenced_page][referent_collection][referent_page] = doc.data['name']
152
+ end
153
+
154
+ def refs_table(refs)
155
+ table = <<~TABLE
156
+ # Referenced By:
157
+ <table>
158
+ <thead>
159
+ <tr>
160
+ <th>Collection</th>
161
+ <th>Links</th>
162
+ </tr>
163
+ </thead>
164
+ <tbody>
165
+ #{refs_rows(refs)}
166
+ </tbody>
167
+ </table>
168
+ TABLE
169
+ table
170
+ end
171
+
172
+ def refs_rows(refs)
173
+ row = ''
174
+ refs.each do |reference|
175
+ row += <<~ROW
176
+ <tr>
177
+ <td markdown="span"><b> #{reference[0].capitalize} </b></td>
178
+ <td markdown="span"> #{refs_links(reference)} </td>
179
+ </tr>
180
+ ROW
181
+ end
182
+ row
183
+ end
184
+
185
+ def refs_links(reference)
186
+ links = ''
187
+ reference[1].each do |link|
188
+ links += " - #{link} <br>"
189
+ end
190
+ links
191
+ end
192
+ end
193
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-rpg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Lockwood
@@ -30,7 +30,9 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
+ - lib/collection_page.rb
33
34
  - lib/jekyll-rpg.rb
35
+ - lib/references.rb
34
36
  homepage: https://github.com/tomlockwood/jekyll-rpg
35
37
  licenses:
36
38
  - MIT
@@ -43,7 +45,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
45
  requirements:
44
46
  - - ">="
45
47
  - !ruby/object:Gem::Version
46
- version: '0'
48
+ version: 2.6.1
47
49
  required_rubygems_version: !ruby/object:Gem::Requirement
48
50
  requirements:
49
51
  - - ">="