Skip to content

Commit

Permalink
Add custom overrides for SmartyPants' default quotes (e.g. for other …
Browse files Browse the repository at this point in the history
…language styles like „German“).
  • Loading branch information
flesser committed Feb 1, 2016
1 parent 359476a commit c18c9ad
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ The `smartypants` plugin comes with some sensible default configuration, that ar

The options are described in detail on the [PHP SmartyPants GitHub Site](https://github.com/michelf/php-smartypants): https://github.com/michelf/php-smartypants

You can override SmartyPants' default quotes (e.g. for other language styles like „German“):

dq_open: “ // Custom HTML string for double quote open
dq_close: ” // Custom HTML string for double quote close
sq_open: ‘ // Custom HTML string for single quote open
sq_close: ’ // Custom HTML string for single quote close

To customize the plugin, you first need to create an override config. To do so, create the folder `user/config/plugins` (if it doesn't exist already) and copy the [smartypants.yaml](smartypants.yaml) config file in there and then make your edits.

Also you can override the default options per-page:
Expand Down
33 changes: 33 additions & 0 deletions blueprints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,36 @@ form:
size: large
placeholder: "SmartyPants Options"
help: See the docs for documentation: https://github.com/michelf/php-smartypants

overrides:
type: section
title: Overrides
underline: 1
fields:
dq_open:
type: text
label: Double quote open
size: small
placeholder: "“"
help: Custom HTML string for double quote open

dq_close:
type: text
label: Double quote close
size: small
placeholder: "”"
help: Custom HTML string for double quote close

sq_open:
type: text
label: Single quote open
size: small
placeholder: "‘"
help: Custom HTML string for single quote open

sq_close:
type: text
label: Single quote close / apostrophe
size: small
placeholder: "’"
help: Custom HTML string for single quote close and apostrophe
25 changes: 21 additions & 4 deletions smartypants.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function onPluginsInitialized()
}

require_once(__DIR__.'/vendor/Michelf/SmartyPants.php');
require_once(__DIR__.'/vendor/Michelf/SmartyPantsTypographer.php');
$this->enable([
'onTwigExtensions' => ['onTwigExtensions', 0],
'onPageProcessed' => ['onPageProcessed', 0],
Expand Down Expand Up @@ -64,9 +65,9 @@ public function onPageProcessed(Event $event)
$config = $this->mergeConfig($page);

if ($config->get('process_title')) {
$page->title(\Michelf\SmartyPants::defaultTransform(
$page->title($this->transform(
$page->title(),
$config->get('options')
$config
));
}
}
Expand All @@ -80,9 +81,9 @@ public function onPageContentProcessed(Event $event)
$config = $this->mergeConfig($page);

if ($config->get('process_content')) {
$page->setRawContent(\Michelf\SmartyPants::defaultTransform(
$page->setRawContent($this->transform(
$page->getRawContent(),
$config->get('options')
$config
));
}
}
Expand All @@ -106,4 +107,20 @@ public function onBlueprintCreated(Event $event)
$inEvent = false;
}
}

/**
* Apply SmartyPants transformation on raw text.
*
* @param string $text raw text
* @return string transformed text
*/
protected function transform($text, $config)
{
$smartypants = new \Michelf\SmartyPantsTypographer($config->get('options'));
if ($config->get('dq_open')) $smartypants->smart_doublequote_open = $config->get('dq_open');
if ($config->get('dq_close')) $smartypants->smart_doublequote_close = $config->get('dq_close');
if ($config->get('sq_open')) $smartypants->smart_singlequote_open = $config->get('sq_open');
if ($config->get('sq_close')) $smartypants->smart_singlequote_close = $config->get('sq_close');
return $smartypants->transform($text);
}
}
14 changes: 12 additions & 2 deletions twig/SmartyPantsTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ public function smartypantsFilter($content, $options = null)
if (!$options) {
$options = $this->config->get('plugins.smartypants.options');
}

return \Michelf\SmartyPants::defaultTransform($content, $options);
$smartypants = new \Michelf\SmartyPantsTypographer($options);

if ($this->config->get('plugins.smartypants.dq_open'))
$smartypants->smart_doublequote_open = $this->config->get('plugins.smartypants.dq_open');
if ($this->config->get('plugins.smartypants.dq_close'))
$smartypants->smart_doublequote_close = $this->config->get('plugins.smartypants.dq_close');
if ($this->config->get('plugins.smartypants.sq_open'))
$smartypants->smart_singlequote_open = $this->config->get('plugins.smartypants.sq_open');
if ($this->config->get('plugins.smartypants.sq_close'))
$smartypants->smart_singlequote_close = $this->config->get('plugins.smartypants.sq_close');

return $smartypants->transform($content);
}

}

0 comments on commit c18c9ad

Please sign in to comment.