feat: Custom settings available for export

This commit is contained in:
Yaotian-Liu 2023-06-04 16:48:16 +08:00
parent 0e09e52568
commit 2aa4bae01b
No known key found for this signature in database
GPG key ID: 160A5464FF2FBA1F
2 changed files with 43 additions and 19 deletions

View file

@ -43,7 +43,7 @@ export default class PseudocodePlugin extends Plugin {
try { try {
pseudocode.renderElement(preEl, this.settings.jsSettings); pseudocode.renderElement(preEl, this.settings.jsSettings);
createExportButton(blockDiv, source); createExportButton(this, blockDiv, source);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
const errorSpan = blockDiv.createEl("span", { const errorSpan = blockDiv.createEl("span", {

View file

@ -2,7 +2,10 @@ const BUTTON_INFO = "Export to clipboard";
const BUTTON_EXPORTED = "Exported!"; const BUTTON_EXPORTED = "Exported!";
const BUTTON_FAILED = "Failed to export"; const BUTTON_FAILED = "Failed to export";
import PseudocodePlugin from "main";
export function createExportButton( export function createExportButton(
parentPlugin: PseudocodePlugin,
parentDiv: HTMLDivElement, parentDiv: HTMLDivElement,
blockContent: string blockContent: string
) { ) {
@ -14,10 +17,10 @@ export function createExportButton(
if (blockContent !== null) { if (blockContent !== null) {
const exportContent = const exportContent =
"\\documentclass{article}\n" + "\\documentclass{article}\n" +
MACROS + macros(parentPlugin) +
"\n" + "\n" +
"\\begin{document}\n" + "\\begin{document}\n" +
blockContent + processBlock(blockContent, parentPlugin) +
"\n\\end{document}"; "\n\\end{document}";
navigator.clipboard navigator.clipboard
@ -41,19 +44,40 @@ export function createExportButton(
}); });
} }
const MACROS = const macros = (parentPlugin: PseudocodePlugin): string => {
"\\usepackage{algorithm}\n" + const noEnd = parentPlugin.settings.jsSettings.noEnd;
"\\usepackage{algpseudocode}\n" + const scopeLines = parentPlugin.settings.jsSettings.scopeLines;
"\n" +
"\\newcommand{\\And}{\\textbf{and~}}\n" + return `
"\\newcommand{\\Or}{\\textbf{or~}}\n" + \\usepackage{algorithm}
"\\newcommand{\\Xor}{\\textbf{xor~}}\n" + \\usepackage[noEnd=${noEnd},indLines=${scopeLines}]{algpseudocodex}
"\\newcommand{\\Not}{\\textbf{not~}}\n" +
"\\newcommand{\\To}{\\textbf{to~}}\n" + \\newcommand{\\And}{\\textbf{and~}}
"\\newcommand{\\DownTo}{\\textbf{downto~}}\n" + \\newcommand{\\Or}{\\textbf{or~}}
"\\newcommand{\\True}{\\textbf{true~}}\n" + \\newcommand{\\Xor}{\\textbf{xor~}}
"\\newcommand{\\False}{\\textbf{false~}}\n" + \\newcommand{\\Not}{\\textbf{not~}}
"\\newcommand{\\Input}{\\item[\\textbf{Input:}]}\n" + \\newcommand{\\To}{\\textbf{to~}}
"\\newcommand{\\Output}{\\item[\\textbf{Output:}]}\n" + \\newcommand{\\DownTo}{\\textbf{downto~}}
"\\renewcommand{\\Return}{\\State \\textbf{return~}}\n" + \\newcommand{\\True}{\\textbf{true~}}
"\\newcommand{\\Print}{\\State \\textbf{print~}}\n"; \\newcommand{\\False}{\\textbf{false~}}
\\newcommand{\\Input}{\\item[\\textbf{Input:}]}
\\renewcommand{\\Output}{\\item[\\textbf{Output:}]}
\\newcommand{\\Print}{\\State \\textbf{print~}}
\\renewcommand{\\Return}{\\State \\textbf{return~}}
`;
};
const processBlock = (
block: string,
parentPlugin: PseudocodePlugin
): string => {
if (parentPlugin.settings.jsSettings.lineNumber)
// Replace "\begin{algorithmic}" with "\begin{algorithmic}[1]"
block = block.replace(
"\\begin{algorithmic}",
"\\begin{algorithmic}[1]"
);
else;
return block;
};