Skip to content

Commit

Permalink
First Craft 3 Version
Browse files Browse the repository at this point in the history
  • Loading branch information
gudehus committed Apr 7, 2018
0 parents commit a3a22dd
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,9 @@
# SearchExcerpt Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 1.2 - 2018-04-03
### Added
- Initial release as Craft 3 plugin
9 changes: 9 additions & 0 deletions LICENSE.md
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2018 Jörg Gudehus

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
@@ -0,0 +1,46 @@
# searchExcerpt plugin for Craft CMS

Display only the part of a text which contains a given searchterm

![Screenshot](resources/screenshots/plugin_logo.png)

## Installation

To install searchExcerpt, follow these steps:

1. Download & unzip the file and place the `searchexcerpt` directory into your `craft/plugins` directory
2. -OR- do a `git clone https://github.com/gudehus/searchexcerpt.git` directly into your `craft/plugins` folder. You can then update it with `git pull`
3. -OR- install with Composer via `composer require gudehus/searchexcerpt`
4. Install plugin in the Craft Control Panel under Settings > Plugins
5. The plugin folder should be named `searchexcerpt` for Craft to see it. GitHub recently started appending `-master` (the branch name) to the name of the folder for zip file downloads.

searchExcerpt works on Craft 2.4.x and Craft 2.5.x.

## searchExcerpt Overview

Finds a given searchterm in a text and higlights it.

## Using searchExcerpt

{{ craft.searchExcerpt.from(text, searchterm[[, padding][, class]]) }}

where text ist the initial text
searchterm is the term which should be higlighted
padding is the (optional) amount of characters which are added to the left an right of the highlighted term
class ist the (optional) css class of the <span> which enclosed the term

e.g.

It’s true, this site doesn’t have a whole lot of content yet, but don’t worry. Our web developers have just installed the CMS, and they’re setting things up for the content editors this very moment. Soon Test.albatros-golf-berlin.de will be an oasis of fresh perspectives, sharp analyses, and astute opinions that will keep you coming back again and again.

{{ craft.searchExcerpt.from(entry.body, 'of', 20) }} equals

… have a whole lot of content yet, but …

## searchExcerpt Roadmap

Some things to do, and ideas for potential features:

* Release it

Brought to you by [Jörg Gudehus](http://joerggudehus.de)
40 changes: 40 additions & 0 deletions composer.json
@@ -0,0 +1,40 @@
{
"name": "gudehus/searchexcerpt",
"description": "Finds a given searchterm in a text and higlights it.",
"type": "craft-plugin",
"version": "1.2",
"keywords": [
"craft",
"cms",
"craftcms",
"craft-plugin",
"searchexcerpt"
],
"support": {
"docs": "https://github.com/gudehus/searchexcerpt/blob/master/README.md",
"issues": "https://github.com/gudehus/searchexcerpt/issues"
},
"license": "MIT",
"authors": [
{
"name": "Jörg Gudehus",
"homepage": "http://joerggudehus.de"
}
],
"require": {
"craftcms/cms": "^3.0.0-RC1"
},
"autoload": {
"psr-4": {
"gudehus\\searchexcerpt\\": "src/"
}
},
"extra": {
"name": "SearchExcerpt",
"handle": "searchexcerpt",
"hasCpSettings": false,
"hasCpSection": false,
"changelogUrl": "https://raw.githubusercontent.com/gudehus/searchexcerpt/master/CHANGELOG.md",
"class": "gudehus\\searchexcerpt\\SearchExcerpt"
}
}
Binary file added resources/img/plugin-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions src/SearchExcerpt.php
@@ -0,0 +1,83 @@
<?php
/**
* SearchExcerpt plugin for Craft CMS 3.x
*
* Finds a given searchterm in a text and higlights it.
*
* @link http://joerggudehus.de
* @copyright Copyright (c) 2018 Jörg Gudehus
*/

namespace gudehus\searchexcerpt;

use gudehus\searchexcerpt\twigextensions\SearchExcerptTwigExtension;

use Craft;
use craft\base\Plugin;

/**
* Craft plugins are very much like little applications in and of themselves. We’ve made
* it as simple as we can, but the training wheels are off. A little prior knowledge is
* going to be required to write a plugin.
*
* For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL,
* as well as some semi-advanced concepts like object-oriented programming and PHP namespaces.
*
* https://craftcms.com/docs/plugins/introduction
*
* @author Jörg Gudehus
* @package SearchExcerpt
* @since 1.2
*
*/
class SearchExcerpt extends Plugin
{
// Static Properties
// =========================================================================

/**
* Static property that is an instance of this plugin class so that it can be accessed via
* SearchExcerpt::$plugin
*
* @var SearchExcerpt
*/
public static $plugin;

// Public Properties
// =========================================================================

/**
* To execute your plugin’s migrations, you’ll need to increase its schema version.
*
* @var string
*/
public $schemaVersion = '1.2';

// Public Methods
// =========================================================================

/**
* Set our $plugin static property to this class so that it can be accessed via
* SearchExcerpt::$plugin
*
* Called after the plugin class is instantiated; do any one-time initialization
* here such as hooks and events.
*
* If you have a '/vendor/autoload.php' file, it will be loaded for you automatically;
* you do not need to load it in your init() method.
*
*/

public function init()
{
parent::init();
self::$plugin = $this;
// Add in our Twig extension
Craft::$app->view->registerTwigExtension(new SearchExcerptTwigExtension());

}

// Protected Methods
// =========================================================================

}
16 changes: 16 additions & 0 deletions src/icon-mask.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icon.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions src/translations/de/search-excerpt.php
@@ -0,0 +1,25 @@
<?php
/**
* SearchExcerpt plugin for Craft CMS 3.x
*
* Finds a given searchterm in a text and higlights it.
*
* @link http://joerggudehus.de
* @copyright Copyright (c) 2018 Jörg Gudehus
*/

/**
* SearchExcerpt en Translation
*
* Returns an array with the string to be translated (as passed to `Craft::t('search-excerpt', '...')`) as
* the key, and the translation as the value.
*
* http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html
*
* @author Jörg Gudehus
* @package SearchExcerpt
* @since 1.2
*/
return [
'SearchExcerpt plugin loaded' => 'SearchExcerpt plugin geladen',
];
25 changes: 25 additions & 0 deletions src/translations/en/search-excerpt.php
@@ -0,0 +1,25 @@
<?php
/**
* SearchExcerpt plugin for Craft CMS 3.x
*
* Finds a given searchterm in a text and higlights it.
*
* @link http://joerggudehus.de
* @copyright Copyright (c) 2018 Jörg Gudehus
*/

/**
* SearchExcerpt en Translation
*
* Returns an array with the string to be translated (as passed to `Craft::t('search-excerpt', '...')`) as
* the key, and the translation as the value.
*
* http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html
*
* @author Jörg Gudehus
* @package SearchExcerpt
* @since 1.2
*/
return [
'SearchExcerpt plugin loaded' => 'SearchExcerpt plugin loaded',
];
50 changes: 50 additions & 0 deletions src/twigextensions/SearchExcerptTwigExtension.php
@@ -0,0 +1,50 @@
<?php

namespace gudehus\searchexcerpt\twigextensions;

use Craft;
use craft\helpers\Template;

class SearchExcerptTwigExtension extends \Twig_Extension
{
/**
* @return string
*/
public function getName()
{
return 'Search Excerpt';
}

/**
* Makes the filters available to the template context
*
* @return array
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('searchexcerpt', [$this, 'searchexcerpt'])
];
}

public function searchexcerpt($text, $term, $padding=20, $class='highlight')
{
$start = max(0, stripos($text, $term) - $padding);
$end = min(strlen($text), $start + strlen($term) + $padding);

$excerpt = substr($text, $start, $padding * 2 + strlen($term));

if ($start != 0)
$excerpt = '… ' . preg_replace('/^\S*\s/', '', $excerpt);

if ($end != strlen($text))
$excerpt = preg_replace('/\s\S*$/', '', $excerpt) . ' …';


$excerpt = preg_replace("/\b(".$term.")\b/i", "<span class='" . $class . "'>$1</span>", $excerpt);

return Template::raw($excerpt);
}


}

0 comments on commit a3a22dd

Please sign in to comment.