mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			58 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			58 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/**
 | 
						|
 * @link http://www.yiiframework.com/
 | 
						|
 * @copyright Copyright (c) 2008 Yii Software LLC
 | 
						|
 * @license http://www.yiiframework.com/license/
 | 
						|
 */
 | 
						|
 | 
						|
namespace yii\apidoc\helpers;
 | 
						|
 | 
						|
use cebe\jssearch\Indexer;
 | 
						|
use cebe\jssearch\tokenizer\StandardTokenizer;
 | 
						|
use cebe\jssearch\TokenizerInterface;
 | 
						|
 | 
						|
class ApiIndexer extends Indexer
 | 
						|
{
 | 
						|
    protected function generateFileInfo($file, $contents, $basePath, $baseUrl)
 | 
						|
    {
 | 
						|
        // create file entry
 | 
						|
        if (preg_match('~<h1>(.*?)</h1>~s', $contents, $matches)) {
 | 
						|
            $title = str_replace('¶', '', strip_tags($matches[1]));
 | 
						|
        } elseif (preg_match('~<title>(.*?)</title>~s', $contents, $matches)) {
 | 
						|
            $title = strip_tags($matches[1]);
 | 
						|
        } else {
 | 
						|
            $title = '<i>No title</i>';
 | 
						|
        }
 | 
						|
 | 
						|
        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]);
 | 
						|
            if (mb_strlen($description) > 1000) { // TODO truncate by words
 | 
						|
                $description = mb_substr($description, 0, 1000) . '...';
 | 
						|
            }
 | 
						|
        } else {
 | 
						|
            $description = '';
 | 
						|
        }
 | 
						|
 | 
						|
        return [
 | 
						|
            'u' => $baseUrl . str_replace('\\', '/', substr($file, strlen(rtrim($basePath, '\\/')))),
 | 
						|
            't' => $title,
 | 
						|
            'd' => $description,
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return TokenizerInterface
 | 
						|
     */
 | 
						|
    public function getTokenizer()
 | 
						|
    {
 | 
						|
        $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);
 | 
						|
        }
 | 
						|
        return $tokenizer;
 | 
						|
    }
 | 
						|
} |