268 lines
4.3 KiB
TypeScript
268 lines
4.3 KiB
TypeScript
|
|
/**
|
|
*/
|
|
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(
|
|
{
|
|
}
|
|
)
|
|
)
|
|
);
|