forked from mkredpoint/braze-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias_generator.rb
137 lines (123 loc) · 4.28 KB
/
alias_generator.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
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
# Alias Generator for Posts.
#
# Generates redirect pages for posts with aliases set in the YAML Front Matter.
#
# Place the full path of the alias (place to redirect from) inside the
# destination post's YAML Front Matter. One or more aliases may be given.
#
# Example Post Configuration:
#
# ---
# layout: post
# title: "How I Keep Limited Pressing Running"
# alias: /post/6301645915/how-i-keep-limited-pressing-running/index.html
# ---
#
# Example Post Configuration:
#
# ---
# layout: post
# title: "How I Keep Limited Pressing Running"
# alias: [/first-alias/index.html, /second-alias/index.html]
# ---
#
# Author: Thomas Mango
# Site: http://thomasmango.com
# Plugin Source: http://github.com/tsmango/jekyll_alias_generator
# Site Source: http://github.com/tsmango/tsmango.github.com
# Plugin License: MIT
#
# Customized to use documents to process all files instead of just pages and post.
#
# Fix conflicts with other generators by inlining function to return static files
# so Jekyll doesn't clean it. Probably better way to do it.
module Jekyll
class AliasGenerator < Jekyll::Generator
def generate(site)
#@site = site
# process_posts
# process_pages
site.documents.each do |docs|
alias_paths ||= Array.new
alias_paths << docs.data['alias']
alias_paths.compact!
alias_paths.flatten.each do |alias_path|
alias_path = File.join('/', alias_path.to_s)
alias_dir = File.extname(alias_path).empty? ? alias_path : File.dirname(alias_path)
alias_file = File.extname(alias_path).empty? ? "index.html" : File.basename(alias_path)
fs_path_to_dir = File.join(site.dest, alias_dir)
alias_sections = alias_dir.split('/')[1..-1]
FileUtils.mkdir_p(fs_path_to_dir)
File.open(File.join(fs_path_to_dir, alias_file), 'w') do |file|
file.write(alias_template(site,docs.url))
end
# Unneeded since folder path for alias should already be created and causes warnings
# alias_sections.size.times do |sections|
# site.static_files << Jekyll::AliasFile.new(site, site.dest, alias_sections[0, sections + 1].join('/'), '')
# end
site.static_files << Jekyll::AliasFile.new(site, site.dest, alias_dir, alias_file)
end
end
end
# def process_posts
# @site.posts.each do |post|
# generate_aliases(post.url, post.data['alias'])
# end
# end
#
# def process_pages
# @site.pages.each do |page|
# generate_aliases(page.destination('').gsub(/index\.(html|htm)$/, ''), page.data['alias'])
# end
# end
#
# def generate_aliases(destination_path, aliases)
# alias_paths ||= Array.new
# alias_paths << aliases
# alias_paths.compact!
#
# alias_paths.flatten.each do |alias_path|
# alias_path = File.join('/', alias_path.to_s)
#
# alias_dir = File.extname(alias_path).empty? ? alias_path : File.dirname(alias_path)
# alias_file = File.extname(alias_path).empty? ? "index.html" : File.basename(alias_path)
#
# fs_path_to_dir = File.join(@site.dest, alias_dir)
# alias_sections = alias_dir.split('/')[1..-1]
#
# FileUtils.mkdir_p(fs_path_to_dir)
#
# File.open(File.join(fs_path_to_dir, alias_file), 'w') do |file|
# file.write(alias_template(@site,destination_path))
# end
#
# alias_sections.size.times do |sections|
# @site.static_files << Jekyll::AliasFile.new(@site, @site.dest, alias_sections[0, sections + 1].join('/'), '')
# end
# @site.static_files << Jekyll::AliasFile.new(@site, @site.dest, alias_dir, alias_file)
# end
# end
def alias_template(site,destination_path)
destination_url = site.config['baseurl'] + destination_path
<<-EOF
<!DOCTYPE html>
<html>
<head>
<link rel="canonical" href="#{destination_url}"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url=#{destination_url}" />
</head>
</html>
EOF
end
end
class AliasFile < StaticFile
require 'set'
def modified?
return false
end
def write(dest)
return true
end
end
end