200 lines
5.1 KiB
PHP
200 lines
5.1 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") 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 .= "
|
|
\\itemize{%
|
|
";
|
|
foreach ($node->childNodes as $childNode) {
|
|
$output .= divToTeX($childNode);
|
|
}
|
|
$output .= "
|
|
}%
|
|
%";
|
|
}
|
|
}
|
|
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 == "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%
|
|
%";
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($concordanceIDFile[$chapterID])) {
|
|
if (!file_exists(__DIR__ . "/$folder")) mkdir(__DIR__ . "/$folder");
|
|
file_put_contents(__DIR__ . "/" . $folder . "/" . $concordanceIDFile[$chapterID], $output);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|