Created
June 22, 2011 04:36
-
-
Save chaoxu/1039506 to your computer and use it in GitHub Desktop.
latex 2 html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function latex2html($text){ | |
$s[] = '/\\\\begin{theorem}\\[(.*?)\\]\\s/'; | |
$r[] = '<blockquote class="theorem"><strong>Theorem ($1)</strong> '; | |
$s[] = '/\\\\begin{lemma}\\[(.*?)\\]\\s/'; | |
$r[] = '<blockquote class="theorem"><strong>Lemma ($1)</strong> '; | |
$s[] = '/\\\\begin{problem}\\[(.*?)\\]\\s/'; | |
$r[] = '<blockquote class="problem"><strong>Problem ($1)</strong> '; | |
$s[] = '/\\\\begin{corollary}\\[(.*?)\\]\\s/'; | |
$r[] = '<blockquote class="theorem"><strong>Corollary ($1)</strong> '; | |
$s[] = '/\\\\begin{definition}\\[(.*?)\\]\\s/'; | |
$r[] = '<blockquote class="definition"><strong>Definition ($1)</strong> '; | |
$s[] = '/\\\\begin{conjecture}\\[(.*?)\\]\\s/'; | |
$r[] = '<blockquote class="conjecture"><strong>Conjecture ($1)</strong> '; | |
$s[] = '/\\\\texttt{(.*?)}/s'; | |
$r[] = '<code>$1</code>'; | |
$s[] = '/\\\\textbf{(.*?)}/s'; | |
$r[] = '<strong>$1</strong>'; | |
$s[] = '/\\\\emph{(.*?)}/s'; | |
$r[] = '<em>$1</em>'; | |
$s[] = '/\\\\textsc{(.*?)}/s'; | |
$r[] = '<span style="font-variant: small-caps;">$1</span>'; | |
$s[] = '/\\\\begin{enumerate}\\s*\\\\item/s'; | |
$r[] = '<ol><li>'; | |
$s[] = '/\\\\end{enumerate}/'; | |
$r[] = '</li></ol>'; | |
$s[] = '/\\\\begin{itemize}\\s*\\\\item/'; | |
$r[] = '<ul><li>'; | |
$s[] = '/\\\\end{itemize}/s'; | |
$r[] = '</li></ul>'; | |
$s[] = '/\\\\begin{theorem}\\s*/s'; | |
$r[] = '<blockquote class="theorem"><strong>Theorem</strong> '; | |
$s[] = '/\\\\begin{lemma}\\s*/s'; | |
$r[] = '<blockquote class="theorem"><strong>Lemma</strong> '; | |
$s[] = '/\\\\begin{problem}\\s*/s'; | |
$r[] = '<blockquote class="problem"><strong>Problem</strong> '; | |
$s[] = '/\\\\begin{corollary}\\s*/s'; | |
$r[] = '<blockquote class="theorem"><strong>Corollary</strong> '; | |
$s[] = '/\\\\begin{definition}\\s*/s'; | |
$r[] = '<blockquote class="definition"><strong>Definition</strong> '; | |
$s[] = '/\\\\begin{conjecture}\\s*/s'; | |
$r[] = '<blockquote class="conjecture"><strong>Conjecture</strong> '; | |
$s[] = '/\\s*\\\\end{proof}/s'; | |
$r[] = '<span style="float:right;font-size:200%">■</span>'; | |
$s[] = '/\\\\begin{example}\\s*/s'; | |
$r[] = '<blockquote>strong>Example</strong> '; | |
$s[] = '/\\\\begin{proof}\\s*/s'; | |
$r[] = '<strong>Proof</strong> '; | |
$s[] = '/\\\\begin{remark}\\s*/s'; | |
$r[] = '<blockquote><strong>Remark</strong> '; | |
$s[] = '/\\\\begin{quotation}\\s*/s'; | |
$r[] = '<blockquote>'; | |
$s[] = '/\\\\href{(.*?)}{(.*?)}/'; | |
$r[] = '<a href="$1">$2</a>'; | |
$s[] = '/\\\\url{(.*?)}/'; | |
$r[] = '<a href="$1">$1</a>'; | |
$s[] = '/\\\\includegraphics{(.*?)}/'; | |
$r[] = '<img src="$1" />'; | |
$s[] = '/\\\\section\\*{(.*?)}/'; | |
$r[] = '<h3>$1</h3>'; | |
$s[] = '/\\\\subsection\\*{(.*?)}/'; | |
$r[] = '<h4>$1</h4>'; | |
$text = preg_replace($s,$r,$text); | |
unset($s,$r); | |
$s[] = '\\item '; | |
$r[] = '</li><li>'; | |
$s[] = '\\end{theorem}'; | |
$s[] = '\\end{lemma}'; | |
$s[] = '\\end{problem}'; | |
$s[] = '\\end{corollary}'; | |
$s[] = '\\end{definition}'; | |
$s[] = '\\end{example}'; | |
$s[] = '\\end{remark}'; | |
$s[] = '\\end{quotation}'; | |
$s[] = '\\end{conjecture}'; | |
for($j=0;$j<9;$j++){ | |
$r[] = '</blockquote>'; | |
} | |
$text = str_replace($s,$r,$text); | |
preg_match_all('/\\\\begin{align\\*}(.*?)\\\\end{align\\*}/si',$text,$tex_matches); | |
$text = replacealign($text,$tex_matches); | |
preg_match_all('/\\\\\[(.*?)\\\\\]/si',$text,$tex_matches); | |
$text = replace($text,$tex_matches,'\displaystyle{','}','class="displaystyle"'); | |
preg_match_all('/^\$(.*?[^\\\\])\$/si',$text,$tex_matches); | |
$text = replace($text,$tex_matches); | |
preg_match_all('/[^\\\\]\$(.*?[^\\\\])\$/si',$text,$tex_matches); | |
$text = replace($text,$tex_matches); | |
$text = str_replace('\\$','$',$text); | |
return $text; | |
} | |
function alt($s){ | |
$s = htmlentities(trim($s), ENT_QUOTES); | |
$s = str_replace(array("\r","\n"),array(" "," "),$s); | |
return $s; | |
} | |
function replace($text, $tex_matches, $prefix="", $suffix="", $class=""){ | |
for ($i=0; $i < count($tex_matches[0]); $i++) { | |
$pos = strpos($text, $tex_matches[0][$i]); | |
$formula = $prefix.trim($tex_matches[1][$i]).$suffix; | |
$alt_formula = alt($tex_matches[1][$i]); | |
$replace = '<img '.$class.' src="http://s2.wordpress.com/latex.php?latex=' . rawurlencode($formula) . '&bg=ffffff&fg=000000" title="'.$alt_formula.'" alt="'.$alt_formula.'">'; | |
if($tex_matches[0][$i][0]!="$" && $tex_matches[0][$i][0]!="\\"){ | |
$replace = $tex_matches[0][$i][0].$replace; | |
} | |
$text = substr_replace($text, $replace, $pos,strlen($tex_matches[0][$i])); | |
} | |
return $text; | |
} | |
function replacealign($text, $tex_matches){ | |
for ($i=0; $i < count($tex_matches[0]); $i++) { | |
$pos = strpos($text, $tex_matches[0][$i]); | |
$formula = '\\eqalign{'.trim($tex_matches[1][$i]).'}'; | |
$alt_formula = alt($tex_matches[1][$i]); | |
$formula = str_replace('\\\\','\\cr',$formula); | |
$replace = '<img class="displaystyle" src="http://www.mathtran.org/cgi-bin/mathtran?D=1;tex=' . rawurlencode($formula) . '" title="'.$alt_formula.'" alt="'.$alt_formula.'">'; | |
if($tex_matches[0][$i][0]!="$" && $tex_matches[0][$i][0]!="\\"){ | |
$replace = $tex_matches[0][$i][0].$replace; | |
} | |
$text = substr_replace($text, $replace, $pos,strlen($tex_matches[0][$i])); | |
} | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment