-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjekyll_offline.rb
executable file
·75 lines (62 loc) · 1.74 KB
/
jekyll_offline.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
#!/usr/bin/env ruby
require 'erb'
require 'fileutils'
require 'uri'
require 'yaml'
require_relative 'lib_rellinks.rb'
def copy_src_dir(src, trg)
dir_contents = Dir.glob(src + "*")
dir_contents.each do |d|
basename = File.basename(d)
if basename == ".git"
next
end
FileUtils.cp_r(d, trg)
end
end
def preprocess_html(html)
html = prune_base(html)
end
def postprocess_html(html)
html.gsub(/https:\/\/@/, "https://")
end
def boilerplate(out_dir)
$site_title = @config[:site_title]
$site_url = @config[:site_url]
$site_logo = @config[:site_logo]
content = ERB.new(File.read("template.rhtml")).result
File.open(out_dir + "START_HERE.html", "w") {|f| f << content }
end
def prune_base(html)
if @config[:relative_base]
html.gsub(/(href|src|data|value)(=["'])#{@config[:relative_base]}/, "\\1\\2")
else
html
end
end
def clone_local_site(absolute_base, source_dir, out_dir)
FileUtils.rm_rf(out_dir)
FileUtils.mkdir_p(out_dir)
boilerplate(out_dir)
out_dir = out_dir + "site/"
FileUtils.mkdir_p(out_dir)
copy_src_dir(source_dir, out_dir)
pages = Dir.glob(out_dir + "**/*.html")
pages.each do |p|
page_content = File.read(p)
basename = File.basename(p)
html = preprocess_html(page_content)
page_output = convert_html(html, p, out_dir, absolute_base)
page_output = postprocess_html(page_output)
File.open(p, "w") {|f| f << page_output }
end
end
@config = YAML::load(File.read("config.yml"))
custom_config = ARGV[0]
if custom_config
@config = YAML::load(File.read(custom_config))
end
absolute_base = @config[:absolute_base]
source_dir = @config[:source_dir].gsub(/^~/, Dir.home)
out_dir = @config[:out_dir].gsub(/^~/, Dir.home)
clone_local_site(absolute_base, source_dir, out_dir)