jekyll-rpg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyll-rpg.rb +150 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c8401558390fb5a2caea3e00dcda596df06996e18faa86de3882376369ad17a1
4
+ data.tar.gz: c6311b8c8c669eb490fb7dc1d5b5eac426abb2580ce7ed88de561917fa0739a2
5
+ SHA512:
6
+ metadata.gz: 31280e6dc53b72c9817de32a737cdc4da39f9378bd2af186aeb86fd0c480ecc2c4dabac9f909348f347709e7574c2faf2729cf794e92ee516ac2f0abee08d2bc
7
+ data.tar.gz: d3f33f666a3e6c02094081af9f5be9fad47131af223ddd72fe54d8835a783dbae1aa1e5c3a29b897777f7999d8391dddd22f9f1573cc0754c6cb4f6ee3ac70c0
data/lib/jekyll-rpg.rb ADDED
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll'
4
+
5
+ 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
+ # Bi-directional page links
143
+ Jekyll::Hooks.register :site, :post_read do |site|
144
+ ref = ReferenceTracker.new(site)
145
+
146
+ site.data['graph'] = ref.references
147
+
148
+ site.data['broken_links'] = ref.broken_links
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-rpg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tom Lockwood
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ description: ''
28
+ email: tom@lockwood.dev
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/jekyll-rpg.rb
34
+ homepage: https://github.com/tomlockwood/jekyll-rpg
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.0.1
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Jekyll plugin for managing RPG information for DMs
57
+ test_files: []