Skip to content

Commit

Permalink
Merge pull request #1 from wedevelopnl/feature/setup
Browse files Browse the repository at this point in the history
Initial setup
  • Loading branch information
Dennisprins93 authored Oct 10, 2023
2 parents ad5ff00 + 51c1edc commit 468c82f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
19 changes: 19 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Contributing

## Bugtracker
Bugs are tracked in the issues section of this repository. Before submitting an issue please read over
existing issues to ensure yours is unique.

If the issue does look like a new bug:

- Create a new issue
- Describe the steps required to reproduce your issue, and the expected outcome. Unit tests, screenshots
and screencasts can help here.
- Describe your environment as detailed as possible: SilverStripe version, Browser, PHP version,
Operating System, any installed SilverStripe modules.

## Security
Please report security issues to the module maintainers directly. Please don't file security issues in the bugtracker.

## Copyright
By supplying code to this module in any form you agree to assign copyright of that code to WeDevelop, on the condition that WeDevelop releases that code under a form of open source license.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# silverstripe-highlights
This module creates a `.Highlight` method on all SS DBStrings to provide the ability to highlight custom texts by placing it between shortcodes that are configurable by yourself.

## Requirements
* See `composer.json` requirement

## Installation
* `composer require wedevelopnl/silverstripe-highlights`

Next, you'll need to run a `dev/build` with a `flush=all` to allow this module to work properly.

## License
See [License](LICENSE)

## Maintainers
* [WeDevelop](https://www.wedevelop.nl/) <development@wedevelop.nl>

## Configuration
```yml
SilverStripe\ORM\FieldType\DBString:
shortcode_start: '['
shortcode_end: ']'
css_class_name: 'highlight'
```
## Example
### Input
```html
This is a [large] content string which outputs HTML around the word 'large'.
```

### Output
```html
This is a <span class="highlight">large</span> content string which outputs HTML around the word 'large'.
```

## Development and contribution
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
See read our [contributing](CONTRIBUTING.md) document for more information.
6 changes: 6 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
Name: wedevelop-highlights
---
SilverStripe\ORM\FieldType\DBString:
extensions:
- WeDevelop\Highlights\Extension\DBStringExtension
20 changes: 17 additions & 3 deletions src/Extension/DBStringExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,29 @@ class DBStringExtension extends Extension

private static string $shortcode_end = ']';

private static string $css_class_name = 'highlight';

private static array $casting = [
'Highlight' => 'Text',
];

public function Highlight(): HTMLValue
{
$regex = sprintf('/\%s(.+)%s/', self::getOwner()->config()->get('shortcode_start'), self::getOwner()->config()->get('shortcode_end'));
$value = preg_replace($regex, '<span class="is-highlighted">$1</span>', $this->getOwner()->getValue());
return HTMLValue::create(preg_replace($this->createRegex(), $this->createElement(), $this->getOwner()->getValue()));
}

private function createElement(): string
{
return sprintf('<span class="%s">$1</span>', $this->getConfig('css_class_name'));
}

return HTMLValue::create($value);
private function createRegex(): string
{
return sprintf('/\%s(.+)%s/', $this->getConfig('shortcode_start'), $this->getConfig('shortcode_end'));
}

private function getConfig(string $name): string
{
return $this->getOwner()->config()->get($name);
}
}

0 comments on commit 468c82f

Please sign in to comment.