This repository has been archived on 2023-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
about-brochure/htmlToTeX.php

222 lines
5.5 KiB
PHP

<?PHP
/**
* File containing functions for generating PDFs from HTML contents.
*
* @author Joshua Ramon Enslin <joshua@jrenslin.de>
*/
/**
* Converts a DOM div element to TeX.
*
* @param DOMElement|DOMText $node Element to convert.
*
* @return string
*/
function divToTeX($node):string {
$type = get_class($node);
$output = "";
if ($type === "DOMText") {
if (trim($node->nodeValue) == "") return "";
return $node->textContent;
}
else {
if ($node->tagName == "img") {
$output = "
\\begin{figure}[h]
\\begin{center}
\\includegraphics[width=\columnwidth]{../../" . transform($node->getAttribute("src")) . "}
\\end{center}
\\end{figure}
";
}
else if ($node->tagName == "ul") {
if ($node->getAttribute("class") == "dl") {
$output .= "
\\begin{description}
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "
\\end{description}
%";
}
else {
$output .= "
{
\\begin{itemize}%
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "
\\end{itemize}
}%
%";
}
}
else if ($node->tagName == "li") {
$output .= "
\\item";
if ($node->getAttribute("data-title") != "") $output .= "[" . transform($node->getAttribute("data-title")) . "]{ \\hfill \\\\";
else $output .= "{%
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "%
}%
";
}
else if ($node->tagName == "h4") {
$output .= "
\\subsection{%
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "%
}%
";
}
else if ($node->tagName == "h5") {
$output .= "
\\subsubsection{%
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "%
}%
";
}
else if ($node->tagName == "div") {
$output .= "{%
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "%
}%
";
}
else if ($node->tagName == "p") {
$output .= "
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "
";
}
else if ($node->tagName == "a") {
if ($node->getAttribute("class") == "url") {
$output .= "\\url{" . transform($node->textContent) . "}";
}
else {
$output .= "\\href{" . $node->getAttribute("href") . "}{%
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "
}%
";
}
}
else
print_r($node);
}
return $output;
}
/**
* Function DOMtoTeX transforms a DOM element into its proper TeX output.
*
* @param DOMElement $inputs A section element.
* @param string $folder Folder to store in.
*
* @return void
*/
function DOMtoTeX(DOMElement $inputs, string $folder) {
$centerTextTeX = function($content, $centered = false) {
if ($centered) $output = "
\centerVertically{%
$content
}
";
else $output = $content;
return $output;
};
$concordanceIDFile = [
"introduction" => "contentsIntroduction.tex",
"frontend" => "contentsFrontend.tex",
"musdb" => "contentsMusdb.tex",
"themator" => "contentsThemator.tex",
"learnMore" => "contentsTail.tex",
"contact" => "contentsContact.tex",
];
$chapterID = $inputs->getAttribute("id");
$chapterTitle = $inputs->getElementsByTagName("h2")[0]->nodeValue;
$output = "%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Chapter: $chapterID
%
\\chapter{" . transform($chapterTitle) . "}%
";
foreach ($inputs->childNodes as $section) {
if (!isset($section->tagName) or $section->tagName != "div") continue;
foreach ($section->childNodes as $sectionElem) {
if (!isset($sectionElem->tagName)) continue;
if ($sectionElem->tagName == "h3") {
$output .= "
\\section{" . transform($sectionElem->textContent) . "}%";
}
else if ($sectionElem->tagName == "div") {
if ($sectionElem->getAttribute("class") == "centerVertically") $centered = true;
else $centered = false;
$sectionOutput = "";
foreach ($sectionElem->childNodes as $node) {
$sectionOutput .= divToTeX($node);
}
$output .= $centerTextTeX($sectionOutput, $centered);
$output .= "%
\\newpage%
%";
}
}
}
$output = explode(PHP_EOL, $output);
foreach ($output as $key => $value) $output[$key] = trim($value);
$output = implode(PHP_EOL, $output);
if (isset($concordanceIDFile[$chapterID])) {
if (!file_exists(__DIR__ . "/$folder")) mkdir(__DIR__ . "/$folder");
file_put_contents(__DIR__ . "/" . $folder . "/" . $concordanceIDFile[$chapterID], $output);
}
}
?>