sami.conf.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. class Markdown_Sami_Twig_Extension extends Twig_Extension {
  3. public function getFunctions() {
  4. return array(
  5. new Twig_Function("toc", array($this, "toc")),
  6. new Twig_Function("href", array($this, "href")),
  7. new Twig_Function("dep", array($this, "dep"))
  8. );
  9. }
  10. private static function dep($ref) {
  11. return sprintf("%s%s", $ref->getName(), $ref->getDeprecated() ? "(DEP)" : "");
  12. }
  13. private static function tab(int $depth) {
  14. return str_repeat("\t", $depth);
  15. }
  16. public static function href(string $ltxt, string $lurl, string $desc = null) {
  17. $desc = $desc ? sprintf(' "%s"', $desc) : null;
  18. return sprintf("[%s](%s%s)", $ltxt, str_replace("\\", "_", $lurl), $desc);
  19. }
  20. public static function toc(array $tree, int $depth) {
  21. $append = (function(string &$into, int $depth, int $idx, string $from) {
  22. $into .= sprintf("%s%d. %s\n", self::tab($depth), $idx + 1, $from);
  23. });
  24. $str = "";
  25. foreach ($tree as $key => $elem) {
  26. $append($str, $depth, $key, self::href(is_string($elem[1]) ? $elem[1] : self::dep($elem), sprintf("#%s", $elem[1]), $elem[1]));
  27. if (isset($elem[2]) && !empty($elem[2])) {
  28. $last = array();
  29. $pos = 0;
  30. foreach($elem[2] as $key2 => $elem2) {
  31. if (isset($elem2[2]) && !empty($elem2[2]))
  32. $last[$key2] = $elem2;
  33. else
  34. $append($str, ($depth+1), $pos++, self::href(self::dep($elem2[1]), sprintf("#%s", $elem2[1]), $elem2[1]));
  35. }
  36. foreach($last as $key2 => $elem2) {
  37. $append($str, ($depth+1), $pos + $key2, self::href(is_string($elem2[1]) ? $elem2[1] : self::dep($elem2[1]), sprintf("#%s", $elem2[1]), $elem2[1]));
  38. $str .= self::toc($elem2[2], ($depth+2));
  39. }
  40. }
  41. }
  42. return $str;
  43. }
  44. }
  45. use Sami\Sami;
  46. use Sami\RemoteRepository\GitHubRemoteRepository;
  47. use Sami\Version\GitVersionCollection;
  48. use Symfony\Component\Finder\Finder;
  49. $iterator = Finder::create()
  50. ->files()
  51. ->name('*.php')
  52. ->in($dir = './src')
  53. ;
  54. $versions = GitVersionCollection::create($dir)
  55. ->add('master', 'master branch')
  56. ;
  57. $s = new Sami($iterator, array(
  58. 'theme' => 'markdown',
  59. 'template_dirs' => array( __DIR__ . '/markdown'),
  60. 'versions' => $versions,
  61. 'title' => 'Symfony2 API',
  62. 'build_dir' => __DIR__.'/build/%version%',
  63. 'cache_dir' => __DIR__.'/cache/%version%',
  64. ));
  65. $s["twig"]->addExtension(new Markdown_Sami_Twig_Extension());
  66. return $s;
  67. ?>