Skip to content

Grid processing article #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions MathJax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
MathJax = {
loader: {
load: ['[tex]/boldsymbol']
},
tex: {
tags: "all",
inlineMath: [['$','$'], ['\\(','\\)']],
displayMath: [['$$','$$'], ['\\[','\\]']],
processEscapes: true,
processEnvironments: true,
packages: {
'[+]': ['boldsymbol']
}
}
};
</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
7 changes: 7 additions & 0 deletions _extensions/leovan/pseudocode/_extension.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
title: Pseudocode
author: 范叶亮 | Leo Van
version: 1.1.1
quarto-required: ">=1.4.0"
contributes:
filters:
- pseudocode.lua
289 changes: 289 additions & 0 deletions _extensions/leovan/pseudocode/pseudocode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
local function ensure_html_deps()
quarto.doc.add_html_dependency({
name = "pseudocode",
version = "2.4.1",
scripts = { "pseudocode.min.js" },
stylesheets = { "pseudocode.min.css" }
})
quarto.doc.include_text("after-body", [[
<script type="text/javascript">
(function(d) {
d.querySelectorAll(".pseudocode-container").forEach(function(el) {
let pseudocodeOptions = {
indentSize: el.dataset.indentSize || "1.2em",
commentDelimiter: el.dataset.commentDelimiter || "//",
lineNumber: el.dataset.lineNumber === "true" ? true : false,
lineNumberPunc: el.dataset.lineNumberPunc || ":",
noEnd: el.dataset.noEnd === "true" ? true : false,
titlePrefix: el.dataset.captionPrefix || "Algorithm"
};
pseudocode.renderElement(el.querySelector(".pseudocode"), pseudocodeOptions);
});
})(document);
(function(d) {
d.querySelectorAll(".pseudocode-container").forEach(function(el) {
let captionSpan = el.querySelector(".ps-root > .ps-algorithm > .ps-line > .ps-keyword")
if (captionSpan !== null) {
let captionPrefix = el.dataset.captionPrefix + " ";
let captionNumber = "";
if (el.dataset.pseudocodeNumber) {
captionNumber = el.dataset.pseudocodeNumber + " ";
if (el.dataset.chapterLevel) {
captionNumber = el.dataset.chapterLevel + "." + captionNumber;
}
}
captionSpan.innerHTML = captionPrefix + captionNumber;
}
});
})(document);
</script>
]])
end

local function ensure_latex_deps()
quarto.doc.use_latex_package("algorithm")
quarto.doc.use_latex_package("algpseudocode")
quarto.doc.use_latex_package("caption")
end

local function extract_source_code_options(source_code, render_type)
local options = {}
local source_codes = {}
local found_source_code = false

for str in string.gmatch(source_code, "([^\n]*)\n?") do
if (string.match(str, "^%s*#|.*") or string.gsub(str, "%s", "") == "") and not found_source_code then
if string.match(str, "^%s*#|%s+[" .. render_type .. "|label].*") then
str = string.gsub(str, "^%s*#|%s+", "")
local idx_start, idx_end = string.find(str, ":%s*")

if idx_start and idx_end and idx_end + 1 < #str then
k = string.sub(str, 1, idx_start - 1)
v = string.sub(str, idx_end + 1)
v = string.gsub(v, "^\"%s*", "")
v = string.gsub(v, "%s*\"$", "")

options[k] = v
else
quarto.log.warning("Invalid pseducode option: " .. str)
end
end
else
found_source_code = true
table.insert(source_codes, str)
end
end

return options, table.concat(source_codes, "\n")
end

local function render_pseudocode_block_html(global_options)
ensure_html_deps()

local filter = {
CodeBlock = function(el)
if not el.attr.classes:includes("pseudocode") then
return el
end

local options, source_code = extract_source_code_options(el.text, "html")

source_code = string.gsub(source_code, "%s*\\begin{algorithm}[^\n]+", "\\begin{algorithm}")
source_code = string.gsub(source_code, "%s*\\begin{algorithmic}[^\n]+", "\\begin{algorithmic}")

local alg_id = options["label"]
options["label"] = nil
options["html-caption-prefix"] = global_options.caption_prefix

if global_options.number_with_in_chapter and global_options.html_chapter_level then
options["html-chapter-level"] = global_options.html_chapter_level
end

if global_options.caption_number then
options["html-pseudocode-number"] = global_options.html_current_number
end

local data_options = {}
for k, v in pairs(options) do
if string.match(k, "^html-") then
data_k = string.gsub(k, "^html", "data")
data_options[data_k] = v
end
end

local inner_el = pandoc.Div(source_code)
inner_el.attr.classes = pandoc.List()
inner_el.attr.classes:insert("pseudocode")

local outer_el = pandoc.Div(inner_el)
outer_el.attr.classes = pandoc.List()
outer_el.attr.classes:insert("pseudocode-container")
outer_el.attr.classes:insert("quarto-float")
outer_el.attr.attributes = data_options

if alg_id then
outer_el.attr.identifier = alg_id
global_options.html_identifier_number_mapping[alg_id] = global_options.html_current_number
global_options.html_current_number = global_options.html_current_number + 1
end

return outer_el
end
}

return filter
end

local function render_pseudocode_block_latex(global_options)
ensure_latex_deps()

if global_options.caption_number then
quarto.doc.include_text("before-body", "\\floatname{algorithm}{" .. global_options.caption_prefix .. "}")
else
quarto.doc.include_text("in-header", "\\DeclareCaptionLabelFormat{algnonumber}{" .. global_options.caption_prefix .. "}")
quarto.doc.include_text("before-body", "\\captionsetup[algorithm]{labelformat=algnonumber}")
end

if global_options.number_with_in_chapter then
quarto.doc.include_text("before-body", "\\numberwithin{algorithm}{chapter}")
end

local filter = {
CodeBlock = function(el)
if not el.attr.classes:includes("pseudocode") then
return el
end

local options, source_code = extract_source_code_options(el.text, "pdf")

local pdf_placement = "H"
if options["pdf-placement"] then
pdf_placement = options["pdf-placement"]
end
source_code = string.gsub(source_code, "\\begin{algorithm}%s*\n", "\\begin{algorithm}[" .. pdf_placement .. "]\n")

if not options["pdf-line-number"] or options["pdf-line-number"] == "true" then
source_code = string.gsub(source_code, "\\begin{algorithmic}%s*\n", "\\begin{algorithmic}[1]\n")
end

if options["label"] then
source_code = string.gsub(source_code, "\\begin{algorithmic}", "\\label{" .. options["label"] .. "}\n\\begin{algorithmic}")
end

return pandoc.RawInline("latex", source_code)
end
}

return filter
end

local function render_pseudocode_block(global_options)
local filter = {
CodeBlock = function(el)
return el
end
}

if quarto.doc.is_format("html") then
filter = render_pseudocode_block_html(global_options)
elseif quarto.doc.is_format("latex") then
filter = render_pseudocode_block_latex(global_options)
end

return filter
end

local function render_pseudocode_ref_html(global_options)
local filter = {
Cite = function(el)
local cite_text = pandoc.utils.stringify(el.content)

for k, v in pairs(global_options.html_identifier_number_mapping) do
if cite_text == "@" .. k then
local link_src = "#" .. k
local alg_id = v

if global_options.html_chapter_level then
alg_id = global_options.html_chapter_level .. "." .. alg_id
end

local link_text = global_options.reference_prefix .. " " .. alg_id
local link = pandoc.Link(link_text, link_src)
link.attr.classes = pandoc.List()
link.attr.classes:insert("quarto-xref")

return link
end
end
end
}

return filter
end

local function render_pseudocode_ref_latex(global_options)
local filter = {
Cite = function(el)
local cite_text = pandoc.utils.stringify(el.content)

if string.match(cite_text, "^@alg-") then
return pandoc.RawInline("latex", global_options.reference_prefix .. "~\\ref{" .. string.gsub(cite_text, "^@", "") .. "}" )
end
end
}

return filter
end

local function render_pseudocode_ref(global_options)
local filter = {
Cite = function(el)
return el
end
}

if quarto.doc.is_format("html") then
filter = render_pseudocode_ref_html(global_options)
elseif quarto.doc.is_format("latex") then
filter = render_pseudocode_ref_latex(global_options)
end

return filter
end

function Pandoc(doc)
local global_options = {
caption_prefix = "Algorithm",
reference_prefix = "Algorithm",
caption_number = true,
number_with_in_chapter = false,
html_chapter_level = nil,
html_current_number = 1,
html_identifier_number_mapping = {}
}

if doc.meta["pseudocode"] then
global_options.caption_prefix = pandoc.utils.stringify(doc.meta["pseudocode"]["caption-prefix"]) or global_options.caption_prefix
global_options.reference_prefix = pandoc.utils.stringify(doc.meta["pseudocode"]["reference-prefix"]) or global_options.reference_prefix
global_options.caption_number = doc.meta["pseudocode"]["caption-number"] or global_options.caption_number
end

if doc.meta["book"] then
global_options.number_with_in_chapter = true

if quarto.doc.is_format("html") then
local _, input_qmd_filename = string.match(quarto.doc["input_file"], "^(.-)([^\\/]-%.([^\\/%.]-))$")
local renders = doc.meta["book"]["render"]

for _, render in pairs(renders) do
if render["file"] and render["number"] and pandoc.utils.stringify(render["file"]) == input_qmd_filename then
global_options.html_chapter_level = pandoc.utils.stringify(render["number"])
end
end
end
end

doc = doc:walk(render_pseudocode_block(global_options))

return doc:walk(render_pseudocode_ref(global_options))
end
1 change: 1 addition & 0 deletions _extensions/leovan/pseudocode/pseudocode.min.css

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

1 change: 1 addition & 0 deletions _extensions/leovan/pseudocode/pseudocode.min.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _freeze/site_libs/quarto-listing/list.min.js

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions _freeze/site_libs/quarto-listing/quarto-listing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const kProgressiveAttr = "data-src";
let categoriesLoaded = false;

window.quartoListingCategory = (category) => {
category = atob(category);
if (categoriesLoaded) {
activateCategory(category);
setCategoryHash(category);
Expand All @@ -15,7 +16,9 @@ window["quarto-listing-loaded"] = () => {
if (hash) {
// If there is a category, switch to that
if (hash.category) {
activateCategory(hash.category);
// category hash are URI encoded so we need to decode it before processing
// so that we can match it with the category element processed in JS
activateCategory(decodeURIComponent(hash.category));
}
// Paginate a specific listing
const listingIds = Object.keys(window["quarto-listings"]);
Expand Down Expand Up @@ -58,7 +61,10 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
);

for (const categoryEl of categoryEls) {
const category = categoryEl.getAttribute("data-category");
// category needs to support non ASCII characters
const category = decodeURIComponent(
atob(categoryEl.getAttribute("data-category"))
);
categoryEl.onclick = () => {
activateCategory(category);
setCategoryHash(category);
Expand Down Expand Up @@ -208,7 +214,9 @@ function activateCategory(category) {

// Activate this category
const categoryEl = window.document.querySelector(
`.quarto-listing-category .category[data-category='${category}'`
`.quarto-listing-category .category[data-category='${btoa(
encodeURIComponent(category)
)}']`
);
if (categoryEl) {
categoryEl.classList.add("active");
Expand All @@ -231,7 +239,9 @@ function filterListingCategory(category) {
list.filter(function (item) {
const itemValues = item.values();
if (itemValues.categories !== null) {
const categories = itemValues.categories.split(",");
const categories = decodeURIComponent(
atob(itemValues.categories)
).split(",");
return categories.includes(category);
} else {
return false;
Expand Down
Loading