-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository
/
Copy pathgithub-wiki-to-html.rb
More file actions
More file actions
142 lines (110 loc) · 3.58 KB
/
github-wiki-to-html.rb
File metadata and controls
142 lines (110 loc) · 3.58 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
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
##
# Main script, run by `github-wiki-to-html.sh`.
#
# frozen_string_literal: true
require 'cgi'
require 'pathname'
require 'uri'
require 'commonmarker'
require 'gollum-lib'
require 'liquid'
require 'nokogiri'
# Load methods.
require_relative 'methods'
# Load constants.
require_relative 'constants'
# Load Gollum configuration.
require_relative 'gollum-config'
# Load the HTML template.
html_template = Liquid::Template.parse(HTML_TEMPLATE_FILE.read, error_mode: :strict2)
# Load the wiki.
wiki = Gollum::Wiki.new(WIKI_REPO.to_s, GOLLUM_OPTIONS)
home_page = wiki.page('Home')
page_footer_html = home_page.footer.formatted_data
# Pages to list on the home page and sitemap file.
all_pages = []
# Generate individual article pages and add them to the list.
wiki.pages.each do |page|
# Skip non-Markdown pages.
next unless page.format == :markdown
slug = page.filename_stripped
# Skip Home and special pages.
next if slug =~ /^(?:Home|LICENSE|README)$/
# Article title.
main_heading = slug.tr('-', ' ')
encoded_slug = URI.encode_uri_component(slug)
# URL of the page on the converted site.
canonical_url = SITE_URL_SLASH.merge(encoded_slug)
# URL of the page on the wiki.
wiki_page_url = WIKI_URL_SLASH.merge(encoded_slug)
# Get the oldest commit of the page (following renames).
oldest_commit = page.versions({
follow: true,
per_page: 100_000,
}).last
# Published date in UTC.
published_date = oldest_commit.authored_date.getutc
published_date_iso = published_date.iso8601
published_date_display = published_date.strftime(DISPLAY_DATE_FORMAT)
# Get the latest commit of the page.
latest_commit = page.last_version
is_modified = latest_commit.id != oldest_commit.id
# Last modified date in UTC.
if is_modified
modified_date = latest_commit.authored_date.getutc
modified_date_iso = modified_date.iso8601
modified_date_display = modified_date.strftime(DISPLAY_DATE_FORMAT)
else
modified_date = published_date
modified_date_iso = published_date_iso
modified_date_display = published_date_display
end
# Name of the original author.
author_name = oldest_commit.author.name
# URL of the oldest commit in the mirror repo.
author_url = MIRROR_REPO_URL_SLASH.merge("commit/#{oldest_commit.id}")
# Generate the HTML file for the article.
generate_html_file("#{slug}.html", page, html_template, {
is_home: false,
main_heading:,
canonical_url: canonical_url.to_s,
wiki_page_url: wiki_page_url.to_s,
page_footer_html:,
published_date_iso:,
published_date_display:,
is_modified:,
modified_date_iso:,
modified_date_display:,
author_name:,
author_url: author_url.to_s,
})
# Add the page to the list.
all_pages << {
title: main_heading,
url: Pathname(HOME_URL_SLASH).join(encoded_slug).to_s,
canonical_url:,
published_date:,
modified_date:,
modified_date_iso:,
}
end
# Pages sorted by published date (newest first).
pages_by_published_date = all_pages
.reject { |page| page[:title].start_with?(SITE_NAME) } # Exclude About pages.
.sort_by { |page| page[:published_date] }
.reverse
# Generate the HTML file for the home page.
generate_html_file('index.html', home_page, html_template, {
is_home: true,
main_heading: HOME_HEADING,
canonical_url: SITE_URL.to_s,
wiki_page_url: WIKI_URL.to_s,
page_footer_html:,
all_pages: pages_by_published_date,
})
# Pages sorted by modified date (newest first).
pages_by_modified_date = all_pages
.sort_by { |page| page[:modified_date] }
.reverse
# Generate the sitemap file.
generate_sitemap_file(pages_by_modified_date)