<?php
/*
* This is the easy script that read the file with LaTeX code and generate the html page with MathJax library
* This php script has been created by Alexander Eist with using chatGPT ver 4o at november 2024
* MIT license for all
* Bug: incorrect interpretates the sigh "-" in the LaTex fiile in some formulas
* Solution: easy replace minus into other member in formula!
*/
// put here the url of text file
$text = file_get_contents("path_to_scientific_document.txt") ;
// transform to HTML
function convertToHTML($text) {
// Change the markdown title and lists to HTML
$text = preg_replace('/### (.*?)\n/', '<h3>$1</h3>', $text);
$text = preg_replace('/#### (.*?)\n/', '<h4>$1</h4>', $text);
$text = preg_replace('/- (.*?)\n/', '<ul><li>$1</li></ul>', $text);
// Change the LaTeX to MathJax formulas
$text = preg_replace('/\$(.*?)\$/', '<span class="mathjax">\\($1\\)</span>', $text);
$text = preg_replace('/\*\*(.*?)\*\*/', '<span class="tenten">$1</span>', $text);
$text = preg_replace('/---/', "<hr>", $text);
return $text;
}
// transform the text to html
$htmlContent = convertToHTML($text);
// create HTML with using bootstrap
$htmlOutput = <<<HTML
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Title</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEJ2QFj7b4Cpm4khiC5z7Pyq5rGzK8fWgUxtDVRgR1FmgDJsmLRuOB8MPl9p5" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<style>
.tenten{font-weight:bolder; color:#033;}
mjx-math{font-weight:bolder!important; color:#818;}
ul{list-style: none}
</style>
</head>
<body>
<div class="container mt-4">
<div class=""row>
<div class="card">
<div class="card-header">
<h2>LaTeX & MathJax interpretator </h2>
<h4>Decoder for scientific documents to html</h4>
</div>
<div class="card-body">
<div class="content">
{$htmlContent}
</div>
</div>
</div>
</div>
</div>
<script>
window.onload = function() {
MathJax.typeset();
};
</script>
</body>
</html>
HTML;
// print generatesd HTML
echo $htmlOutput;
?>
|