1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- class Markdown_Sami_Twig_Extension extends Twig_Extension {
- public function getFunctions() {
- return array(
- new Twig_Function("toc", array($this, "toc")),
- new Twig_Function("href", array($this, "href")),
- new Twig_Function("dep", array($this, "dep"))
- );
- }
- private static function dep($ref) {
- return sprintf("%s%s", $ref->getName(), $ref->getDeprecated() ? "(DEP)" : "");
- }
- private static function tab(int $depth) {
- return str_repeat("\t", $depth);
- }
- public static function href(string $ltxt, string $lurl, string $desc = null) {
- $desc = $desc ? sprintf(' "%s"', $desc) : null;
- return sprintf("[%s](%s%s)", $ltxt, str_replace("\\", "_", $lurl), $desc);
- }
- public static function toc(array $tree, int $depth) {
- $append = (function(string &$into, int $depth, int $idx, string $from) {
- $into .= sprintf("%s%d. %s\n", self::tab($depth), $idx + 1, $from);
- });
- $str = "";
- foreach ($tree as $key => $elem) {
- $append($str, $depth, $key, self::href(is_string($elem[1]) ? $elem[1] : self::dep($elem), sprintf("#%s", $elem[1]), $elem[1]));
- if (isset($elem[2]) && !empty($elem[2])) {
- $last = array();
- $pos = 0;
-
- foreach($elem[2] as $key2 => $elem2) {
- if (isset($elem2[2]) && !empty($elem2[2]))
- $last[$key2] = $elem2;
- else
- $append($str, ($depth+1), $pos++, self::href(self::dep($elem2[1]), sprintf("#%s", $elem2[1]), $elem2[1]));
- }
- foreach($last as $key2 => $elem2) {
- $append($str, ($depth+1), $pos + $key2, self::href(is_string($elem2[1]) ? $elem2[1] : self::dep($elem2[1]), sprintf("#%s", $elem2[1]), $elem2[1]));
- $str .= self::toc($elem2[2], ($depth+2));
- }
- }
- }
- return $str;
- }
- }
- use Sami\Sami;
- use Sami\RemoteRepository\GitHubRemoteRepository;
- use Sami\Version\GitVersionCollection;
- use Symfony\Component\Finder\Finder;
- $iterator = Finder::create()
- ->files()
- ->name('*.php')
- ->in($dir = './src')
- ;
- $versions = GitVersionCollection::create($dir)
- ->add('master', 'master branch')
- ;
- $s = new Sami($iterator, array(
- 'theme' => 'markdown',
- 'template_dirs' => array( __DIR__ . '/markdown'),
- 'versions' => $versions,
- 'title' => 'Symfony2 API',
- 'build_dir' => __DIR__.'/build/%version%',
- 'cache_dir' => __DIR__.'/cache/%version%',
- ));
- $s["twig"]->addExtension(new Markdown_Sami_Twig_Extension());
- return $s;
- ?>
|