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

224 lines
5.7 KiB
PHP
Raw Permalink Normal View History

2018-06-01 22:35:44 +02:00
<?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 = "";
2018-06-02 16:57:00 +02:00
if ($type === "DOMText") {
if (trim($node->nodeValue) == "") return "";
return $node->textContent;
}
2018-06-01 22:35:44 +02:00
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 .= "
2018-06-02 16:57:00 +02:00
{
\\begin{itemize}%
2018-06-01 22:35:44 +02:00
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "
2018-06-02 16:57:00 +02:00
\\end{itemize}
2018-06-01 22:35:44 +02:00
}%
%";
}
}
else if ($node->tagName == "li") {
2018-06-02 16:57:00 +02:00
$output .= "
\\item";
2018-06-01 22:35:44 +02:00
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") {
2018-06-02 16:57:00 +02:00
$output .= "
\\subsection{%
2018-06-01 22:35:44 +02:00
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "%
}%
";
}
else if ($node->tagName == "h5") {
2018-06-02 16:57:00 +02:00
$output .= "
\\subsubsection{%
2018-06-01 22:35:44 +02:00
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "%
}%
";
}
else if ($node->tagName == "div") {
$output .= "{%
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "%
}%
2018-06-02 16:57:00 +02:00
";
}
else if ($node->tagName == "p") {
$output .= "
";
foreach ($node->childNodes as $childNode) {
$output .= divToTeX($childNode);
}
$output .= "
2018-06-01 22:35:44 +02:00
";
}
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;
2019-08-15 00:12:15 +02:00
2018-06-01 22:35:44 +02:00
}
/**
* 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 = "
2018-06-02 16:57:00 +02:00
\centerVertically{%
$content
2018-06-01 22:35:44 +02:00
}
";
else $output = $content;
return $output;
};
$concordanceIDFile = [
2018-06-04 01:54:32 +02:00
"introduction" => "contentsIntroduction.tex",
"frontend" => "contentsFrontend.tex",
"musdb" => "contentsMusdb.tex",
"themator" => "contentsThemator.tex",
"learnMore" => "contentsTail.tex",
"contact" => "contentsContact.tex",
"introductionPress" => "contentsIntroductionPress.tex",
"germanTexts" => "contentsPressGerman.tex",
2018-06-01 22:35:44 +02:00
];
$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%
%";
}
}
}
2018-06-02 16:57:00 +02:00
$output = explode(PHP_EOL, $output);
foreach ($output as $key => $value) $output[$key] = trim($value);
$output = implode(PHP_EOL, $output);
2018-06-01 22:35:44 +02:00
if (isset($concordanceIDFile[$chapterID])) {
if (!file_exists(__DIR__ . "/$folder")) mkdir(__DIR__ . "/$folder");
file_put_contents(__DIR__ . "/" . $folder . "/" . $concordanceIDFile[$chapterID], $output);
}
}