diff --git a/build_docs.pl b/build_docs.pl
index d32210bb81be0..30bae9f5c4658 100755
--- a/build_docs.pl
+++ b/build_docs.pl
@@ -567,6 +567,7 @@ sub build_entries {
dir => $build,
raw_dir => $raw_build,
temp_dir => $temp_dir,
+ url_mappings => 'url_mappings.json',
%$entry
);
$toc->add_entry( $book->build( $Opts->{rebuild}, $ConfPath ) );
diff --git a/lib/ES/Book.pm b/lib/ES/Book.pm
index 9ef7552154bff..c95ed5a7efcf4 100644
--- a/lib/ES/Book.pm
+++ b/lib/ES/Book.pm
@@ -12,62 +12,136 @@ use File::Copy::Recursive qw(fcopy rcopy);
use ES::Toc();
use utf8;
use List::Util qw(first);
+use JSON::PP;
+
+# Add new function to load URL mappings
+sub _load_url_mappings {
+ my ($self, $json_file) = @_;
+ return {} unless -f $json_file;
+
+ local $/;
+ open(my $fh, '<', $json_file) or die "Cannot open $json_file: $!";
+ my $json_text = <$fh>;
+ close($fh);
+
+ return decode_json($json_text);
+}
+
+# Add function to get redirect URL
+sub _get_redirect_url {
+ my ($self, $current_url, $mappings, $canonical_url) = @_;
+ # If we have a canonical URL, use that as the redirect URL
+ return $canonical_url if $canonical_url;
+ # Otherwise fall back to the mappings or default
+ return $mappings->{$current_url} || 'https://www.elastic.co/docs';
+}
our %Page_Header = (
en => {
- old => <<"HEADER",
+ old => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+
+ printf("current_url: %s\n", $current_url);
+ printf("redirect_url: %s\n", $redirect_url);
+
+ return <<"HEADER";
A newer version is available. For the latest information, see the
-current release documentation.
+current release documentation.
HEADER
- dead => <<"HEADER",
+ },
+ dead => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
IMPORTANT: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
-current release documentation.
+current release documentation.
HEADER
- new => <<"HEADER"
-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 serverless docs for more details.
+ },
+ new => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER"
+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 serverless docs for more details.
HEADER
+ }
},
zh_cn => {
- old => <<"HEADER",
-你当前正在查看的是旧版本的文档。如果不是你要找的,请点击查看 当前发布版本的文档。
+ old => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
+你当前正在查看的是旧版本的文档。如果不是你要找的,请点击查看 当前发布版本的文档。
HEADER
- dead => <<"HEADER",
-你当前正在查看的是旧版本的文档。如果不是你要找的,请点击查看 当前发布版本的文档。
+ },
+ dead => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
+你当前正在查看的是旧版本的文档。如果不是你要找的,请点击查看 当前发布版本的文档。
HEADER
- new => <<"HEADER"
-你当前正在查看的是未发布版本的预览版文档。如果不是你要找的,请点击查看 当前发布版本的文档。
+ },
+ new => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
+你当前正在查看的是未发布版本的预览版文档。如果不是你要找的,请点击查看 当前发布版本的文档。
HEADER
+ }
},
ja => {
- old => <<"HEADER",
+ old => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
A newer version is available. For the latest information, see the
-current release documentation.
+current release documentation.
HEADER
- dead => <<"HEADER",
+ },
+ dead => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
IMPORTANT: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
-current release documentation.
+current release documentation.
HEADER
- new => <<"HEADER"
-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 serverless docs for more details.
+ },
+ new => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
+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 serverless docs for more details.
HEADER
+ }
},
ko => {
- old => <<"HEADER",
+ old => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
A newer version is available. For the latest information, see the
-current release documentation.
+current release documentation.
HEADER
- dead => <<"HEADER",
+ },
+ dead => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
IMPORTANT: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
-current release documentation.
+current release documentation.
HEADER
- new => <<"HEADER"
-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 serverless docs for more details.
+ },
+ new => sub {
+ my ($self, $mappings, $current_url, $canonical_url) = @_;
+ my $redirect_url = $self->_get_redirect_url($current_url, $mappings, $canonical_url);
+ return <<"HEADER";
+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 serverless docs for more details.
HEADER
}
-
+ }
);
#===================================
@@ -119,10 +193,6 @@ sub new {
push @difference, $item unless grep { $item eq $_ } @branches;
}
- # print "Branches: ", join(", ", @branches), "\n";
- # print "Live: ", join(", ", @$live_branches), "\n";
- # print "Difference: ", join(", ", @difference), "\n";
-
my $missing = join ", ", @difference;
die "Live branch(es) <$missing> not in in book <$title>"
if $difference[0];
@@ -170,6 +240,7 @@ sub new {
respect_edit_url_overrides => $respect_edit_url_overrides,
suppress_migration_warnings => $args{suppress_migration_warnings} || 0,
toc_extra => $args{toc_extra} || '',
+ url_mappings_file => $args{url_mappings_file} || '',
}, $class;
}
@@ -299,7 +370,7 @@ sub _build_book {
private => $self->private( $branch ),
noindex => $self->noindex( $branch ),
multi => $self->is_multi_version,
- page_header => $self->_page_header($branch),
+ page_header => $self->_page_header($branch, $first_path-($index), $version),
section_title => $section_title,
subject => $subject,
toc => $self->toc,
@@ -324,7 +395,7 @@ sub _build_book {
noindex => $self->noindex( $branch ),
chunk => $self->chunk,
multi => $self->is_multi_version,
- page_header => $self->_page_header($branch),
+ page_header => $self->_page_header($branch, $first_path->file($index), $version),
section_title => $section_title,
subject => $subject,
resource => [$checkout],
@@ -432,7 +503,7 @@ sub _copy_branch_to_current {
#===================================
sub _page_header {
#===================================
- my ( $self, $branch ) = @_;
+ my ( $self, $branch, $index_path, $version ) = @_;
return '' unless $self->is_multi_version;
my $current = $self->current;
@@ -448,19 +519,72 @@ sub _page_header {
my $key = $branchidx > $currentidx ? 'old' : 'new';
$key = 'dead' if $key eq 'old' && !grep( /^$branch$/, @{ $self->{live_branches} } );
- return $self->_page_header_text( $key );
+ return $self->_page_header_text( $key, $index_path, $version, $branch );
}
#===================================
sub _page_header_text {
#===================================
- my ( $self, $phrase ) = @_;
+ my ( $self, $phrase, $index_path, $version, $branch ) = @_;
$phrase ||= '';
- return $Page_Header{ $self->lang }{$phrase}
+
+ my $header_sub = $Page_Header{ $self->lang }{$phrase}
|| die "No page header available for lang: "
. $self->lang
. " and phrase: $phrase";
+
+ # Load URL mappings from JSON file if it exists
+ my $mappings = $self->_load_url_mappings($self->{url_mappings_file} || 'url_mappings.json');
+
+ # Get current URL based on context info
+ my $current_url = $self->_get_current_url($index_path, $version, $branch);
+
+ # Get canonical URL from the document attributes
+ my $canonical_url = '';
+ if ($index_path && -e $index_path) {
+ my $content = $index_path->slurp(iomode => '<:encoding(UTF-8)');
+ if ($content =~ /:page-canonical-url:\s*(.+)$/m) {
+ $canonical_url = $1;
+ }
+ }
+
+ # Call the header sub with mappings, current URL, and canonical URL
+ return $header_sub->($self, $mappings, $current_url, $canonical_url);
+}
+#===================================
+sub _get_current_url {
+#===================================
+ my ($self, $index_path, $version, $branch) = @_;
+
+ # Extract book path component
+ my $book_path = $self->prefix;
+
+ # Get relative path from index.asciidoc to the current file being processed
+ my $relative_path = '';
+ if ($index_path) {
+ # If we're processing index.asciidoc, use it directly
+ if ($index_path->basename eq 'index.asciidoc') {
+ $relative_path = 'index.html';
+ } else {
+ # Otherwise get the path relative to the book root
+ my $file_path = $index_path->stringify;
+ if ($file_path =~ m|/([^/]+?)\.asciidoc$|) {
+ $relative_path = "$1.html";
+ } else {
+ $relative_path = 'index.html'; # Default fallback
+ }
+ }
+ } else {
+ $relative_path = 'index.html'; # Default fallback
+ }
+
+ # Construct the URL using the domain, book path, version and file
+ return sprintf("https://www.elastic.co/guide/%s/%s/%s",
+ $book_path,
+ $version,
+ $relative_path
+ );
}
#===================================
diff --git a/lib/ES/Source.pm b/lib/ES/Source.pm
index 710664166c0c0..9cf9849a20ea9 100644
--- a/lib/ES/Source.pm
+++ b/lib/ES/Source.pm
@@ -147,6 +147,7 @@ sub _sources_for_branch {
#===================================
sub sources { shift->{sources} }
sub temp_dir { shift->{temp_dir} }
+sub path { shift->{path} }
#===================================
1;
diff --git a/url_mappings.json b/url_mappings.json
new file mode 100644
index 0000000000000..bda3ba112a2ff
--- /dev/null
+++ b/url_mappings.json
@@ -0,0 +1,3 @@
+{
+ "https://www.elastic.co/guide/en/elasticsearch/reference/7.17/runtime.html": "https://www.elastic.co/docs"
+}