|
1 | | -# laravel-string-typography |
2 | | -A set of simple Str & Stringable macros helping to implement typographic rules |
| 1 | +# Typography Rules for Laravel |
| 2 | + |
| 3 | +Rendering strings provided by users can result in unexpected typographic results. For instance, in most latin languages (such as french) it is recommended to add a non-breakable space in front of, amongst others, exclamation or question marks (`!` and `?`). Most users will probably just type a regular space, which could result in an unwanted line break just before these punctuation marks. |
| 4 | + |
| 5 | +This simple package provides a `typography` macro to Laravel's `Str` facade and `Stringable` instances (created using `Str::of()` or `str()`) that will take care of these typographic details. |
| 6 | + |
| 7 | +It is also possible to enhance the package by adding your own typographic rules. |
| 8 | + |
| 9 | +```php |
| 10 | +$content = 'Mama mia !'; |
| 11 | + |
| 12 | +echo str($content)->typography(); |
| 13 | +``` |
| 14 | +``` |
| 15 | +Mama mia ! |
| 16 | +``` |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +composer require whitecube/laravel-string-typography |
| 22 | +``` |
| 23 | + |
| 24 | +## Getting started |
| 25 | + |
| 26 | +The package's ServiceProvider and therefore its `typography` macro will automatically be registered upon installation so you can start using it right away: |
| 27 | + |
| 28 | +```blade |
| 29 | +<!-- Using the Str facade --> |
| 30 | +<h1>{!! Str::typography($title) !!}</h1> |
| 31 | +<!-- Using the str() helper method --> |
| 32 | +<p>{!! str($paragraph)->typography() !!}</p> |
| 33 | +``` |
| 34 | + |
| 35 | +Using `Stringable` instances, you can chain the `typography` method with other helpers: |
| 36 | + |
| 37 | +```blade |
| 38 | +<div>{!! str($text)->markdown()->typography() !!}</div> |
| 39 | +``` |
| 40 | + |
| 41 | +## Default typographic rules |
| 42 | + |
| 43 | +| Key | Usage | Description | |
| 44 | +| ------------------------- | --------------------------------------------------- | --------------------------------------------------------------------------------------------- | |
| 45 | +| `unbreakable-punctuation` | Remove unwanted line breaks in front of punctuation | Replaces ` !`, ` ?`, ` :`, ` ;` with ` !`, ` ?`, ` :`, ` ;` respectively. | |
| 46 | +| `hellip` | Use the correct "horizontal ellipsis" HTML entity | Replaces `…`, `…`, `...`, `…` with `…`. | |
| 47 | + |
| 48 | +## Registering & removing typographic rules |
| 49 | + |
| 50 | +```php |
| 51 | +use Whitecube\Strings\Typography; |
| 52 | + |
| 53 | +Typography::rule( |
| 54 | + key: 'a-to-b', |
| 55 | + regex: '/a/', |
| 56 | + callback: fn(array $matches) => 'b', |
| 57 | +); |
| 58 | +``` |
| 59 | + |
| 60 | +By default all the previously registered typographic rules will be applied when using the new `typography` method. It is possible to globally remove one of them using: |
| 61 | + |
| 62 | +```php |
| 63 | +use Whitecube\Strings\Typography; |
| 64 | + |
| 65 | +Typography::remove('a-to-b'); |
| 66 | +``` |
| 67 | + |
| 68 | +## Filtering typographic rules |
| 69 | + |
| 70 | +Call a single or a few specific rules: |
| 71 | + |
| 72 | +```blade |
| 73 | +<div>{!! str($text)->typography(only: 'hellip') !!}</div> |
| 74 | +<div>{!! str($text)->typography(only: ['hellip', 'unbreakable-punctuation']) !!}</div> |
| 75 | +``` |
| 76 | + |
| 77 | +Call all rules except a single or a few specific unwanted rules: |
| 78 | + |
| 79 | +```blade |
| 80 | +<div>{!! str($text)->typography(except: 'hellip') !!}</div> |
| 81 | +<div>{!! str($text)->typography(except: ['hellip', 'unbreakable-punctuation']) !!}</div> |
| 82 | +``` |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## 🔥 Sponsorships |
| 87 | + |
| 88 | +If you are reliant on this package in your production applications, consider [sponsoring us](https://github.com/sponsors/whitecube)! It is the best way to help us keep doing what we love to do: making great open source software. |
| 89 | + |
| 90 | +## Contributing |
| 91 | + |
| 92 | +Feel free to suggest changes, ask for new features or fix bugs yourself. We're sure there are still a lot of improvements that could be made, and we would be very happy to merge useful pull requests. Thanks! |
| 93 | + |
| 94 | +## Made with ❤️ for open source |
| 95 | + |
| 96 | +At [Whitecube](https://www.whitecube.be) we use a lot of open source software as part of our daily work. |
| 97 | +So when we have an opportunity to give something back, we're super excited! |
| 98 | + |
| 99 | +We hope you will enjoy this small contribution from us and would love to [hear from you ](mailto:[email protected]) if you find it useful in your projects. Follow us on [Twitter ](https://twitter.com/whitecube_be) for more updates! |
0 commit comments