Skip to content

Commit 43623f0

Browse files
committed
test
1 parent 5230f6d commit 43623f0

File tree

2 files changed

+112
-30
lines changed

2 files changed

+112
-30
lines changed

build_docs.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@ sub build_entries {
567567
dir => $build,
568568
raw_dir => $raw_build,
569569
temp_dir => $temp_dir,
570+
url_mappings => 'url_mappings.json',
570571
%$entry
571572
);
572573
$toc->add_entry( $book->build( $Opts->{rebuild}, $ConfPath ) );

lib/ES/Book.pm

Lines changed: 111 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,129 @@ use File::Copy::Recursive qw(fcopy rcopy);
1212
use ES::Toc();
1313
use utf8;
1414
use List::Util qw(first);
15+
use JSON::PP;
16+
17+
# Add new function to load URL mappings
18+
sub _load_url_mappings {
19+
my ($self, $json_file) = @_;
20+
return {} unless -f $json_file;
21+
22+
local $/;
23+
open(my $fh, '<', $json_file) or die "Cannot open $json_file: $!";
24+
my $json_text = <$fh>;
25+
close($fh);
26+
27+
return decode_json($json_text);
28+
}
29+
30+
# Add function to get redirect URL
31+
sub _get_redirect_url {
32+
my ($self, $current_url, $mappings) = @_;
33+
return $mappings->{$current_url} || '../current/index.html'; # Default fallback
34+
}
1535

1636
our %Page_Header = (
1737
en => {
18-
old => <<"HEADER",
38+
old => sub {
39+
my ($self, $mappings, $current_url) = @_;
40+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
41+
return <<"HEADER";
1942
A newer version is available. For the latest information, see the
20-
<a href="../current/index.html">current release documentation</a>.
43+
<a href="$redirect_url">current release documentation</a>.
2144
HEADER
22-
dead => <<"HEADER",
45+
},
46+
dead => sub {
47+
my ($self, $mappings, $current_url) = @_;
48+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
49+
return <<"HEADER";
2350
<strong>IMPORTANT</strong>: No additional bug fixes or documentation updates
2451
will be released for this version. For the latest information, see the
25-
<a href="../current/index.html">current release documentation</a>.
52+
<a href="$redirect_url">current release documentation</a>.
2653
HEADER
27-
new => <<"HEADER"
28-
This documentation contains work-in-progress information for future Elastic Stack and Cloud releases. Use the version selector to view supported release docs. It also contains some Elastic Cloud serverless information. Check out our <a href="https://www.elastic.co/docs/current/serverless">serverless docs</a> for more details.
54+
},
55+
new => sub {
56+
my ($self, $mappings, $current_url) = @_;
57+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
58+
return <<"HEADER"
59+
This documentation contains work-in-progress information for future Elastic Stack and Cloud releases. Use the version selector to view supported release docs. It also contains some Elastic Cloud serverless information. Check out our <a href="$redirect_url">serverless docs</a> for more details.
2960
HEADER
61+
}
3062
},
3163
zh_cn => {
32-
old => <<"HEADER",
33-
你当前正在查看的是旧版本的文档。如果不是你要找的,请点击查看 <a href="../current/index.html">当前发布版本的文档</a>。
64+
old => sub {
65+
my ($self, $mappings, $current_url) = @_;
66+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
67+
return <<"HEADER";
68+
你当前正在查看的是旧版本的文档。如果不是你要找的,请点击查看 <a href="$redirect_url">当前发布版本的文档</a>。
3469
HEADER
35-
dead => <<"HEADER",
36-
你当前正在查看的是旧版本的文档。如果不是你要找的,请点击查看 <a href="../current/index.html">当前发布版本的文档</a>。
70+
},
71+
dead => sub {
72+
my ($self, $mappings, $current_url) = @_;
73+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
74+
return <<"HEADER";
75+
你当前正在查看的是旧版本的文档。如果不是你要找的,请点击查看 <a href="$redirect_url">当前发布版本的文档</a>。
3776
HEADER
38-
new => <<"HEADER"
39-
你当前正在查看的是未发布版本的预览版文档。如果不是你要找的,请点击查看 <a href="../current/index.html">当前发布版本的文档</a>。
77+
},
78+
new => sub {
79+
my ($self, $mappings, $current_url) = @_;
80+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
81+
return <<"HEADER";
82+
你当前正在查看的是未发布版本的预览版文档。如果不是你要找的,请点击查看 <a href="$redirect_url">当前发布版本的文档</a>。
4083
HEADER
84+
}
4185
},
4286
ja => {
43-
old => <<"HEADER",
87+
old => sub {
88+
my ($self, $mappings, $current_url) = @_;
89+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
90+
return <<"HEADER";
4491
A newer version is available. For the latest information, see the
45-
<a href="../current/index.html">current release documentation</a>.
92+
<a href="$redirect_url">current release documentation</a>.
4693
HEADER
47-
dead => <<"HEADER",
94+
},
95+
dead => sub {
96+
my ($self, $mappings, $current_url) = @_;
97+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
98+
return <<"HEADER";
4899
<strong>IMPORTANT</strong>: No additional bug fixes or documentation updates
49100
will be released for this version. For the latest information, see the
50-
<a href="../current/index.html">current release documentation</a>.
101+
<a href="$redirect_url">current release documentation</a>.
51102
HEADER
52-
new => <<"HEADER"
53-
This documentation contains work-in-progress information for future Elastic Stack and Cloud releases. Use the version selector to view supported release docs. It also contains some Elastic Cloud serverless information. Check out our <a href="https://www.elastic.co/docs/current/serverless">serverless docs</a> for more details.
103+
},
104+
new => sub {
105+
my ($self, $mappings, $current_url) = @_;
106+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
107+
return <<"HEADER";
108+
This documentation contains work-in-progress information for future Elastic Stack and Cloud releases. Use the version selector to view supported release docs. It also contains some Elastic Cloud serverless information. Check out our <a href="$redirect_url">serverless docs</a> for more details.
54109
HEADER
110+
}
55111
},
56112
ko => {
57-
old => <<"HEADER",
113+
old => sub {
114+
my ($self, $mappings, $current_url) = @_;
115+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
116+
return <<"HEADER";
58117
A newer version is available. For the latest information, see the
59-
<a href="../current/index.html">current release documentation</a>.
118+
<a href="$redirect_url">current release documentation</a>.
60119
HEADER
61-
dead => <<"HEADER",
120+
},
121+
dead => sub {
122+
my ($self, $mappings, $current_url) = @_;
123+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
124+
return <<"HEADER";
62125
<strong>IMPORTANT</strong>: No additional bug fixes or documentation updates
63126
will be released for this version. For the latest information, see the
64-
<a href="../current/index.html">current release documentation</a>.
127+
<a href="$redirect_url">current release documentation</a>.
65128
HEADER
66-
new => <<"HEADER"
67-
This documentation contains work-in-progress information for future Elastic Stack and Cloud releases. Use the version selector to view supported release docs. It also contains some Elastic Cloud serverless information. Check out our <a href="https://www.elastic.co/docs/current/serverless">serverless docs</a> for more details.
129+
},
130+
new => sub {
131+
my ($self, $mappings, $current_url) = @_;
132+
my $redirect_url = $self->_get_redirect_url($current_url, $mappings);
133+
return <<"HEADER";
134+
This documentation contains work-in-progress information for future Elastic Stack and Cloud releases. Use the version selector to view supported release docs. It also contains some Elastic Cloud serverless information. Check out our <a href="$redirect_url">serverless docs</a> for more details.
68135
HEADER
69136
}
70-
137+
}
71138
);
72139

73140
#===================================
@@ -119,10 +186,6 @@ sub new {
119186
push @difference, $item unless grep { $item eq $_ } @branches;
120187
}
121188

122-
# print "Branches: ", join(", ", @branches), "\n";
123-
# print "Live: ", join(", ", @$live_branches), "\n";
124-
# print "Difference: ", join(", ", @difference), "\n";
125-
126189
my $missing = join ", ", @difference;
127190
die "Live branch(es) <$missing> not in <branches> in book <$title>"
128191
if $difference[0];
@@ -170,6 +233,7 @@ sub new {
170233
respect_edit_url_overrides => $respect_edit_url_overrides,
171234
suppress_migration_warnings => $args{suppress_migration_warnings} || 0,
172235
toc_extra => $args{toc_extra} || '',
236+
url_mappings_file => $args{url_mappings_file} || '',
173237
}, $class;
174238
}
175239

@@ -456,11 +520,28 @@ sub _page_header_text {
456520
#===================================
457521
my ( $self, $phrase ) = @_;
458522
$phrase ||= '';
459-
return $Page_Header{ $self->lang }{$phrase}
523+
524+
my $header_sub = $Page_Header{ $self->lang }{$phrase}
460525
|| die "No page header available for lang: "
461526
. $self->lang
462527
. " and phrase: $phrase";
528+
529+
# Load URL mappings from JSON file if it exists
530+
my $mappings = $self->_load_url_mappings($self->{url_mappings_file} || 'url_mappings.json');
531+
532+
# Get current URL from the context
533+
my $current_url = $self->_get_current_url();
534+
535+
# Call the header sub with mappings and current URL
536+
return $header_sub->($self, $mappings, $current_url);
537+
}
463538

539+
#===================================
540+
sub _get_current_url {
541+
#===================================
542+
my ($self) = @_;
543+
# Construct the current URL based on the book's context
544+
return sprintf("%s/%s/index.html", $self->prefix, $self->branch_title($self->current));
464545
}
465546

466547
#===================================

0 commit comments

Comments
 (0)