Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sommerregen committed May 14, 2015
2 parents 2c15e16 + aea22cb commit 1745fba
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 28 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# v1.1.0
## 05/14/2015

2. [](#improved)
* Improved `anchorlinks``generation
* Truncate headings to a maximum width of 32 chars in TOC and MINITOC
* Corrected spelling and markup in [README.md](https://github.com/Sommerregen/grav-plugin-toc/blob/master/README.md)

# v1.0.0
## 05/10/2015

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## About

`Toc` is a plugin for [**Grav**](http://getgrav.org) used to generate Table of Contents from a Markdown document based on special markers. The markers are `[TOC]` and `[MINITOC]`, where the latter can be used for a (minified) Table of Contents (to give an overview of the current content of the section). By default, the Table of Contents links to the contents (`anchorlinks`) and adds visible permanent links (`permalinks`) to all headers. Further, all headers will automatically have unique id attributes generated based upon the text of the header. See how it looks like:
`Toc` is a plugin for [**Grav**](http://getgrav.org) used to generate Table of Contents from a Markdown document based on special markers. The markers are `[TOC]` and `[MINITOC]`, where the latter can be used for a (minified) Table of Contents ( to give an overview of the current content of the section. By default, the Table of Contents links to the contents (`anchorlinks`) and adds visible permanent links (`permalinks`) to all headers. Further, all headers will automatically have unique id attributes generated based upon the text of the header. See how it looks like:

![Screenshot Toc Plugin](assets/screenshot.png "Toc Preview")

Expand All @@ -22,7 +22,7 @@ The `Toc` plugin comes with some sensible default configuration, that are pretty

### Config Defaults

```
```yaml
# Global plugin configurations

enabled: true # Set to false to disable this plugin completely
Expand All @@ -42,14 +42,14 @@ If you need to change any value, then the best process is to copy the [toc.yaml]

If you want to alter the settings for one or a few pages only, you can do so by adding page specific configurations into your page headers, e.g.

```
```yaml
toc:
permalink: false
```

to disable permalinks or

```
```yaml
toc: false
```

Expand Down
1 change: 1 addition & 0 deletions assets/css/toc.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
box-shadow: 0 0 5px 0 rgba(50, 50, 50, 0.4);
padding: 1em 2em;
min-width: 25%;
max-width: 25%;
}
.table-of-contents.toc {
float: right;
Expand Down
4 changes: 2 additions & 2 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: "Toc"
version: 1.0.0
version: 1.1.0
description: "This plugin automagically generates a (minified) Table of Contents based on special markers in the document and adds it into the resulting HTML document."
icon: connectdevelop
icon: language
author:
name: Sommerregen
email: [email protected]
Expand Down
95 changes: 78 additions & 17 deletions classes/Toc.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function($match) use ($replacements) {
* @param array $list A list with headings
* @return array A flattened tree of the $list.
*/
protected function mapTree($list)
protected function mapTree(array $list)
{
static $indent = -1;

Expand Down Expand Up @@ -341,40 +341,101 @@ protected function parseAttributes($text)
*
* @return string The converted attributes
*/
protected function htmlAttributes($attributes = [])
protected function htmlAttributes(array $attributes = [])
{
foreach ($attributes as $attribute => &$data) {
$data = implode(' ', (array) $data);
$data = $attribute.'="'.htmlspecialchars($data, ENT_QUOTES, 'UTF-8').'"';
}
return $attributes ? ' '. implode(' ', $attributes) : '';
return $attributes ? ' '.implode(' ', $attributes) : '';
}

/**
* Converts a word "into-it-s-hyphenated-version" (UTF-8 safe).
*
* A hyphenated word must begin with a letter ([A-Za-z]) and may be
* followed by any number of letters, digits ([0-9]), hyphens ("-"),
* underscores ("_"), colons (":"), and periods (".").
*
* @param string $word Word to hyphenate
* @return string The hyphenated word
*/
protected function hyphenize($word)
{
$string = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE',$word);
$string = htmlspecialchars_decode($string);
// Set locale for transliterating Unicode text to plain ASCII text
$locale = setlocale(LC_CTYPE, 0);
setlocale(LC_CTYPE, 'en_US.UTF8');

// Ensure word is UTF-8 encoded
$text = html_entity_decode($word, ENT_COMPAT, 'UTF-8');

// Replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
$text = preg_replace('~(\w)-s-~i', '$1s', $text);

// Character replacements
$text = preg_replace('~([A-Z]+)([A-Z][a-z])~', '\1-\2', $text);
$text = preg_replace('~([a-z]{2,})([A-Z])~', '\1-\2', $text);

// Trim
$text = trim($text, '-');

// Transliterate
$text = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $text);

// Character replacements
$string = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1-\2', $string);
$string = preg_replace('/([a-zd])([A-Z])/', '\1-\2', $string);
// Lowercase
$text = strtolower($text);

// REmove unwanted characters
$string = preg_replace('/(?:[\s_-]|\.)+/', '-', $string);
$string = preg_replace('/[^a-zA-Z0-9\-]/', '', $string);
// Remove unwanted characters and duplicate dashes
$text = preg_replace('~[^-\w]+~', '', $text);

// Remove duplicate dashes
$string = preg_replace('/(-)\1+/', '-', $string);
// Exception '&' (double-dash in github)
$string = preg_replace('/-&-/', '--', $string);
// Trim dashes from the beginning and end of string
$text = trim($text, '.-_ ');

// Trim dashes from the beginning and end of string
return strtolower(trim($string, '.-_ '));
// Truncate string (hard-coded configurations)
$text = $this->truncate($text, $limit = 32);

// Provide default
if (empty($text)) {
return 'n-a';
}

// Restore locale
setlocale(LC_CTYPE, $locale);

// Return hyphenated word
return $text;
}

/**
* Truncates a string to a maximum length at word boundaries.
*
* @param string $string The string which should be truncated.
* @param integer $limit The maximum length the string should have
* after truncating.
* @param string $break The break delimiter to divide the string
* into pieces of words.
* @param string $pad Added to the end of the truncated string.
*
* @return string The truncated string,
*/
protected function truncate($string, $limit = 32, $break = '-', $pad = '-...')
{
$charset = 'UTF-8';
if (mb_strlen($string, $charset) > $limit) {
if (false !== ($breakpoint = strpos($string, $break, $limit))) {
if ($breakpoint < mb_strlen($string, $charset) - 1) {
$string = mb_substr($string, 0, $breakpoint, $charset);
}
} else {
// Truncate string to a maximum length
$string = substr($string, 0, $limit);
}

// Add truncate marker to the end of the string
$string = preg_replace('~(\w)[^\p{L}]?$~', "$1$pad", $string);
}

return $string;
}
}
4 changes: 2 additions & 2 deletions templates/partials/toc.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@

{# Show TOC link based on anchorlinks option #}
{% if toc.anchorlink %}
<li><a href="#{{ entry.id }}" class="toclink">{{ entry.text }}</a></li>
<li><a href="#{{ entry.id }}" class="toclink" title="{{ entry.text }}">{{ entry.text|truncate(32, " ") }}</a></li>
{% else %}
<li><span class="toclink">{{ entry.text }}</span></li>
<li><span class="toclink">{{ entry.text|truncate(32, " ") }}</span></li>
{% endif %}
{% endfor %}

Expand Down
6 changes: 3 additions & 3 deletions toc.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Toc v1.0.0
* Toc v1.1.0
*
* This plugin automagically generates a (minified) Table of Contents
* based on special markers in the document and adds it into the
Expand All @@ -9,7 +9,7 @@
* Licensed under MIT, see LICENSE.
*
* @package Toc
* @version 1.0.0
* @version 1.1.0
* @link <https://github.com/sommerregen/grav-plugin-external-links>
* @author Benjamin Regler <[email protected]>
* @copyright 2015, Benjamin Regler
Expand Down Expand Up @@ -73,7 +73,7 @@ public function onPluginsInitialized() {
return;
}

if ( $this->config->get('plugins.toc.enabled') ) {
if ($this->config->get('plugins.toc.enabled')) {
// Initialize Toc class
require_once(__DIR__.'/classes/Toc.php');
$this->toc = new Toc();
Expand Down

0 comments on commit 1745fba

Please sign in to comment.