-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository
/
Copy pathmethods.rb
More file actions
More file actions
88 lines (70 loc) · 2.06 KB
/
methods.rb
File metadata and controls
88 lines (70 loc) · 2.06 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
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
##
# Methods for use in the main script.
#
# frozen_string_literal: true
#
# Tweak HTML converted from Markdown.
def postprocess_html(html)
dom = Nokogiri::HTML5.fragment(html)
# Handle links converted from internal link syntax.
dom.css('a.internal').each do |a|
uri = URI(a['href'])
# Strip the file extension.
uri.path = Pathname(uri.path).sub_ext('').to_s
a['href'] = uri.to_s
end
dom.to_html
end
# Generate the HTML file.
def generate_html_file(filename, article_body_html, html_template, options)
# Tweak the HTML.
article_body_html = postprocess_html(article_body_html)
# Escape user-provided strings.
[:article_title, :author_name].each do |key|
options[key] = CGI.escape_html(options[key]) if options[key]
end
options[:all_pages]&.map! do |page|
page.merge(title: CGI.escape_html(page[:title]))
.transform_keys(&:to_s) # Stringify symbol keys for Liquid.
end
# Render the full HTML.
full_html = html_template.render!({
site_name: CGI.escape_html(SITE_NAME),
site_url: SITE_URL.to_s,
publisher_name: CGI.escape_html(PUBLISHER_NAME),
publisher_url: PUBLISHER_URL.to_s,
publisher_logo_url: PUBLISHER_LOGO_URL.to_s,
home_url: HOME_URL.to_s,
stylesheet_url: STYLESHEET_URL.to_s,
mathjax_config_script_url: MATHJAX_CONFIG_SCRIPT_URL.to_s,
article_body_html:,
**options
}.transform_keys(&:to_s), {
strict_variables: true,
strict_filters: true,
})
# Write the HTML file.
output_file = OUTPUT_DIRECTORY.join(filename)
output_file.write(full_html)
end
# Generate a sitemap file.
def generate_sitemap_file(pages)
xml = <<~XML
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>#{SITE_URL}</loc>
</url>
XML
pages.each do |page|
xml << <<~XML % page
<url>
<loc>%{canonical_url}</loc>
<lastmod>%{modified_date_iso}</lastmod>
</url>
XML
end
xml << '</urlset>'
sitemap_file = OUTPUT_DIRECTORY.join('sitemap.xml')
sitemap_file.write(xml)
end