ApiIndexer.php 2.06 KB
Newer Older
1 2
<?php
/**
3 4 5
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
6 7 8 9 10 11 12 13
 */

namespace yii\apidoc\helpers;

use cebe\jssearch\Indexer;
use cebe\jssearch\tokenizer\StandardTokenizer;
use cebe\jssearch\TokenizerInterface;

14 15 16
/**
 * ApiIndexer indexes framework API
 */
17 18
class ApiIndexer extends Indexer
{
19 20 21 22 23 24 25
    /**
     * @param string $file
     * @param string $contents
     * @param string $basePath
     * @param string $baseUrl
     * @return array
     */
26
    protected function generateFileInfo($file, $contents, $basePath, $baseUrl)
27 28 29 30 31 32 33 34 35
    {
        // create file entry
        if (preg_match('~<h1>(.*?)</h1>~s', $contents, $matches)) {
            $title = str_replace('&para;', '', strip_tags($matches[1]));
        } elseif (preg_match('~<title>(.*?)</title>~s', $contents, $matches)) {
            $title = strip_tags($matches[1]);
        } else {
            $title = '<i>No title</i>';
        }
36 37 38 39 40

        if (preg_match('~<div id="classDescription">\s*<strong>(.*?)</strong>~s', $contents, $matches)) {
            $description = strip_tags($matches[1]);
        } elseif (preg_match('~<p>(.*?)</p>~s', $contents, $matches)) {
            $description = strip_tags($matches[1]);
41 42
            if (mb_strlen($description) > 1000) { // TODO truncate by words
                $description = mb_substr($description, 0, 1000) . '...';
43 44 45 46 47
            }
        } else {
            $description = '';
        }

48 49 50 51 52 53
        return [
            'u' => $baseUrl . str_replace('\\', '/', substr($file, strlen(rtrim($basePath, '\\/')))),
            't' => $title,
            'd' => $description,
        ];
    }
54 55

    /**
56 57 58 59
     * @return TokenizerInterface
     */
    public function getTokenizer()
    {
60 61 62 63 64 65
        $tokenizer = parent::getTokenizer();
        if ($tokenizer instanceof StandardTokenizer) {
            // yii is part of every doc and makes weird search results
            $tokenizer->stopWords[] = 'yii';
            $tokenizer->stopWords = array_unique($tokenizer->stopWords);
        }
66 67
        return $tokenizer;
    }
68
}