PHP Classes

How to Use a PHP LaTeX Render Script to Generate HTML Pages Using the Package LaTeXP Decoder: Convert LaTeX documents to HTML pages

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-02-07 (3 days ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
latexp-decoder 1.0MIT/X Consortium ...5HTML, PHP 5, Text processing, Documents
Description 

Author

This package can convert LaTeX documents to HTML pages.

It provides a script that can take the name of a LaTeX file stored in a directory as a parameter.

The script can read and process the LaTex document file and generate HTML to display the document contents on a Web page.

The package includes sample LaTeX files to publish scientific documents as an HTML page.

The formatting instructions supported by this script include the LaTeX, MathJax, and a few added formatting roles.

Picture of Alexander Eist
  Performance   Level  
Name: Alexander Eist <contact>
Classes: 5 packages by
Country: Czech Republic
Innovation award
Innovation award
Nominee: 3x

Example

<?php
$debug
= 1;
// ?????? ????????????? ???????:
define ('DATA_DIR', "data");
define ('DS', DIRECTORY_SEPARATOR);
define ('FILE_EXTENSION', "tex"); // ?????????? ?????? ??????



$cat = (array_key_exists('cat', $_REQUEST))? $_REQUEST['cat']:"chatGPT";
$tex = (array_key_exists('tex', $_REQUEST))? $_REQUEST['tex']:"learn_LaTexP";
$show_row_numbers = (array_key_exists('show_row_numbers', $_REQUEST))? $_REQUEST['show_row_numbers']:false;
$tex_path = DATA_DIR. DS . $cat . DS . $tex . "." . FILE_EXTENSION;
$dirs = get_dirs();
extract($_GET);
//if($debug)print_mess( basename(__file__). " " . __line__ . " \$_REQUEST:" , $_REQUEST);
//if($debug)print_mess( basename(__file__). " " . __line__ . " \$cat " , $cat);
//if($debug)print_mess( basename(__file__). " " . __line__ . " \$tex_path " , $tex_path);

//$f_name = array_key_exists('tex', $_REQUEST)? $_REQUEST['tex']:"learn_LaTexP" ;
$directory = DATA_DIR.DS.$cat;//DATA_DIR.DS.$dirs[0].DS.$f_name;
$extension = '.'. FILE_EXTENSION; // ???????? ?? ?????? ????????? ?????

$files = scanDirectoryForFiles($directory, $extension);
//if($debug)print_mess( basename(__file__). " " . __line__ . " \$files " , $files);

// create menu
$menu = create_menu($dirs);
echo
$menu;



// ??????? ?????????
//if($debug)print_mess( basename(__file__). " " . __line__ . " Files with extension " . $extension , $files);
//if($debug)print_mess( basename(__file__). " " . __line__ . " \$menu: " , $menu);
//-------
// ????????? ?????????? ??????
$text = file_get_contents($tex_path); // 20241125.txt") ;

$head ="";
$titles = get_title_subtitle();
// if ($debug) print_mess(__line__ . " \$text ", $text );
/// functions ////////////////////
function create_menu($dirs_){
$menu = "<nav class='navbar navbar-expand-lg navbar-light bg-light'>
          <div class='container-fluid'>
            <button class='navbar-toggler' type='button' data-bs-toggle='collapse' data-bs-target='#navbarNav' aria-controls='navbarNav' aria-expanded='false' aria-label='Toggle navigation'>
              <span class='navbar-toggler-icon'></span>
            </button>
            <div class='collapse navbar-collapse' id='navbarNav'>
              <ul class='navbar-nav'>"
;

foreach (
$dirs_ as $key => $val) {
   
// ????????? ?????? ?????????? ??? ????????? ?????? ??????
   
$menu .= "<li class='nav-item dropdown'>
                <a class='nav-link dropdown-toggle' href='?' id='navbarDropdown
{$key}' role='button' data-bs-toggle='dropdown' aria-expanded='false'>
                 
{$val}
                </a>
                <ul class='dropdown-menu' aria-labelledby='navbarDropdown
{$key}'>";

   
$_ = scandir(DATA_DIR . DS . $val); // ????????? ????????, ????????? ?? ????? ??????
   
$_ = clear_dots($_);

    foreach (
$_ as $f => $tex_) {
        if (
str_ends_with($tex_, FILE_EXTENSION)){ // only fo PHP 8.x (substr($tex_, -1*(strlen(FILE_EXTENSION) )) == FILE_EXTENSION) {
           
$tex = pathinfo($tex_, PATHINFO_FILENAME);
           
$menu .= "<li><a class='dropdown-item' href='?cat={$dirs_[$key]}&tex={$tex}'>{$tex_}</a></li>";
        }
    }

   
$menu .= "</ul></li>";
}

$menu .= "</ul></div></div></nav>";

return
$menu;
}
function
get_dirs(){
$dir_ = clear_dots(scandir(DATA_DIR)); // here must be only directories
$dirs = []; // the list of directories with latex formatted files
foreach($dir_ as $key=>$val){ // ???????? ?????? ????????? ?????????? ???????????? ?? ?????
 
if(@scandir(DATA_DIR. DS .$val)) $dirs[] = $val; // ???????? ?????? ????????? ??? ????, ??????? ??? ???????? ???????? ????
}
return
$dirs;
}
function
clear_dots($a=[]){
$ret=$a;
array_shift($ret); // remove the .
array_shift($ret); // remove the ..
return $ret;
}
// ??????? markdown ????????? ? ?????? ?? HTML ???????? ?????? ? ?????? ?????? '/Me:(.*?)chatGPT:/s'
function markdown_to_html($text){
// $text = preg_replace('/### Me: (.*?)chatGPT:/s', "<p class='me'>\$1</p>", $text);
// $text = preg_replace('/### chatGPT: (.*?)Me:/s', "<p class='chatGPT'>\$1</p>", $text);
   
$text = preg_replace('/###### (.*?)\n/', '<h6>$1</h6>', $text);
   
$text = preg_replace('/##### (.*?)\n/', '<h5>$1</h5>', $text);
   
$text = preg_replace('/#### (.*?)\n/', '<h4>$1</h4>', $text);
   
$text = preg_replace('/### (.*?)\n/', '<h3>$1</h3>', $text);
   
$text = preg_replace('/## (.*?)\n/', '<h2>$1</h2>', $text);
   
$text = preg_replace('/# (.*?)\n/', '<h1>$1</h1>', $text);
   
$text = preg_replace('/ccc (.*?)\n/', '<center>$1</center>', $text);
   
$text = preg_replace('/---(.*?)\n/', "<hr>", $text);
   
$text = preg_replace('/-- (.*?)\n/', '<br><i>$1</i>', $text);
   
$text = preg_replace('/- (.*?)\n/', '<br>$1', $text);
   
$text = preg_replace('/```(.*?)\n/', '<b>$1</b>', $text);
   
$text = preg_replace('/\$(.*)\$/', "<span class='php_var'>$\$1</span>", $text);
   
$text = preg_replace('/;/', ";\n<br>", $text);
   
$text = preg_replace('/\*\*\*(.*?)\n/', "<span class='red'> \$1</span>", $text); // 3 ????????? - ???????
   
$text = preg_replace('/\*\*(.*?)\*\*/', '<span class="tenten">$1</span>', $text); // 2 ????????? - ????? ????
   
$text = preg_replace('/\*(.*?)\*/', '<span class="bold">$1</span>', $text); // 1 ????????? - ????
   
$text = preg_replace('/\/\/ (.*?)\n/', "<span class='comment'>// \$1</span>", $text);
   
$text = preg_replace('/!!! (.*?)\n/', '<div style="display:none">$1</div>', $text);
   
$text = preg_replace('/___ (.*?)\n/', '<small>$1</small>', $text);
   
//
     //$text = preg_replace('/<span class="tenten">Me: <\/span>(.*?)<span class="tenten">chatGPT: <\/span>/s', "<div class='me'>$1<\/div>", $text);
    
$text = preg_replace('/<span class="tenten">Me: <\/span>(.*?)<span class="tenten">chatGPT: <\/span>/s', "<span class='me'>$1</span>", $text);
    return
$text;
}
// ??????????? ????? ? HTML
function convertToHTML($t) {

   
$text = markdown_to_html($t);
   
// ??????? ??????? LaTeX ?? ?????? MathJax
   
$text = preg_replace('/\$(.*?)\$/', '<span class="mathjax">\\($1\\)</span>', $text);


        return
$text;
}
// ?????? ?????????
function get_title_subtitle()
{
// ???????? ?????? ?????? ?????????????? ?????
   
GLOBAL $tex_path, $debug;
   
$ret = [];
   
$title = 0;
   
$_ = file($tex_path);
   
// if ($debug) print_mess(__line__ . " \$_ ", $_ );
   
for ($i = 0; $i < count($_); $i++) {
       
$__ = trim($_[$i]); // ??????? ?? ???????? ???????? ? ?????? ?????????? ??????
       
if (strlen($__) > 0) { // ???? ?????? ?? ??????
           
$ret[] = markdown_to_html($__); // ????????? ?? ?????????? ? ???
           
$title++; // ????????? ????? ? ???????????? ?????
       
}
       
// if ($debug) print_mess(__line__ . " \$__ ", $__ );
       
if ($title == 2) return $ret; // ???? ????????? - ??????????
   
}
   
// ?????????? ????????? ? $title 1 ??? 0
   
if (count($ret) == 0) {
       
$ret[] = ""; // ????????? ?????? ?????????, ???? ??? ? ???? ?????????
       
$ret[] = ""; // ????????? ?????? ????????????, ???? ??? ? ???? ?????????
   
} else {
       
$ret[] = ""; // ????????? ?????? ????????????, ???? ??? ? ???? ?????????
   
}
    return
$ret; // ??????????? ??????? $ret ? ????? ??????????
}
// debug print
function print_mess($sender, $data){
echo(
"<div class='debug'> $sender<pre>");
print_r($data);
echo(
"</pre></did>");

}
/////////////////////////////////////////////////////////
function scanDirectoryForFiles($directory, $extension) {
   
// ?????????, ?????????? ?? ??????????
   
if (!@scandir($directory)) {
        return
"??????: ????????? ???? ?? ???????? ???????????!";
    }

   
// ???????? ?????? ???? ?????? ? ????? ? ??????????
   
$files = scandir($directory);

   
// ?????? ??? ???????? ?????? ? ?????? ??????????
   
$filteredFiles = [];

   
// ???????? ?? ???? ??????
   
foreach ($files as $file) {
       
// ?????????? . ? .. (??????????? ???????? ? ???????????)
       
if ($file !== '.' && $file !== '..') {
           
// ?????????, ??? ???? ????? ?????? ?????????
           
if (substr($file, -strlen($extension)) === $extension) {
               
$filteredFiles[] = $file; // ????????? ? ?????????
           
}
        }
    }

   
// ?????????? ?????? ?????? ? ???????? ??????????
   
return $filteredFiles;
}

// main //////////
$text = file_get_contents($tex_path);

$i = 0; // ??????? ?????
if($show_row_numbers){
 
$text = preg_replace_callback(
     
'/\n/',
      function () use (&
$i) {
         
$i++;
          return
"\n $i : ";
      },
     
$text
 
);
}
$htmlContent = convertToHTML($text);

// if ($debug) print_mess(__line__ . " \$title ", $title );
// if ($debug) print_mess(__line__ . " \$htmlContent ", $htmlContent );
$footer = <<<FOOTER
<div class="container">
<div class = "row">
<footer class="footer mt-auto py-3 bg-dark text-light">
    <div class="container text-center">
        <div class="row">
           
            <div class="col-md-6">
                <a href="https://opensource.org/licenses/MIT" target="_blank">???????? MIT:
<!--img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/MIT_logo.svg/1280px-MIT_logo.svg.png" alt="MIT License" style="width: 50px; height: auto;"-->
                    <div class="svg_mit">
    <img class="svg_mit" src="../../../../aekap.api/images/mit_icon.svg" alt="MIT License" >
                    </div>
                </a>
            </div>
            <div class="col-md-6">
                <h5>????????:</h5>
                git:
                <p>Email: [email protected]
                <br>Viber: +420 792 455 840
                <br>git: <a href="https://github.com/alexeist/LaTeXP-decoder/">https://github.com/alexeist/LaTeXP-decoder/</a>
                </p>
            </div>
        </div>
    </div>
  </footer>
 </div>
</div>

FOOTER;

$htmlOutput = <<<HTML
<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
{$titles[0]}</title>
    <link href="include/css/bootstrap.min.css" rel="stylesheet">
    <link href="include/css/default.min.css" rel="stylesheet" >
    <script src="include/js/tex-mml-chtml.js"></script>
    <script src="include/js/highlight.min.js"></script>
    <script>hljs.highlightAll();</script>
    <style>
        body:{font-size:large;}
        .tenten{font-weight:bolder; color:#036;}
        mjx-math{font-size:larger!important; color:#404;}
        ul{list-style: none}
        .mt-4{background-color:#ccf!important; padding:20px;}
        .php_var{color: #006; font-weight:bolder}
        .comment{color: #aaa!important}
        .me{background-color: #eef; border-radius:10px; border:thin solid black;padding: 10px;}
        .chatGPT{background-color: #efe; border-radius:10px; border:thin solid black;padding: 10px}
        .comment{color:#999}
        .red{color:red}
        .bold{font-weight:bold}
        .svg_mit{height: 50px; width:auto;}
    </style>
</head>
<body>

<div class="container mt-4">
  <div class="row">
    <div class="card">

        <div class="card-body">
            <div class="content">
               
{$htmlContent}
            </div>
        </div>
    </div>
</div>
</div>
{$footer}
<script src="include/js/bootstrap.bundle.min.js"></script>
<script>
    window.onload = function() {
        MathJax.typeset();
    };
</script>

</body>
</html>
HTML;

// ??????? ??????????????? HTML
echo $htmlOutput;
?>



Details

Hello here! Here you will find a simple LaTexP decoding machine (LaTeX MathJax Plus origin formatting rooles) for scientific articles for publication in HTML format. System Requirements - PHP ver 8 or later A complete list of the commands used is given in the LaTexP-file that opens by default. All texts are made using neural networks, whose names you will find in the menu. How to use: Menus and submenus are generated automatically in any language. It is enough to upload to the server into the project directory in the data folder of the categories's folders and into them any documents in LaTexP format (extended latex plus format) text with the extension .tex. Make the file name for publishing according to the following template: < any name in any language > .tex Note: the version of PHP must be 8.x


  Files folder image Files (11)  
File Role Description
Files folder imagedata (1 directory)
Files folder imageinclude (2 directories)
Accessible without login Plain text file index.php Example Configuration script
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (11)  /  data  
File Role Description
Files folder imagechatGPT (4 files)

  Files folder image Files (11)  /  data  /  chatGPT  
File Role Description
  Accessible without login Plain text file learn_LaTexP.tex Data Auxiliary data
  Accessible without login Plain text file membrana.tex Data Auxiliary data
  Accessible without login Plain text file tachion.tex Data Auxiliary data
  Accessible without login Plain text file TheoryOfValues.tex Data Auxiliary data

  Files folder image Files (11)  /  include  
File Role Description
Files folder imagecss (2 files)
Files folder imagejs (3 files)

  Files folder image Files (11)  /  include  /  css  
File Role Description
  Accessible without login Plain text file bootstrap.min.css Data Auxiliary data
  Accessible without login Plain text file default.min.css Data Auxiliary data

  Files folder image Files (11)  /  include  /  js  
File Role Description
  Accessible without login Plain text file bootstrap.bundle.min.js Data Auxiliary data
  Accessible without login Plain text file highlight.min.js Data Auxiliary data
  Accessible without login Plain text file tex-mml-chtml.js Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0