Skip to content

Commit c720829

Browse files
committed
Retour à JS pour le bouton
1 parent daa6c4c commit c720829

File tree

1 file changed

+33
-48
lines changed

1 file changed

+33
-48
lines changed
Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,43 @@
11
function RawBlock(el)
22
if el.format ~= "html" then return end
33

4-
local input_file = quarto.doc.input_file
5-
local project_dir = quarto.project.directory
6-
7-
-- Échapper les caractères spéciaux pour le pattern Lua
8-
local function escape_pattern(str)
9-
return str:gsub("([^%w])", "%%%1")
10-
end
11-
12-
local escaped_project_dir = escape_pattern(project_dir)
13-
local relative_path = input_file:gsub("^" .. escaped_project_dir, "")
14-
relative_path = relative_path:gsub("\\", "/") -- Pour Windows
15-
relative_path = relative_path:gsub("%.qmd$", ".html")
16-
17-
-- Détection de langue
18-
local is_english = relative_path:match("^/en/")
19-
20-
-- Nettoyage du path
21-
local final_relative_path
22-
if is_english then
23-
final_relative_path = relative_path:gsub("^/en/", "/")
24-
else
25-
final_relative_path = "/en" .. relative_path
26-
end
27-
28-
local label, flag_svg
29-
if is_english then
30-
label = "Passer à la version française"
31-
flag_svg = [[
32-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="20" height="20" style="vertical-align: middle; margin-left: 0.5em; display: inline-block;"><mask id="circleFlagsLangFr"><circle cx="256" cy="256" r="256" fill="#fff"/></mask><g mask="url(#circleFlagsLangFr)"><path fill="#0055A4" d="M0 0h512v512H0z"/><path fill="#fff" d="M170.7 0h170.6v512H170.7z"/><path fill="#EF4135" d="M341.3 0H512v512H341.3z"/></g></svg>
33-
]]
34-
else
35-
label = "Switch to English version"
36-
flag_svg = [[
37-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="20" height="20" style="vertical-align: middle; margin-left: 0.5em; display: inline-block;"><mask id="circleFlagsLangEn"><circle cx="256" cy="256" r="256" fill="#fff"/></mask><g mask="url(#circleFlagsLangEn)"><path fill="#eee" d="M256 0L0 256v64l32 32l-32 32v128l22-8l23 8h23l54-32l54 32h32l48-32l48 32h32l54-32l54 32h68l-8-22l8-23v-23l-32-54l32-54v-32l-32-48l32-48v-32l-32-54l32-54V0z"/><path fill="#d80027" d="M224 64v64h160l64-64zm0 128l32 64l-48 48v208h96V304h208v-96H304l16-16zM0 320v64h128l-64 64H0v64h45l131-131v-45l16-16zm336 16l176 176v-45L381 336Z"/><path fill="#0052b4" d="M0 0v256h256V0zm512 68L404 176h108zM404 336l108 108V336zm-228 68L68 512h108zm160 0v108h108z"/><path fill="#eee" d="m187 243l57-41h-70l57 41l-22-67zm-81 0l57-41H93l57 41l-22-67zm-81 0l57-41H12l57 41l-22-67zm162-81l57-41h-70l57 41l-22-67zm-81 0l57-41H93l57 41l-22-67zm-81 0l57-41H12l57 41l-22-67Zm162-82l57-41h-70l57 41l-22-67zm-81 0l57-41H93l57 41l-22-67Zm-81 0l57-41H12l57 41l-22-67Z"/></g></svg>
4+
if el.text:match('<main.-id="quarto%-document%-content".->') then
5+
local js_script = [[
6+
<script>
7+
const langButton = document.createElement("div");
8+
langButton.className = "lang-switch-button";
9+
langButton.style.textAlign = "right";
10+
langButton.style.marginTop = "1rem";
11+
langButton.style.marginBottom = "1rem";
12+
13+
const currentPath = window.location.pathname;
14+
const isEnglish = currentPath.startsWith("/en/");
15+
let targetHref, label, svg;
16+
17+
if (isEnglish) {
18+
targetHref = currentPath.replace(/^\/en\//, "/");
19+
label = "Passer à la version française";
20+
svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="20" height="20" style="vertical-align: middle; margin-left: 0.5em; display: inline-block;"><mask id="circleFlagsLangFr"><circle cx="256" cy="256" r="256" fill="#fff"/></mask><g mask="url(#circleFlagsLangFr)"><path fill="#0055A4" d="M0 0h512v512H0z"/><path fill="#fff" d="M170.7 0h170.6v512H170.7z"/><path fill="#EF4135" d="M341.3 0H512v512H341.3z"/></g></svg>`;
21+
} else {
22+
targetHref = "/en" + currentPath;
23+
label = "Switch to English version";
24+
svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="20" height="20" style="vertical-align: middle; margin-left: 0.5em; display: inline-block;"><mask id="circleFlagsLangEn"><circle cx="256" cy="256" r="256" fill="#fff"/></mask><g mask="url(#circleFlagsLangEn)"><path fill="#eee" d="M256 0L0 256v64l32 32l-32 32v128l22-8l23 8h23l54-32l54 32h32l48-32l48 32h32l54-32l54 32h68l-8-22l8-23v-23l-32-54l32-54v-32l-32-48l32-48v-32l-32-54l32-54V0z"/><path fill="#d80027" d="M224 64v64h160l64-64zm0 128l32 64l-48 48v208h96V304h208v-96H304l16-16zM0 320v64h128l-64 64H0v64h45l131-131v-45l16-16zm336 16l176 176v-45L381 336Z"/><path fill="#0052b4" d="M0 0v256h256V0zm512 68L404 176h108zM404 336l108 108V336zm-228 68L68 512h108zm160 0v108h108z"/><path fill="#eee" d="m187 243l57-41h-70l57 41l-22-67zm-81 0l57-41H93l57 41l-22-67zm-81 0l57-41H12l57 41l-22-67zm162-81l57-41h-70l57 41l-22-67zm-81 0l57-41H93l57 41l-22-67zm-81 0l57-41H12l57 41l-22-67Zm162-82l57-41h-70l57 41l-22-67zm-81 0l57-41H93l57 41l-22-67Zm-81 0l57-41H12l57 41l-22-67Z"/></g></svg>`;
25+
}
26+
27+
langButton.innerHTML = `
28+
<a href="${targetHref}" class="button-cta">
29+
<button class="btn"><i class="fa fa-language"></i> ${label} ${svg}</button>
30+
</a>
31+
`;
32+
33+
const main = document.querySelector("main#quarto-document-content");
34+
if (main) main.insertAdjacentElement("afterbegin", langButton);
35+
</script>
3836
]]
39-
end
4037

41-
-- URL finale (absolue)
42-
local final_url = "https://pythonds.linogaliana.fr" .. final_relative_path
43-
44-
local button_html = string.format([[
45-
<div class="lang-switch-button" style="text-align:right; margin-top:1rem; margin-bottom:1rem;">
46-
<a href="%s" class="button-cta">
47-
<button class="btn"><i class="fa fa-language"></i> %s %s</button>
48-
</a>
49-
</div>
50-
]], final_url, label, flag_svg)
51-
52-
if el.text:match('<main.-id="quarto%-document%-content".->') then
5338
return {
5439
el,
55-
pandoc.RawBlock("html", button_html)
40+
pandoc.RawBlock("html", js_script)
5641
}
5742
end
5843
end

0 commit comments

Comments
 (0)