-
Notifications
You must be signed in to change notification settings - Fork 17
/
categories.rb
74 lines (51 loc) · 2.15 KB
/
categories.rb
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
module Hardwired
class Template
def reachable_in_versions(versions)
current_version = versions.select{|b| self.path.start_with?(b[:folder])}.first
if current_version.nil?
#STDERR << "#{self.path} not found in versioned folder\n"
return []
end
versions.map do |hash|
prefix = hash[:folder]
rel_path = self.path[current_version[:folder].length..-1]
other_version = prefix + rel_path
dest = Hardwired::Aliases::AliasTable.get_final_destination(other_version, other_version) || other_version
final_page = Hardwired::Index[dest]
#STDERR << "#{other_version} mapped to #{dest} #{final_page.nil? ? '[missing!]' : '[exists]'}. \n"
final_page.nil? ? nil : {active: hash == current_version, page_path: dest}.merge(hash)
end.compact
end
def after_load
#Evaluate all categories, assign meta.sidebar and meta.primary_category
@@cats ||= Hardwired::Config.config.categories
@@cats_by_tag ||= Hash[@@cats.map{ |c| [c.tag.to_s,c] }]
matches = @@cats.select do |c|
tag?(c.tag) || [c.include].flatten.select { |p| path.start_with?(p) }.length > 0
end
unless matches.empty?
#Join with existing category tags
meta.categories = (meta.categories || []) | matches.map { |c| c.tag }
#Select the first category from the union
meta.category ||= meta.categories.first
c = @@cats_by_tag[meta.category]
#Clone all specified metadata, without overwriting anything
@meta = NormalizingRecursiveOpenStruct.new(c.meta.to_hash.merge((meta || {}).to_hash)) unless c.nil? || c.meta.nil?
#p "Categorized #{path} as #{meta.categories * ','} (primary #{meta.category})"
#Add the primary category to the tags
meta.tags = tags | [meta.category]
end
end
end
end
module Tilt
class KramdownTemplate < Template
def prepare
options[:smart_quotes] = DUMB_QUOTES unless options[:smartypants]
options[:input] = :GFM
options[:hard_wrap] = false
@engine = Kramdown::Document.new(data, options)
@output = nil
end
end
end