Skip to content

Commit 4853ec5

Browse files
committed
update-book: refactor the code to look for downloads
Some books have downloadable PDF/ebook versions. In the `remote_genbook2` function, this information is discovered dynamically by looking at the corresponding repository's latest release. This functionality is useful even in the GitHub workflow that keeps https://git-scm.com/book/ up to date, so let's refactor it into a reusable function. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 538ea5e commit 4853ec5

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

script/update-book2.rb

+19-14
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,24 @@ def genbook(language_code, &get_content)
239239
book
240240
end
241241

242+
# Update just the download data, based on the latest tag in the repository
243+
def update_downloads(book, repo, octokit)
244+
begin
245+
rel = octokit.latest_release(repo)
246+
get_url = lambda do |name_re|
247+
asset = rel.assets.find { |asset| name_re.match(asset.name) }
248+
asset&.browser_download_url
249+
end
250+
book.ebook_pdf = get_url.call(/\.pdf$/)
251+
book.ebook_epub = get_url.call(/\.epub$/)
252+
book.ebook_mobi = get_url.call(/\.mobi$/)
253+
rescue Octokit::NotFound
254+
book.ebook_pdf = nil
255+
book.ebook_epub = nil
256+
book.ebook_mobi = nil
257+
end
258+
end
259+
242260
# Generate book html directly from remote git repo
243261
def remote_genbook2(language_code)
244262
@octokit = Octokit::Client.new(access_token: ENV.fetch("GITHUB_API_TOKEN", nil))
@@ -269,20 +287,7 @@ def remote_genbook2(language_code)
269287

270288
book.sha = repo_head.sha
271289

272-
begin
273-
rel = @octokit.latest_release(repo)
274-
get_url = lambda do |name_re|
275-
asset = rel.assets.find { |asset| name_re.match(asset.name) }
276-
asset&.browser_download_url
277-
end
278-
book.ebook_pdf = get_url.call(/\.pdf$/)
279-
book.ebook_epub = get_url.call(/\.epub$/)
280-
book.ebook_mobi = get_url.call(/\.mobi$/)
281-
rescue Octokit::NotFound
282-
book.ebook_pdf = nil
283-
book.ebook_epub = nil
284-
book.ebook_mobi = nil
285-
end
290+
update_downloads(book, repo, @octokit)
286291

287292
book.save
288293
rescue StandardError => e

0 commit comments

Comments
 (0)