Skip to content

Commit c8657dc

Browse files
committed
docs(translated): add redirects for missing files
Obviously, we cannot add redirects for _all_ the untranslated docs. But at least we can do that for the not-yet-translated pages that are referenced from existing translated pages. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent fad0cee commit c8657dc

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

script/update-docs.rb

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ def index_l10n_doc(filter_tags, doc_list, get_content)
105105
[new_content, name]
106106
end
107107

108+
check_paths = Set.new([])
109+
108110
doc_files.each do |entry|
109111
full_path, sha = entry
110112
ids = Set.new([])
@@ -125,7 +127,10 @@ def index_l10n_doc(filter_tags, doc_list, get_content)
125127
content = get_content.call sha
126128
categories = {}
127129
expand_l10n(full_path, content, get_content_f, categories)
128-
content.gsub!(/link:(?:technical\/)?(\S*?)\.html(\#\S*?)?\[(.*?)\]/m, "link:/docs/\\1/#{lang}\\2[\\3]")
130+
content.gsub!(/link:(?:technical\/)?(\S*?)\.html(\#\S*?)?\[(.*?)\]/m) do |match|
131+
check_paths.add("docs/#{$1}/#{lang}")
132+
"link:/docs/#{$1}/#{lang}#{$2}[#{$3}]"
133+
end
129134
asciidoc = make_asciidoc(content)
130135
asciidoc_sha = Digest::SHA1.hexdigest(asciidoc.source)
131136
if !File.exists?("#{SITE_ROOT}external/docs/asciidoc/#{asciidoc_sha}")
@@ -141,6 +146,8 @@ def index_l10n_doc(filter_tags, doc_list, get_content)
141146
html.gsub!(/linkgit:(\S+?)\[(\d+)\]/) do |line|
142147
x = /^linkgit:(\S+?)\[(\d+)\]/.match(line)
143148
relurl = "docs/#{x[1].gsub(/&#x2d;/, '-')}/#{lang}"
149+
# record path to check for broken links afterwards
150+
check_paths.add(relurl)
144151
"<a href='{{< relurl \"#{relurl}\" >}}'>#{x[1]}[#{x[2]}]</a>"
145152
end
146153
# Handle Chinese "full stop" character
@@ -158,7 +165,19 @@ def index_l10n_doc(filter_tags, doc_list, get_content)
158165
"<dt class=\"hdlist1\" id=\"#{anchor}\"> <a class=\"anchor\" href=\"##{anchor}\"></a>#{$1} </dt>"
159166
end
160167
# Make links relative
161-
html.gsub!(/(<a href=['"])\/([^'"]*)/, '\1{{< relurl "\2" >}}')
168+
html.gsub!(/(<a href=['"])\/([^'"]*)/) do |match|
169+
before = $1
170+
after = $2
171+
# record path to check for broken links afterwards
172+
path2 = after.sub(/#.*/, '') # rtrim `#<anchor>`
173+
if path2.end_with?(lang)
174+
check_paths.add(path2)
175+
else
176+
puts "warning: will not check path #{path2} because it does not end in /#{lang}"
177+
end
178+
179+
"#{before}{{< relurl \"#{after}\" >}}"
180+
end
162181

163182
# Write <docname>/<lang>.html
164183
front_matter = {
@@ -179,7 +198,22 @@ def index_l10n_doc(filter_tags, doc_list, get_content)
179198

180199
lang_data[lang] = asciidoc_sha
181200
end
201+
202+
# In some cases, translations are not complete. As a consequence, some
203+
# translated manual pages may point to other translated manual pages that do
204+
# not exist. In these cases, redirect to the English version.
205+
check_paths.each do |path|
206+
doc_path = "#{SITE_ROOT}external/docs/content/#{path}.html"
207+
if !File.exists?(doc_path)
208+
front_matter = { "redirect_to" => "#{path.sub(/\/[^\/]*$/, '')}" } # rtrim `/<lang>`
209+
FileUtils.mkdir_p(File.dirname(doc_path))
210+
File.open(doc_path, "w") do |out|
211+
out.write(wrap_front_matter(front_matter))
212+
end
213+
end
214+
end
182215
end
216+
183217
File.open(DATA_FILE, "w") do |out|
184218
YAML.dump(data, out)
185219
end

0 commit comments

Comments
 (0)