-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRakefile
More file actions
211 lines (169 loc) · 6.39 KB
/
Copy pathRakefile
File metadata and controls
211 lines (169 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
require "date"
require "English"
require "fileutils"
require "open3"
require "set"
require "uri"
require "yaml"
POSTS_DIR = "_posts"
ASSET_DIRS = ["assets", "img"].freeze
POST_FILE_RE = /\A\d{4}-\d{2}-\d{2}-.+\.md\z/
RAW_CODE_ALLOWLIST = Set.new.freeze
def post_files
Dir.glob("#{POSTS_DIR}/**/*.md").sort
end
def slugify(value)
value
.downcase
.gsub(/&/, " and ")
.gsub(/[^a-z0-9]+/, "-")
.gsub(/\A-+|-+\z/, "")
.gsub(/-{2,}/, "-")
end
def parse_tags(value)
value.to_s.split(",").map(&:strip).reject(&:empty?)
end
def read_frontmatter(source)
normalized = source.gsub("\r\n", "\n")
return nil unless normalized.start_with?("---\n")
end_index = normalized.index("\n---", 4)
return nil unless end_index
normalized[4...end_index]
end
def frontmatter_hash(frontmatter)
YAML.safe_load(frontmatter, permitted_classes: [Date, Time], aliases: true) || {}
rescue Psych::SyntaxError
nil
end
def strip_fenced_code(source)
source.gsub(/```[\s\S]*?```/, "")
end
def raw_script_or_style?(source)
strip_fenced_code(source).lines.any? { |line| line.match?(/\A\s{0,3}<(script|style)\b/i) }
end
def local_asset_paths(source)
patterns = [
/!\[[^\]]*\]\((\/(?:assets|img)\/[^)\n]+)\)/,
/\b(?:src|href)=["'](\/(?:assets|img)\/[^"']+)["']/,
/^(?:image|og_image):\s*['"]?(\/(?:assets|img)\/[^'"\n]+)['"]?/,
]
patterns.flat_map { |pattern| source.scan(pattern).flatten }
.map { |path| URI.decode_www_form_component(path.strip.sub(/\s+["'][^"']+["']\z/, "")) }
.uniq
end
desc "Create a publish-ready post: bundle exec rake new_post title=\"...\" tags=\"research\" description=\"...\""
task :new_post do
title = ENV["title"] || ENV["TITLE"]
tags = parse_tags(ENV["tags"] || ENV["TAGS"])
description = ENV["description"] || ENV["DESCRIPTION"]
author = ENV["author"] || ENV["AUTHOR"]
date = ENV["date"] || ENV["DATE"] || Date.today.strftime("%Y-%m-%d")
slug = slugify(ENV["slug"] || ENV["SLUG"] || title.to_s)
draft = ENV["draft"] == "true" || ENV["DRAFT"] == "true"
abort "Missing title=\"...\"" if title.to_s.strip.empty?
abort "Missing tags=\"...\"" if tags.empty?
abort "Missing description=\"...\"" if description.to_s.strip.empty?
abort "Date must be YYYY-MM-DD" unless date.match?(/\A\d{4}-\d{2}-\d{2}\z/)
abort "Slug is empty after normalization; pass slug=\"...\"" if slug.empty?
Date.iso8601(date)
FileUtils.mkdir_p(POSTS_DIR)
path = File.join(POSTS_DIR, "#{date}-#{slug}.md")
abort "Post already exists: #{path}" if File.exist?(path)
frontmatter = [
"---",
"title: '#{title.gsub("'", "''")}'",
"header: '#{title.gsub("'", "''")}'",
"description: '#{description.gsub("'", "''")}'",
(author ? "author: '#{author.gsub("'", "''")}'" : nil),
"tags: [#{tags.map { |tag| "'#{tag.gsub("'", "''")}'" }.join(", ")}]",
(draft ? "draft: true" : nil),
"---"
].compact.join("\n")
File.write(path, <<~POST)
#{frontmatter}
Write the opening paragraph here. Keep it specific enough to work as the listing excerpt.
<!--more-->
Continue the article here.
## Assets
Place local images and downloads under `assets/` and reference them with root-relative paths, for example `/assets/images/example.png`.
POST
year, month, = date.split("-")
puts "Created #{path}"
puts "Public URL: /#{year}/#{month}/#{slug}.html"
puts "Draft mode: visible with demo config only." if draft
end
desc "Validate posts, frontmatter, raw script/style policy, and local assets"
task :check_content do
errors = []
post_files.each do |path|
filename = File.basename(path)
source = File.read(path)
frontmatter = read_frontmatter(source)
errors << "#{path}: filename must be YYYY-MM-DD-slug.md" unless filename.match?(POST_FILE_RE)
unless frontmatter
errors << "#{path}: missing YAML frontmatter"
next
end
data = frontmatter_hash(frontmatter)
unless data
errors << "#{path}: invalid YAML frontmatter"
next
end
%w[title header].each do |key|
errors << "#{path}: missing required frontmatter key '#{key}'" if data[key].to_s.strip.empty?
end
tags = data["tags"]
errors << "#{path}: missing non-empty 'tags' frontmatter" unless tags.respond_to?(:any?) && tags.any?
if raw_script_or_style?(source) && !RAW_CODE_ALLOWLIST.include?(path)
errors << "#{path}: raw <script> or <style> is not allowed in posts"
end
local_asset_paths("#{frontmatter}\n#{source}").each do |asset|
file_path = asset.sub(%r{\A/}, "")
errors << "#{path}: referenced asset not found: #{asset}" unless File.exist?(file_path)
end
end
if errors.any?
warn "Content check failed:"
errors.each { |error| warn "- #{error}" }
exit 1
end
puts "Content check OK (#{post_files.length} posts)."
end
desc "Report conservative content cleanup candidates without failing"
task :audit_content do
missing_excerpt = []
raw_html = []
http_links = []
jekyll_patterns = []
referenced_assets = Set.new
post_files.each do |path|
source = File.read(path)
missing_excerpt << path unless source.include?("<!--more-->")
raw_html << path if raw_script_or_style?(source)
http_links << path if source.match?(%r{http://})
jekyll_patterns << path if source.match?(/\{\%|\{\{/) || source.match?(/\{:\s*\./)
local_asset_paths(source).each { |asset| referenced_assets << asset.sub(%r{\A/}, "") }
end
assets = ASSET_DIRS.flat_map { |dir| Dir.glob("#{dir}/**/*").select { |entry| File.file?(entry) } }
unreferenced = assets.reject { |asset| referenced_assets.include?(asset) }
{
"Posts without <!--more--> excerpt marker" => missing_excerpt,
"Posts containing raw <script> or <style>" => raw_html,
"Posts containing http:// links" => http_links,
"Posts containing possible Jekyll/Liquid patterns" => jekyll_patterns,
"Potentially unreferenced public assets" => unreferenced
}.each do |title, entries|
next if entries.empty?
puts "\n#{title} (#{entries.length})"
entries.each { |entry| puts "- #{entry}" }
end
puts "\nContent audit completed. This report is informational and does not fail CI."
end
desc "Run bundler-audit"
task :security_audit do
system("bundle", "exec", "bundle-audit", "check", "--update") || exit($CHILD_STATUS.exitstatus || 1)
end
desc "Run the local CI gate"
task ci: [:check_content, :audit_content, :security_audit] do
system("bundle", "exec", "jekyll", "build") || exit($CHILD_STATUS.exitstatus || 1)
end