Skip to content

Commit

Permalink
Redirect to translation index page in serve command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruin0x11 committed Sep 15, 2021
1 parent 8869c2c commit 85ab4d3
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 71 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ chrono = "0.4"
clap = "2.24"
env_logger = "0.7.1"
handlebars = "4.0"
http = "0.2.4"
lazy_static = "1.0"
log = "0.4"
memchr = "2.0"
Expand Down
32 changes: 28 additions & 4 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{get_book_dir, get_build_opts, open};
use clap::{App, Arg, ArgMatches, SubCommand};
use futures_util::sink::SinkExt;
use futures_util::StreamExt;
use http::Uri;
use mdbook::errors::*;
use mdbook::utils;
use mdbook::utils::fs::get_404_output_file;
Expand Down Expand Up @@ -60,7 +61,7 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
pub fn execute(args: &ArgMatches) -> Result<()> {
let book_dir = get_book_dir(args);
let build_opts = get_build_opts(args);
let mut book = MDBook::load_with_build_opts(&book_dir, build_opts)?;
let mut book = MDBook::load_with_build_opts(&book_dir, build_opts.clone())?;

let port = args.value_of("port").unwrap();
let hostname = args.value_of("hostname").unwrap();
Expand All @@ -82,6 +83,18 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
update_config(&mut book);
book.build()?;

let language: Option<String> = match build_opts.language_ident {
// index.html will be at the root directory.
Some(_) => None,
None => match book.config.language.default_language() {
// If book has translations, index.html will be under src/en/ or
// similar.
Some(lang_ident) => Some(lang_ident.clone()),
// If not, it will be at the root.
None => None,
}
};

let sockaddr: SocketAddr = address
.to_socket_addrs()?
.next()
Expand All @@ -100,7 +113,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> {

let reload_tx = tx.clone();
let thread_handle = std::thread::spawn(move || {
serve(build_dir, sockaddr, reload_tx, &file_404);
serve(build_dir, sockaddr, reload_tx, &file_404, language);
});

let serving_url = format!("http://{}", address);
Expand Down Expand Up @@ -140,6 +153,7 @@ async fn serve(
address: SocketAddr,
reload_tx: broadcast::Sender<Message>,
file_404: &str,
language: Option<String>,
) {
// A warp Filter which captures `reload_tx` and provides an `rx` copy to
// receive reload messages.
Expand All @@ -166,13 +180,23 @@ async fn serve(
// The fallback route for 404 errors
let fallback_route = warp::fs::file(build_dir.join(file_404))
.map(|reply| warp::reply::with_status(reply, warp::http::StatusCode::NOT_FOUND));
let routes = livereload.or(book_route).or(fallback_route);

std::panic::set_hook(Box::new(move |panic_info| {
// exit if serve panics
error!("Unable to serve: {}", panic_info);
std::process::exit(1);
}));

warp::serve(routes).run(address).await;
if let Some(lang_ident) = language {
// Redirect root to the default translation directory, if serving a localized book.
// BUG: This can't be `/{lang_ident}`, or the static assets won't get loaded.
let index_for_language = format!("/{}/index.html", lang_ident).parse::<Uri>().unwrap();
let redirect_to_index = warp::path::end().map(move || warp::redirect(index_for_language.clone()));
let routes = livereload.or(redirect_to_index).or(book_route).or(fallback_route);
warp::serve(routes).run(address).await;
}
else {
let routes = livereload.or(book_route).or(fallback_route);
warp::serve(routes).run(address).await;
};
}
136 changes: 69 additions & 67 deletions src/theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,81 +428,83 @@ function playground_text(playground) {
var languageToggleButton = document.getElementById('language-toggle');
var languagePopup = document.getElementById('language-list');

function showLanguages() {
languagePopup.style.display = 'block';
languageToggleButton.setAttribute('aria-expanded', true);
}

function hideLanguages() {
languagePopup.style.display = 'none';
languageToggleButton.setAttribute('aria-expanded', false);
languageToggleButton.focus();
}

function set_language(language) {
console.log("Set language " + language)
}

languageToggleButton.addEventListener('click', function () {
if (languagePopup.style.display === 'block') {
hideLanguages();
} else {
showLanguages();
if (languageToggleButton !== null) {
function showLanguages() {
languagePopup.style.display = 'block';
languageToggleButton.setAttribute('aria-expanded', true);
}
});

languagePopup.addEventListener('click', function (e) {
var language = e.target.id || e.target.parentElement.id;
set_language(language);
});

languagePopup.addEventListener('focusout', function(e) {
// e.relatedTarget is null in Safari and Firefox on macOS (see workaround below)
if (!!e.relatedTarget && !languageToggleButton.contains(e.relatedTarget) && !languagePopup.contains(e.relatedTarget)) {
hideLanguages();
function hideLanguages() {
languagePopup.style.display = 'none';
languageToggleButton.setAttribute('aria-expanded', false);
languageToggleButton.focus();
}
});

// Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang/mdBook/issues/628
document.addEventListener('click', function(e) {
if (languagePopup.style.display === 'block' && !languageToggleButton.contains(e.target) && !languagePopup.contains(e.target)) {
hideLanguages();
function set_language(language) {
console.log("Set language " + language)
}
});

document.addEventListener('keydown', function (e) {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
if (!languagePopup.contains(e.target)) { return; }
languageToggleButton.addEventListener('click', function () {
if (languagePopup.style.display === 'block') {
hideLanguages();
} else {
showLanguages();
}
});

switch (e.key) {
case 'Escape':
e.preventDefault();
languagePopup.addEventListener('click', function (e) {
var language = e.target.id || e.target.parentElement.id;
set_language(language);
});

languagePopup.addEventListener('focusout', function(e) {
// e.relatedTarget is null in Safari and Firefox on macOS (see workaround below)
if (!!e.relatedTarget && !languageToggleButton.contains(e.relatedTarget) && !languagePopup.contains(e.relatedTarget)) {
hideLanguages();
break;
case 'ArrowUp':
e.preventDefault();
var li = document.activeElement.parentElement;
if (li && li.previousElementSibling) {
li.previousElementSibling.querySelector('button').focus();
}
break;
case 'ArrowDown':
e.preventDefault();
var li = document.activeElement.parentElement;
if (li && li.nextElementSibling) {
li.nextElementSibling.querySelector('button').focus();
}
break;
case 'Home':
e.preventDefault();
languagePopup.querySelector('li:first-child button').focus();
break;
case 'End':
e.preventDefault();
languagePopup.querySelector('li:last-child button').focus();
break;
}
});
}
});

// Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang/mdBook/issues/628
document.addEventListener('click', function(e) {
if (languagePopup.style.display === 'block' && !languageToggleButton.contains(e.target) && !languagePopup.contains(e.target)) {
hideLanguages();
}
});

document.addEventListener('keydown', function (e) {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
if (!languagePopup.contains(e.target)) { return; }

switch (e.key) {
case 'Escape':
e.preventDefault();
hideLanguages();
break;
case 'ArrowUp':
e.preventDefault();
var li = document.activeElement.parentElement;
if (li && li.previousElementSibling) {
li.previousElementSibling.querySelector('button').focus();
}
break;
case 'ArrowDown':
e.preventDefault();
var li = document.activeElement.parentElement;
if (li && li.nextElementSibling) {
li.nextElementSibling.querySelector('button').focus();
}
break;
case 'Home':
e.preventDefault();
languagePopup.querySelector('li:first-child button').focus();
break;
case 'End':
e.preventDefault();
languagePopup.querySelector('li:last-child button').focus();
break;
}
});
}
})();

(function sidebar() {
Expand Down

0 comments on commit 85ab4d3

Please sign in to comment.