+++ /dev/null
-Copyright (c) 2013, Michael Wallner <mike@php.net>.
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are met:
-
- * Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
-DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
-FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
-OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
+++ /dev/null
-function log() {
- // console.log.apply(console, arguments);
-}
-
-function is_constant(s) {
- s = s.replace(/v\d+(_\d+)?$/, "");
- if (s.length < 2) {
- return false;
- }
- return s.toUpperCase(s) === s;
-}
-
-function is_variable(s) {
- return s.substring(0,1) === "$";
-}
-
-var is_in_string = false;
-
-function type(s, nn) {
- var i, j, t;
- //log("type", s);
- // nothing
- if (!s.match(/[a-zA-Z]/)) {
- return;
- }
-
- switch (s) {
- // types
- case "void":
- case "bool":
- case "int":
- case "float":
- case "string":
- case "resource":
- case "array":
- case "object":
- case "callable":
- case "mixed":
- // Zend/SPL
- case "stdClass":
- case "Exception":
- case "ErrorException":
- case "RuntimeException":
- case "UnexpectedValueException":
- case "DomainException":
- case "InvalidArgumentException":
- case "BadMethodCallException":
- case "Closure":
- case "Generator":
- case "Countable":
- case "Serializable":
- case "Traversable":
- case "Iterator":
- case "IteratorAggregate":
- case "ArrayAccess":
- case "ArrayObject":
- case "ArrayIterator":
- case "RecursiveArrayIterator":
- case "SplObserver":
- case "SplSubject":
- case "SplObjectStorage":
- return "<code>";
-
- // keywords
- case "is":
- if (nn !== "H1") {
- return;
- }
- case "extends":
- case "implements":
- if (nn === "H1") {
- return "<br> <em>";
- }
- case "class":
- case "interface":
- case "namespace":
- case "public":
- case "protected":
- case "private":
- case "static":
- case "final":
- case "abstract":
- case "self":
- case "parent":
- // phrases
- case "Optional":
- case "optional":
- return "<em>";
- }
-
- // class members
- if (-1 !== (i = s.indexOf("::"))) {
- t = s.substring(i+2);
- if (!is_constant(t) && !is_variable(t)) {
- // methods
- return "<a href=\"" + s.replace(/::|\\/g, "/") + "\">";
- }
- }
- if (-1 !== (j = s.indexOf("\\")) && s.substr(j+1,1) !== "n") {
- return "<a href=\"" + s.replace(/\\/g, "/").replace(/::|$/, "#") + "\">";
- }
-
- switch (s.toLowerCase()) {
- // variables
- default:
- if (!is_variable(s)) {
- break;
- }
- // special constants
- case "null":
- case "true":
- case "false":
- return "<span class=\"var\">";
- }
-
- // constants
- if (is_constant(s)) {
- return "<span class=\"constant\">";
- }
-}
-
-function node(s, nn) {
- //log("node", s);
-
- var t;
-
- if ((t = type(s, nn))) {
- return $(t).text(s);
- }
- return document.createTextNode(s);
-}
-function wrap(n, nn) {
- var $n = $(n)
- var a = [];
-
- $n.text().split(/([^a-zA-Z0-9_\\\$:]+)/).forEach(function(v) {
- a.push(node(v, nn));
- });
- $n.replaceWith(a);
-}
-function walk(i, e) {
- log("walk", i, e);
-
- $.each($.makeArray(e.childNodes), function(i, n) {
- switch (n.nodeName) {
- case "A":
- case "BR":
- case "HR":
- break;
- case "#text":
- wrap(n, e.nodeName);
- break;
- default:
- walk(-1, n);
- break;
- }
- });
-}
-
-function blink(c) {
- var $c = $(c);
-
- $c.fadeOut("fast").queue(function(next) {
- this.style.color = "red";
- next();
- }).fadeIn("fast").fadeOut("slow").queue(function(next) {
- this.style.color = "";
- next();
- }).fadeIn("slow");
-}
-
-function hashchange() {
- if (location.hash.length > 1) {
- var hash = location.hash.substring(1);
- var name = is_variable(hash) ? ".var" : ".constant";
- var scrolled = false;
-
- $(name).each(hash.substring(hash.length-1) === "_" ? function(i, c) {
- if (c.textContent.substring(0, hash.length) === hash) {
- if (!scrolled) {
- $(window).scrollTop($(c).offset().top - 100);
- scrolled = true;
- }
- blink(c);
- }
- } : function(i, c) {
- if (c.textContent === hash) {
- $(window).scrollTop($(c).offset().top - 100);
- blink(c);
- return false;
- }
- });
- }
-}
-
-$(function() {
- $("h1,h2,h3,h4,h5,h6,p,li,code").each(walk);
- $(window).on("hashchange", hashchange);
- hashchange();
-});
+++ /dev/null
-<?php
-
-error_reporting(E_ALL &~ E_DEPRECATED);
-
-function cut(array $lines, array $specs) {
- $delim = "[[:space:]]+";
- $bytes = [];
- $fields= [];
-
- foreach ($specs as $spec => $value) {
- switch ($spec) {
- case "d":
- $delim = $value;
- break;
- case "b":
- $bytes = $value;
- break;
- case "f":
- $fields = $value;
- break;
- }
- }
-
- $result = [];
- if ($bytes) {
- $func = "substr";
- } else {
- $func = function($a, $o = 0, $l = 0) {
- return join(" ", array_slice($a, $o, $l ? $l+1 : count($a)-$o));
- };
- }
- foreach ($lines as $line) {
- if ($bytes) {
- $spec = $bytes;
- } else {
- $line = split($delim, $line);
- $spec = $fields;
- }
-
- if ($spec[0] == "-") {
- $result[] = $func($line, 0, $spec[1]);
- } elseif ($spec[1] == "-") {
- if (empty($spec[2])) {
- $result[] = $func($line, $spec[0]);
- } else {
- $result[] = $func($line, $spec[0], $spec[2]-$spec[0]);
- }
- } else {
- $result[] = $line{$spec[0]};
- }
- }
- return $result;
-}
-
-function head($file, $lines = 1) {
- $ld = [];
- if (is_resource($file) || ($file = fopen($file, "r"))) {
- while ($lines--) {
- $ld[] = fgets($file);
- }
- }
- return $ld;
-}
-
-function ns($path) {
- $ns = "";
- $parts = explode("/", $path);
- $upper = ctype_upper($path[0]);
- for ($i = 0; $i < count($parts); ++$i) {
- if (!strlen($parts[$i]) || $parts[$i] === ".") {
- continue;
- }
- if (strlen($ns)) {
- if ($upper && !ctype_upper($parts[$i][0])) {
- $ns .= "::";
- } else {
- $ns .= "\\";
- }
- }
- $ns .= $parts[$i];
- $upper = ctype_upper($parts[$i][0]);
- }
- return $ns;
- return str_replace("/", "\\", str_replace("//", "/", trim($file, "/.")));
-}
-
-function urlpath($dir, $file) {
- return (strlen($dir) ? $dir . "/" : "") . basename($file, ".md");
-}
-
-function email($email) {
- if (preg_match("/^([^<]+) <([^>]+)>/", $email, $matches)) {
- list(, $name, $mail) = $matches;
- return sprintf('<a href="mailto:%s">%s</a>',
- htmlspecialchars($mail),
- htmlspecialchars($name));
- }
- var_dump(sscanf($email, "%s <%s>"));
-}
-
-function ls($dir) {
- $dir = rtrim(is_dir($dir) ? $dir : dirname($dir) ."/". basename($dir, ".md"), "/");
- printf("<ul>\n");
- printf("<li>⇐ <a href=>Home</a></li>\n");
- if ($dir !== "." && ($dn = dirname($dir)) !== ".") {
- printf("<li>⇑ <a href=%s>%s</a></li>\n",
- urlpath($dir, ".."),
- ns($dn));
- }
- if (is_dir($dir)) {
- if ($dir !== ".") {
- printf("<ul>\n<li> %s</li>\n", ns($dir));
- }
- if (($glob = glob("$dir/[_a-zA-Z]*.md"))) {
- printf("<ul>\n");
- foreach ($glob as $file) {
- printf("<li>⇒ <a href=\"%s\">%s</a></li>\n",
- urlpath($dir, $file),
- ns("$dir/".basename($file, ".md")));
- }
- printf("</ul>\n");
- }
- if ($dir !== ".") {
- printf("</ul>\n");
- }
- }
-
- printf("</ul>\n");
-}
-
-function ml($file) {
- $pi = pathinfo($file);
- if (!isset($pi["extension"])) {
- return;
- }
- if ($pi["extension"] !== "md") {
- return;
- }
- $dir = $pi["dirname"] . "/" . $pi["filename"];
- if (($glob = glob("$dir/[_a-z]*.md"))) {
- printf("<h2>%s:</h2>\n", !ctype_upper($pi["filename"][0]) ?
- "Functions" : "Methods");
- printf("<ul>\n");
- foreach ($glob as $file) {
- printf("<li><h3><a href=\"%s\">%s</a></h3><p>%s</p><p>%s</p></li>\n",
- urlpath($dir, $file),
- basename($file, ".md"),
- @end(head($file, 3)),
- join(" ", cut(head($file), ["f"=>"1-"]))
- );
- }
- printf("</ul>\n");
- }
-}
-
-function md($file, $res) {
- $file = rtrim($file, "/");
- if (is_file($file) || is_file($file .= ".md")) {
- $pi = pathinfo($file);
-
- switch (@$pi["extension"]) {
- case "md":
- $r = fopen($file, "r");
- $md = MarkdownDocument::createFromStream($r);
- $md->compile(MarkdownDocument::AUTOLINK|MarkdownDocument::TOC);
- print $md->getHtml();
- fclose($r);
- ml($file);
- break;
- case null:
- printf("<h1>%s</h1>", basename($file));
- printf("<pre>%s</pre>\n", htmlspecialchars(file_get_contents($file)));
- break;
- }
- } else {
- $res->setResponseCode(404);
- printf("<h1>Not Found</h1>\n");
- printf("<blockquote><p>Sorry, I could not find <code>%s/%s</code>.</p></blockquote>", dirname($file), basename($file, ".md"));
- }
-}
-
-chdir(__DIR__);
-$t = ["css"=>"text/css", "js"=>"application/javascript"];
-$r = new http\Env\Request;
-$u = new http\Url($r->getRequestUrl());
-$s = new http\Env\Response;
-$b = dirname($_SERVER["SCRIPT_NAME"]);
-$p = ".". substr($u->path, strlen($b));
-
-switch($p) {
-case "./index.php":
- exit;
-case "./index.js":
-case "./index.css":
- $s->setHeader("Content-type", $t[pathinfo($p, PATHINFO_EXTENSION)]);
- $s->setBody(new http\Message\Body(fopen($p, "r")));
- $s->send();
- exit;
-}
-
-ob_start($s);
-
-?>
-<!doctype html>
-<html>
-<head>
- <meta charset="utf-8">
- <title><?=ns($p)?></title>
- <base href="<?=$b?>/">
- <link rel="stylesheet" href="index.css">
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
-</head>
-<body>
- <div class="sidebar">
- <?php ls($p); ?>
- </div>
- <?php if ($p === "./") : ?>
- <h1>Quick Markdown Documentation Browser</h1>
- <p><a href="https://github.com/m6w6/mdref">mdref-v<?php readfile("VERSION")?></a></p>
- <pre><?php
- ob_start(function($s) {
- return htmlspecialchars($s);
- });
- readfile("LICENSE");
- ob_end_flush();
- ?></pre>
- <?php else: ?>
- <?php md($p, $s); ?>
- <?php endif; ?>
-
- <div id="disqus_thread"></div>
-
- <footer>
- <ul>
- <li><a href="https://github.com/m6w6/mdref">mdref-v<?php readfile("VERSION")?></a></li>
- <li><a href="LICENSE">© <?= implode("-", array_unique([2013,idate("Y")]))?></a></li>
- </ul>
- </footer>
- <script src="index.js"></script>
- <?php if ($_SERVER["SERVER_NAME"] != "localhost") : ?>
- <script>
- var disqus_shortname = 'mdref';
- var disqus_identifier = '<?=$p?>';
- (function() {
- var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
- dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
- (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
- })();
- </script>
- <?php endif; ?>
-</body>
-</html>
-<?php
-
-ob_end_flush();
-$s->send();