[add] output:tex

This commit is contained in:
fenris 2024-05-03 11:00:12 +02:00
parent 2d19dd3d8a
commit 54cf6f1eef
2 changed files with 268 additions and 0 deletions

View file

@ -0,0 +1,267 @@
/**
*/
class type_output_tex implements type_output<string>
{
public readonly kind : string = "tex";
public readonly data : {};
public constructor(data : {}) {this.data = data;}
/**
*/
private static coin(
template : string,
arguments_ : Record<string, any>
) : string
{
return lib_plankton.string.coin(
template,
arguments_,
{
"open": "<<",
"close": ">>",
}
);
}
/**
*/
private render_object_internal(
object : type_object,
options : {
level ?: int;
depth ?: int;
} = {}
) : string
{
options = Object.assign(
{
"level": 0,
"depth": 0,
},
options
);
return lib_plankton.call.distinguish<string>(
object,
{
"text": ({"content": content}) => content,
"code": ({"content": content}) => type_output_tex.coin(
"\\( <<content>> \\)",
{
"content": content,
}
),
"link": ({"target": target, "label": label}) => type_output_tex.coin(
"\\href{<<target>>}{<<label>>}",
{
"target": target,
"label": label,
}
),
"section": ({"title": title, "content": content}) => type_output_tex.coin(
(
"\\begin{section}{<<title>>}\n"
+
"<<content>>\n"
+
"\\end{section}\n"
),
{
"title": title,
"content": this.render_object_internal(
content,
{
"level": (options.level + 1),
"depth": (options.depth + 1),
}
),
}
),
"group": ({"members": members}) => (
members
.map(
x => this.render_object_internal(
x,
{
"depth": options.depth,
"level": options.level,
}
)
)
.join("\n\n")
),
"list": ({"items": items}) => type_output_tex.coin(
(
"\\begin{itemize}\n"
+
"<<items>>"
+
"\\end{itemize}\n"
),
{
"items": (
items
.map(
item => type_output_tex.coin(
"\item{<<item>>}",
{
"item": this.render_object_internal(
item,
{
"level": options.level,
"depth": (options.depth + 1),
}
),
}
)
)
)
}
),
"table": ({"head": head, "rows": rows}) => type_output_tex.coin(
(
"\\begin{tabular}{<<definition>>}\n"
+
"<<head>> \\\\\n"
+
"\\hline\n"
+
"<<body>>\n"
+
"\\end{tabular}\n"
),
{
"definition": (
lib_plankton.list.sequence(head.length)
.map(x => "l")
.join("|")
),
"head": (
head
.map(
cell => this.render_object_internal(
cell,
{
"level": options.level,
"depth": (options.depth + 1),
}
)
)
.join(" & ")
),
"body": (
rows
.map(
row => (
row
.map(
cell => this.render_object_internal(
cell,
{
"level": options.level,
"depth": (options.depth + 1),
}
),
)
.join(" & ")
)
)
.join(" \\\\\n")
),
}
),
},
{
"fallback": (object) => type_output_tex.coin(
(
"\\begin{comment}\n"
+
"<<content>>"
+
"\\end{comment}\n"
),
{
"content": JSON.stringify(object, undefined, " "),
}
),
}
);
}
/**
* @implementation
*/
public render_object(
object : type_object
) : string
{
return this.render_object_internal(object);
}
/**
* @implementation
*/
public render_document(
document : type_document
) : string
{
return type_output_tex.coin(
(
"\\documentclass{article}\n"
+
"<<packages>>\n"
+
"<<settings>>\n"
+
"\\begin{document}\n"
+
"<<main>>\n"
+
"\\end{document}\n"
),
{
"packages": (
[
"hyperref",
"verbatim",
"fontspec",
"unicode-math",
]
.map(
name => ("\\usepackage{" + name + "}\n")
)
.join("")
),
"settings": (
"\\setmainfont{Linux Biolinum O}\n"
+
"\\setmathfont{Noto Mono}\n"
),
"main": this.render_object_internal(
document.content_main,
{
"depth": 1,
}
),
}
);
}
}
/**
*/
output_kind_register(
"tex",
(data, sub) => (
new type_output_tex(
{
}
)
)
);

View file

@ -36,6 +36,7 @@ ${dir_temp}/sd-unlinked.js: \
${dir_source}/outputs/base.ts \ ${dir_source}/outputs/base.ts \
${dir_source}/outputs/implementations/json.ts \ ${dir_source}/outputs/implementations/json.ts \
${dir_source}/outputs/implementations/html.ts \ ${dir_source}/outputs/implementations/html.ts \
${dir_source}/outputs/implementations/tex.ts \
${dir_source}/main.ts ${dir_source}/main.ts
@ ${cmd_log} "compiling …" @ ${cmd_log} "compiling …"
@ ${cmd_mkdir} $(dir $@) @ ${cmd_mkdir} $(dir $@)