-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
194 lines (162 loc) · 5.29 KB
/
Rakefile
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
=begin "Rakefile" v0.3.0 | 2021/12/10 | by Tristano Ajmone
================================================================================
Rakefile draft.
System Requirements:
* Ruby:
* Asciidoctor
* Rouge
* Node.js
================================================================================
=end
# Custom helpers:
require './assets/rake/globals.rb'
require './assets/rake/asciidoc.rb'
require './assets/rake/html_chunker.rb'
# ==============================================================================
# -----------------:{ T O O L C H A I N S E T T I N G S }:------------------
# ==============================================================================
SRC_DIR = 'docs_src'
WWW_DIR = 'docs'
PUB_DIR = "#{WWW_DIR}/download"
BOOK_SRC = "#{SRC_DIR}/gitbuch.asciidoc"
BOOK_DEPS = FileList[
BOOK_SRC,
"#{SRC_DIR}/*.adoc",
"#{SRC_DIR}/images/*.svg",
"#{SRC_DIR}/images/*.png"
]
BOOK_PREV = "#{SRC_DIR}/Git-Book_Preview.html"
BOOK_HTML = "#{PUB_DIR}/Git-Book.html"
CHUNKER_SRC = "#{SRC_DIR}/Git-Book_Chunker-Input.html"
## Asciidoctor Options
######################
# Options common to all docs (let's keep it DRY):
ADOC_OPTS_SHARED = <<~HEREDOC
--failure-level WARN \
--verbose \
--timings \
--safe-mode unsafe \
-a source-highlighter=rouge \
-a rouge-style=monokai.sublime \
-a imagesdir=images \
-a experimental \
-a icons=font \
-a linkattrs \
-a reproducible \
-a sectanchors \
-a toc=left
HEREDOC
## Asciidoctor-Chunker
######################
CHUNKER_JS = "#{$repo_root}/assets/node-js/asciidoctor-chunker_mod.js"
# ==============================================================================
# -------------------------:{ M A I N T A S K S }:--------------------------
# ==============================================================================
# Building a local preview of the book (ignored by Git)
# is the most useful task for project maintainers:
task :default => :preview
## All Tasks
############
desc "Run all tasks"
task :all => [:clobber, :preview, :publish]
## Preview
##########
desc "Local book (default task)"
task :preview => BOOK_PREV
## Publish
##########
desc "Publish book (all editions)"
task :publish => %w[publish:www publish:html]
namespace "publish" do
desc "Publish book: online edition (chunked)"
task :www => CHUNKER_SRC
desc "Publish book: HTML edition (single file)"
task :html => BOOK_HTML
end
## Clean & Clobber
##################
require 'rake/clean'
CLOBBER.include('**/*.html').exclude('assets*/**/*.html')
# ==============================================================================
# -------------------------:{ F I L E T A S K S }:--------------------------
# ==============================================================================
file BOOK_PREV => BOOK_DEPS do |t|
src_file = BOOK_SRC.pathmap("%f")
out_file = File.expand_path(t.name)
adoc_opts = ADOC_OPTS_SHARED.chomp + " "
adoc_opts += <<~HEREDOC
-a toclevels=5 \
--out-file=#{out_file} \
#{src_file}
HEREDOC
AsciidoctorConvert(BOOK_SRC, t.name, adoc_opts)
end
file CHUNKER_SRC => BOOK_DEPS do |t|
# ------------------------------------
# Create Temporary Single HTML for WWW
# ------------------------------------
adoc_src = BOOK_SRC.pathmap("%f")
adoc_out = File.expand_path(t.name)
adoc_opts = ADOC_OPTS_SHARED.chomp + " "
adoc_opts += <<~HEREDOC
-a web-edition \
-a toclevels=2 \
-a data-uri \
--out-file=#{adoc_out} \
#{adoc_src}
HEREDOC
AsciidoctorConvert(BOOK_SRC, t.name, adoc_opts)
TaskHeader("Chunking Website Edition")
# ------------------------
# Delete Old Chunked Files
# ------------------------
puts "Chunker input file: #{CHUNKER_SRC}"
puts "Chunker out folder: #{CHUNKER_SRC}"
puts "\n1. Deleting old chunked files."
cd "#{$repo_root}/#{WWW_DIR}", verbose: false
FileUtils.rm Dir.glob('*.{html,css}'), force: true
# ---------------------------
# Create Chunked Book for WWW
# ---------------------------
puts "\n2. Invoking Asciidoctor-Chunker."
cd "#{$repo_root}/#{SRC_DIR}", verbose: false
begin
chunk_src = t.name.pathmap("%f")
stdout, stderr, status = Open3.capture3 <<~HEREDOC
node #{CHUNKER_JS} \
--outdir=#{$repo_root}/#{WWW_DIR} \
--titlePage="Title Page" \
#{chunk_src}
HEREDOC
puts stderr if status.success? # Even success is logged to STDERR!
raise unless status.success?
rescue
rake_msg = "Asciidoctor-Chunker failed: #{CHUNKER_SRC}"
our_msg = "Asciidoctor-Chunker reported some errors.\n" \
"The generated HTML files should not be considered safe to deploy."
PrintTaskFailureMessage(our_msg, stderr)
# Abort Rake execution with error description:
raise rake_msg
else
# ----------------------------
# Fix <title> of Chunked Files
# ----------------------------
puts "3. Invoking <title> tags fixer."
FixChuncksTitles("#{WWW_DIR}", "Git-Book_", "Git Book — ")
ensure
cd $repo_root, verbose: false
end
end
file BOOK_HTML => BOOK_DEPS do |t|
directory "#{PUB_DIR}"
src_file = BOOK_SRC.pathmap("%f")
out_file = File.expand_path(t.name)
adoc_opts = ADOC_OPTS_SHARED.chomp + " "
adoc_opts += <<~HEREDOC
-a toclevels=2 \
-a data-uri \
--out-file=#{out_file} \
#{src_file}
HEREDOC
AsciidoctorConvert(BOOK_SRC, t.name, adoc_opts)
end