From a3ebf23243bb1b333139fdaf335a7ce6bfa5a7fa Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Wed, 28 Mar 2012 18:05:51 +0200 Subject: [PATCH] Just a test of a simple parser... --- index.php | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 index.php diff --git a/index.php b/index.php new file mode 100644 index 0000000..ac3171c --- /dev/null +++ b/index.php @@ -0,0 +1,87 @@ + true); + + $content = preg_replace('@^(\s*%.*)$@imsU', '', $content); + + preg_match_all('@(\\\\include\{([^\}]+)\})@i', $content, $m); + + if (is_array($m[2])) { + foreach($m[2] AS $idx => $include) { + if (isset($ignore_file[$include])) { + echo "Ignore include of " . $include . ".tex...\n"; + $content = str_replace($m[0][$idx], + '' . "\n", + $content); + continue; + } + + echo "Parsing include of " . $include . ".tex...\n"; + $content = str_replace($m[0][$idx], + '' . "\n" + . latex_convert(file_get_contents($include . '.tex'), $depth+1) + . '' . "\n", $content); + } + } + + latex_grep('@\\\\([\w]+)\s@imsU', 'LaTeX commands like \stuff (without commands)', $content); + + latex_grep('@\\\\([\w]+)(\{[^\}]*\})+@ims', 'LaTeX commands like \stuff{...} (with commands)', $content); + + if ($depth == 0) { + arsort($texcommands); + print_r($texcommands); + } + + return $content; +} + +function latex_grep($regexp, $desc, &$content) { + global $texcommands; + + preg_match_all($regexp, $content, $m); + + if (is_array($m[1])) { + foreach($m[1] AS $idx => $command) { + if ($GLOBALS['debug']) echo "Found LaTEX $desc:\n " . trim($m[0][$idx]) . "\n"; + + if ($command == 'emph') { + if ($GLOBALS['debug']) echo " replacement.\n"; + $content = str_replace($m[0][$idx], '' . trim($m[2][$idx]) . '', $content); + continue; + } + + if ($command == 'cmd') { + if ($GLOBALS['debug']) echo " replacement.\n"; + $content = str_replace($m[0][$idx], '' . trim($m[2][$idx]) . '', $content); + continue; + } + + if ($command == 'index') { + if ($GLOBALS['debug']) echo " replacement, ignore.\n"; + continue; + } + + + @$texcommands[$command]++; + + } + } + +} + +$debug = false; + +$texcommands = array(); +$base = file_get_contents('serendipity.tex'); + +$all = latex_convert($base); +$fp = fopen('index.txt', 'w'); + +if (!$fp) die('index.txt needs to be writable'); +echo fwrite($fp, $all) . " bytes written to index.txt\n"; +