fabianmichael / kirby-typography

Typographic enhancements for your Kirby-driven website.
GNU General Public License v3.0
78 stars 4 forks source link

Providing a hyphenate field method #7

Open fvsch opened 8 years ago

fvsch commented 8 years ago

Would this plugin be able provide a $field->hyphenate($lang) method, without major refactoring?

As argued in #6, hyphenation is a design choice. And I'll add that a designer or client may want hyphenation for some texts (e.g. an article's body) but not for others (e.g. a big title).

What might serve this use case:

<article>
  <header>
    <h1><?php echo $page->title()->smartypants(); ?></h1>
  </header>
  <div class="article-body">
    <?php echo $page->text()->kirbytext()->hyphenate(); ?>
  </div>
</article>
fabianmichael commented 8 years ago

Hi again :-)

This is really one of the features, which is harder to implement, because PHP-Typography is meant to be given a string, where features are applied to. The only possibility to do this, would be to create a new instance of PHP-Typography and then apply everything to it. It is also possible to clone the current instance of the class and change all settings. With the current codebase, this is very hard to realize.

But it should be possible to use both Smartypants an Kirby-Typography in the same template without too many changes. It also seems to be possible to override built-in field methods. With that in mind, it would be possible to override the default settings for every call to field->kirbytext() with an additional parameter or provide methods like unhyphenate(), where a simple str_replace() coudl remove all &shy; characters from the text. Custom options for Kirbytext could be a really interesting way how to do it. I could also think of providing a field->typography() method for custom configuration instead. I will think about that, but to 1.0.0, I would prefer to get rid of current bugs and leave all new features and APIs out of the initial release. Added it to the 1.1 milestone.

echo $page->text()->kirbytext([
  'typography.hyphenation' => false,
  'typography.hyphenation.minlength' => 10,
]);

or:

echo $page->text()->kirbytext()->typography([
  'hyphenation' => false,
  'hyphenation.minlength' => 10,
]);
mundschenk-at commented 8 years ago

You can set PHP_Typography->settings['hyphenateTitle'] to false and call PHP_Typography::process with $isTitle set to true to achieve that effect.