custom_update_tags 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/update_tags.rb +109 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 137c74328662ed0111ef7ec7c808ac2589953a70106143f02bef2437d6340e21
|
4
|
+
data.tar.gz: d6e9f3875bebc72b09e4c7ce0ec78859e7942a9e35179627e9f4fdbbddc3ab4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1d8200b15bc48510fa3b28b631ab8c712259a9d9acd1ff4d9802004d53942a9ec440d2cdb90e569393436fb587de7d79fd2ca76f35974572f75085a790fed32
|
7
|
+
data.tar.gz: bdd27c290bcb1a055cef16a2d18e5c9925ebacb4718ae9ae04a06a2e9fcbfdb2d37b940056f6daf603713f1be7e75da052b155faea08b811d028a9566ce25235
|
data/lib/update_tags.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require "front_matter_parser"
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
class UpdateTags
|
5
|
+
def self.update(tag_directory:, path_to_tag_index_page_template:)
|
6
|
+
new(tag_directory, path_to_tag_index_page_template).update
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :tag_directory, :path_to_tag_index_page_template
|
10
|
+
|
11
|
+
def initialize(tag_directory, path_to_tag_index_page_template)
|
12
|
+
@tag_directory = tag_directory
|
13
|
+
@path_to_tag_index_page_template = path_to_tag_index_page_template
|
14
|
+
end
|
15
|
+
|
16
|
+
def update
|
17
|
+
print "Updating tags..."
|
18
|
+
delete_tag_pages
|
19
|
+
create_tag_pages
|
20
|
+
print " Done.\n"
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def delete_tag_pages
|
26
|
+
FileUtils.rm_rf(tag_directory)
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_tag_pages
|
30
|
+
FileUtils.mkdir(tag_directory)
|
31
|
+
|
32
|
+
tags.each do |tag|
|
33
|
+
File.write(
|
34
|
+
tag_index_page_path(tag),
|
35
|
+
tag_index_page_source(tag),
|
36
|
+
)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def blog_post_filenames
|
41
|
+
directories = ["./_posts", "./_building", "./_writing", "./_notes"]
|
42
|
+
@blog_post_filenames ||= directories.inject([]) do |acc, directory|
|
43
|
+
entries = Dir.entries(directory) - [".", ".."]
|
44
|
+
entries = entries.map { |entry| "#{directory}/#{entry}" } # Prepend the directory path
|
45
|
+
acc + entries
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
def tags
|
51
|
+
@tags ||= blog_post_filenames.inject([]) do |tags, blog_post_filename|
|
52
|
+
tags += FrontMatterParser::Parser
|
53
|
+
.parse_file(blog_post_file(blog_post_filename))
|
54
|
+
.front_matter["tags"]
|
55
|
+
end.uniq
|
56
|
+
end
|
57
|
+
|
58
|
+
def blog_post_file(blog_post_filename)
|
59
|
+
blog_post_filename
|
60
|
+
end
|
61
|
+
|
62
|
+
def tag_index_page_path(tag)
|
63
|
+
"#{tag_directory}/#{url_friendly_tag(tag)}.md"
|
64
|
+
end
|
65
|
+
|
66
|
+
def url_friendly_tag(tag)
|
67
|
+
tag.downcase.split(" ").join("-")
|
68
|
+
end
|
69
|
+
|
70
|
+
def tag_index_page_front_matter_parser
|
71
|
+
@tag_index_page_front_matter_parser ||= FrontMatterParser::Parser
|
72
|
+
.parse_file(path_to_tag_index_page_template)
|
73
|
+
end
|
74
|
+
|
75
|
+
def tag_index_page_front_matter
|
76
|
+
@tag_index_page_front_matter ||= \
|
77
|
+
tag_index_page_front_matter_parser.front_matter.map do |key, value|
|
78
|
+
"#{key}: #{value}"
|
79
|
+
end.join("\n")
|
80
|
+
end
|
81
|
+
|
82
|
+
def tag_index_page_content
|
83
|
+
@tag_index_page_content ||= tag_index_page_front_matter_parser.content
|
84
|
+
end
|
85
|
+
|
86
|
+
def injected_liquid_for_tag_index_page(tag)
|
87
|
+
<<~INJECTED_LIQUID
|
88
|
+
{% assign tag = "#{tag}" %}
|
89
|
+
{% assign tagged_posts = "" | split: "" %}
|
90
|
+
{% for post in site.posts %}
|
91
|
+
{% if post.tags contains tag %}
|
92
|
+
{% assign tagged_posts = tagged_posts | push: post %}
|
93
|
+
{% endif %}
|
94
|
+
{% endfor %}
|
95
|
+
INJECTED_LIQUID
|
96
|
+
end
|
97
|
+
|
98
|
+
def tag_index_page_source(tag)
|
99
|
+
<<~TAG_INDEX_PAGE
|
100
|
+
---
|
101
|
+
#{tag_index_page_front_matter}
|
102
|
+
---
|
103
|
+
|
104
|
+
#{injected_liquid_for_tag_index_page(tag)}
|
105
|
+
|
106
|
+
#{tag_index_page_content}
|
107
|
+
TAG_INDEX_PAGE
|
108
|
+
end
|
109
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_update_tags
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Pachulski
|
@@ -77,6 +77,7 @@ extensions: []
|
|
77
77
|
extra_rdoc_files: []
|
78
78
|
files:
|
79
79
|
- bin/update_tags
|
80
|
+
- lib/update_tags.rb
|
80
81
|
homepage: https://github.com/pachun/update_tags
|
81
82
|
licenses:
|
82
83
|
- MIT
|