jekyll-rpg 0.0.4 → 0.0.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: 4dbaff86bfac3c1c821e36321851b94f3cc5e7dfa293543dc49809dfa3f331b1
4
- data.tar.gz: e45e59b1260ae0b3f7c853e1780e436d123df64e604fdefbeac1f75bd1b036d1
3
+ metadata.gz: 16f14fd9c3d40e94808552f1b4a38813cb1f75e97a50ab5f69acbd57026dadb2
4
+ data.tar.gz: 770ce2e9837de58c045cf32a2b20272f65b237420f3c733ade1081d6aee6d7b4
5
5
  SHA512:
6
- metadata.gz: cf357ffe093de9d47beb3f7d9eb26ebe555c6ae312e10d54f397b81e7c6b0628cb083ef8f5f97bee46997970380531eb1ba7ae5c64411cd8b64cbd9d478f0442
7
- data.tar.gz: 645a38316b3b70083c0783c2591f9565fbeca5c81fee926ceba3cbb6c3d6d785a595de74b2a0e74eb5155d62fc9eb50a5a519f303ec2e353435782d2d2c529b7
6
+ metadata.gz: 3e0dd4929f15a759ead437924a0ed5efc49e336a1cdb14474b5b008e060af0300f693c8280e8e7e9f921c8a40eaad10070b7acac786cbcadc0bc5a86ba198018
7
+ data.tar.gz: 620e850bb614a48458c7b78326af3d3a45f47c620b597d8a4aef217f9ded8106a2be791cb7278224f0f35d892f79c7b72b0fce1a279fa6fefebdeefcbd8f8aad
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllRPG
4
+ # Represents a document that may be in a Jekyll collection
5
+ class CollectionDocument
6
+ attr_accessor :name, :collection, :slug, :written
7
+
8
+ def extract_doc(doc)
9
+ @name = doc.data['name']
10
+ @collection = doc.collection.label
11
+ @slug = doc.data['slug']
12
+ @written = true
13
+ self
14
+ end
15
+
16
+ # extracts link text, collection and slug
17
+ # [@name](/@collection/@slug)
18
+ def extract_markdown(site, link)
19
+ @collection = link[%r{(?<=/).*(?=/)}]
20
+ @slug = link[%r{(?<=/)(?:(?!/).)*?(?=\))}]
21
+ @written = document_exists(site)
22
+ @name = @written ? find_document(site).data['name'] : link[/(?<=\[).*?(?=\])/]
23
+ self
24
+ end
25
+
26
+ # Checks whether document exists in a site
27
+ def document_exists(site)
28
+ !site.collections[@collection].nil? && !find_document(site).nil?
29
+ end
30
+
31
+ # Find a document based on its collection and slug
32
+ def find_document(site)
33
+ site.collections[@collection].docs.find { |doc| doc.data['slug'] == @slug }
34
+ end
35
+
36
+ def markdown_link
37
+ "[#{@name}](/#{@collection}/#{@slug})"
38
+ end
39
+
40
+ def hash
41
+ {
42
+ 'name' => @name,
43
+ 'collection' => @collection,
44
+ 'slug' => @slug,
45
+ 'link' => markdown_link
46
+ }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllRPG
4
+ # Edge between two documents
5
+ class Edge
6
+ attr_accessor :reference, :referent
7
+
8
+ def initialize(referent, reference)
9
+ @referent = referent
10
+ @reference = reference
11
+ end
12
+
13
+ def hash
14
+ {
15
+ 'reference' => reference.hash,
16
+ 'referent' => referent.hash
17
+ }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllRPG
4
+ # Graph of relationships between CollectionDocuments
5
+ class Graph
6
+ attr_accessor :edges
7
+
8
+ def initialize
9
+ @edges = []
10
+ end
11
+
12
+ # Get the information for every page the current doc is referenced in
13
+ # And push links to an array that represents the collections of those pages
14
+ def document_references(doc)
15
+ document_hash = {}
16
+
17
+ referenced_in(doc).each do |reference|
18
+ document_hash[reference.collection] = [] unless document_hash.key?(reference.collection)
19
+ document_hash[reference.collection].push(reference.markdown_link)
20
+ end
21
+
22
+ document_hash.each do |k, v|
23
+ document_hash[k] = v.uniq
24
+ end
25
+ document_hash
26
+ end
27
+
28
+ # Based on the graph, returns edges that a specific document is the referent of
29
+ def referenced_in(doc)
30
+ collection = doc.collection.label
31
+ slug = doc.data['slug']
32
+ @edges.select do |edge|
33
+ edge.reference.collection == collection && edge.reference.slug == slug
34
+ end.map(&:referent)
35
+ end
36
+
37
+ # Based on the graph, returns documents that are referenced, but do not exist yet
38
+ def unwritten
39
+ @edges.reject do |edge|
40
+ edge.reference.written
41
+ end
42
+ end
43
+
44
+ def hash
45
+ @edges.map(&:hash)
46
+ end
47
+ end
48
+ end
@@ -9,7 +9,7 @@ module JekyllRPG
9
9
  Jekyll::Hooks.register :site, :post_read do |site|
10
10
  ref = References.new(site)
11
11
 
12
- site.data['graph'] = ref.hashed_graph
12
+ site.data['graph'] = ref.graph.hash
13
13
 
14
14
  site.data['broken_links'] = ref.broken_links
15
15
  end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JekyllRPG
4
+ # Generates Reference Table
5
+ class ReferenceTable
6
+ attr_accessor :html
7
+
8
+ def initialize(site, doc)
9
+ @html = ''
10
+ @html = refs_table(doc.data['referenced_by']) if refs_table_required(site, doc)
11
+ end
12
+
13
+ # Determines if refs table is required based on document,
14
+ # then collection, then site
15
+ def refs_table_required(site, doc)
16
+ if doc.data.key?('refs')
17
+ doc.data['refs']
18
+ elsif site.config['collections'][doc.collection.label].key?('refs')
19
+ site.config['collections'][doc.collection.label]['refs']
20
+ elsif site.config.key?('refs')
21
+ site.config['refs']
22
+ end
23
+ end
24
+
25
+ def refs_table(refs)
26
+ table = <<~TABLE
27
+ # Referenced By:
28
+ <table>
29
+ <thead>
30
+ <tr>
31
+ <th>Collection</th>
32
+ <th>Links</th>
33
+ </tr>
34
+ </thead>
35
+ <tbody>
36
+ #{refs_rows(refs)}
37
+ </tbody>
38
+ </table>
39
+ TABLE
40
+ table
41
+ end
42
+
43
+ def refs_rows(refs)
44
+ row = ''
45
+ refs.each do |reference|
46
+ row += <<~ROW
47
+ <tr>
48
+ <td markdown="span"><b> #{reference[0].capitalize} </b></td>
49
+ <td markdown="span"> #{refs_links(reference)} </td>
50
+ </tr>
51
+ ROW
52
+ end
53
+ row
54
+ end
55
+
56
+ def refs_links(reference)
57
+ links = ''
58
+ reference[1].each do |link|
59
+ links += " - #{link} <br>"
60
+ end
61
+ links
62
+ end
63
+ end
64
+ end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'collection_page'
4
- require 'pry'
3
+ require 'collection_document'
4
+ require 'edge'
5
+ require 'graph'
6
+ require 'reference_table'
5
7
 
6
8
  module JekyllRPG
7
9
  # References within Jekyll Collections
@@ -10,59 +12,53 @@ module JekyllRPG
10
12
 
11
13
  def initialize(site)
12
14
  @site = site
13
- @graph = []
15
+ @graph = Graph.new
14
16
  @broken_links = []
15
17
  @collection_keys = @site.collections.keys - ['posts']
16
18
 
19
+ reference_pass
20
+ referent_pass
21
+
22
+ # Create list of broken links
23
+ @graph.unwritten.each do |edge|
24
+ @broken_links.push(edge.hash)
25
+ end
26
+ end
27
+
28
+ # Generating data on how documents reference other documents
29
+ def reference_pass
17
30
  # Parse references from markdown links
18
- @collection_keys.each do |collection|
19
- @site.collections[collection].docs.each do |doc|
20
- if doc.data['dm'] && !@site.config['dm_mode']
21
- doc.data['published'] = false
22
- else
23
- referent = CollectionPage.new(
24
- doc.data['name'],
25
- doc.collection.label,
26
- doc.data['slug'],
27
- true
28
- )
29
- markdown_links(doc).each do |reference|
30
- @graph.push(edge(referent, reference))
31
- end
31
+ collection_documents.each do |doc|
32
+ # Do not publish or reference a page if the site is not in DM Mode
33
+ # And the page is marked as for dms
34
+ if doc.data['dm'] && !@site.config['dm_mode']
35
+ doc.data['published'] = false
36
+ else
37
+ referent = CollectionDocument.new.extract_doc(doc)
38
+ markdown_links(doc).each do |link|
39
+ reference = CollectionDocument.new.extract_markdown(@site, link)
40
+ @graph.edges.push(Edge.new(referent, reference))
32
41
  end
33
42
  end
34
43
  end
44
+ end
35
45
 
46
+ # Generating data on how documents are referenced to
47
+ def referent_pass
36
48
  # 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 = {}
49
+ collection_documents.each do |doc|
50
+ # Put the reference data on the doc
51
+ doc.data['referenced_by'] = @graph.document_references(doc)
40
52
 
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
53
+ # If the references table option is configured, append the table
54
+ table = ReferenceTable.new(@site, doc).html
55
+ doc.content = doc.content + table
61
56
  end
57
+ end
62
58
 
63
- # Create list of broken links
64
- unwritten_pages.each do |edge|
65
- @broken_links.push(edge_hash(edge))
59
+ def collection_documents
60
+ @collection_keys.flat_map do |collection|
61
+ @site.collections[collection].docs
66
62
  end
67
63
  end
68
64
 
@@ -72,130 +68,5 @@ module JekyllRPG
72
68
  def markdown_links(doc)
73
69
  doc.to_s.scan(%r{(?<=[^!])\[.*?\]\(/.*?/.*?\)})
74
70
  end
75
-
76
- # returns link text, collection and slug
77
- # [0](/1/2) - as a [0, 1, 2]
78
- def link_components(link)
79
- [link[/(?<=\[).*?(?=\])/], link[%r{(?<=/).*(?=/)}], link[%r{(?<=/)(?:(?!/).)*?(?=\))}]]
80
- end
81
-
82
- # Find a document based on its collection and slug
83
- def find_page(collection, slug)
84
- @site.collections[collection].docs.find { |doc| doc.data['slug'] == slug }
85
- end
86
-
87
- # Returns true if document cannot be found in collection
88
- def page_missing(collection, slug)
89
- @site.collections[collection].nil? || find_page(collection, slug).nil?
90
- end
91
-
92
- # Returns a hash of two CollectionPages representing a graph edge
93
- def edge(referent, reference)
94
- referenced_name, referenced_collection, referenced_slug = link_components(reference)
95
- if page_missing(referenced_collection, referenced_slug)
96
- written = false
97
- name = referenced_name
98
- else
99
- written = true
100
- name = find_page(referenced_collection, referenced_slug).data['name']
101
- end
102
- {
103
- 'referent' => referent,
104
- 'reference' => CollectionPage.new(
105
- name,
106
- referenced_collection,
107
- referenced_slug,
108
- written
109
- )
110
- }
111
- end
112
-
113
- # Based on the graph, returns edges that a specific document is the referent of
114
- def referenced_in(collection, slug)
115
- @graph.select do |edge|
116
- edge['reference'].collection == collection && edge['reference'].slug == slug
117
- end.map { |edge| edge['referent'] }
118
- end
119
-
120
- # Based on the graph, returns documents that are referenced, but do not exist yet
121
- def unwritten_pages
122
- @graph.reject do |edge|
123
- edge['reference'].written
124
- end
125
- end
126
-
127
- # Determines if refs table is required based on document,
128
- # then collection, then site
129
- def refs_table_required(doc)
130
- if doc.data.key?('refs')
131
- doc.data['refs']
132
- elsif @site.config['collections'][doc.collection.label].key?('refs')
133
- @site.config['collections'][doc.collection.label]['refs']
134
- elsif @site.config.key?('refs')
135
- @site.config['refs']
136
- end
137
- end
138
-
139
- # Returns an easily accessible hash representing an edge for Jekyll purposes
140
- def edge_hash(edge)
141
- {
142
- 'reference_name' => edge['reference'].name,
143
- 'reference_collection' => edge['reference'].collection,
144
- 'reference_slug' => edge['reference'].slug,
145
- 'reference_link' => edge['reference'].markdown_link,
146
- 'referent_name' => edge['referent'].name,
147
- 'referent_collection' => edge['referent'].collection,
148
- 'referent_slug' => edge['referent'].slug,
149
- 'referent_link' => edge['referent'].markdown_link
150
- }
151
- end
152
-
153
- # Returns a graph made up of hashed edges
154
- def hashed_graph
155
- @graph.map { |edge| edge_hash(edge) }
156
- end
157
-
158
- # The following three functions return a HTML table
159
- # That for a specific document shows the documents that
160
- # reference it
161
-
162
- def refs_table(refs)
163
- table = <<~TABLE
164
- # Referenced By:
165
- <table>
166
- <thead>
167
- <tr>
168
- <th>Collection</th>
169
- <th>Links</th>
170
- </tr>
171
- </thead>
172
- <tbody>
173
- #{refs_rows(refs)}
174
- </tbody>
175
- </table>
176
- TABLE
177
- table
178
- end
179
-
180
- def refs_rows(refs)
181
- row = ''
182
- refs.each do |reference|
183
- row += <<~ROW
184
- <tr>
185
- <td markdown="span"><b> #{reference[0].capitalize} </b></td>
186
- <td markdown="span"> #{refs_links(reference)} </td>
187
- </tr>
188
- ROW
189
- end
190
- row
191
- end
192
-
193
- def refs_links(reference)
194
- links = ''
195
- reference[1].each do |link|
196
- links += " - #{link} <br>"
197
- end
198
- links
199
- end
200
71
  end
201
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-rpg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Lockwood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-19 00:00:00.000000000 Z
11
+ date: 2019-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: factory_bot
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: pry
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -58,8 +72,11 @@ executables: []
58
72
  extensions: []
59
73
  extra_rdoc_files: []
60
74
  files:
61
- - lib/collection_page.rb
75
+ - lib/collection_document.rb
76
+ - lib/edge.rb
77
+ - lib/graph.rb
62
78
  - lib/jekyll-rpg.rb
79
+ - lib/reference_table.rb
63
80
  - lib/references.rb
64
81
  homepage: https://github.com/tomlockwood/jekyll-rpg
65
82
  licenses:
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JekyllRPG
4
- # Represents a document that may be in a Jekyll collection
5
- class CollectionPage
6
- attr_accessor :name, :collection, :slug, :written
7
-
8
- def initialize(name, collection, slug, written)
9
- @name = name
10
- @collection = collection
11
- @slug = slug
12
- # boolean
13
- @written = written
14
- end
15
-
16
- def markdown_link
17
- "[#{@name}](/#{@collection}/#{@slug})"
18
- end
19
- end
20
- end