Skip to content

Commit

Permalink
Added PipeTables plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Nov 22, 2016
1 parent 06658f1 commit bda0c45
Show file tree
Hide file tree
Showing 16 changed files with 1,387 additions and 7 deletions.
31 changes: 31 additions & 0 deletions docs/Plugins/PipeTables/Synopsis.md
@@ -0,0 +1,31 @@
This plugin implements a type of ASCII-style tables inspired by GitHub-flavored Markdown, Pandoc's pipe tables and PHP Markdown Extra's simple tables.

See its [Syntax](Syntax.md).

### References

* [GitHub-flavored Markdown](https://help.github.com/articles/organizing-information-with-tables/)
* [Pandoc's pipe_tables extension](http://pandoc.org/MANUAL.html#extension-pipe_tables)
* [PHP Markdown Extra](https://michelf.ca/projects/php-markdown/extra/#table)

## Example

```php
$configurator = new s9e\TextFormatter\Configurator;
$configurator->plugins->load('PipeTables');

// Get an instance of the parser and the renderer
extract($configurator->finalize());

$text = 'a | b' . "\n"
. '--|--' . "\n"
. 'c | d';
$xml = $parser->parse($text);
$html = $renderer->render($xml);

echo $html;
```
```html
<table><thead><tr><th>a</th><th>b</th></tr></thead>
<tbody><tr><td>c</td><td>d</td></tr></tbody></table>
```
25 changes: 25 additions & 0 deletions docs/Plugins/PipeTables/Syntax.md
@@ -0,0 +1,25 @@
### Simple table

```
| Header 1 | Header 2 |
|----------|----------|
| Cell 1 | Cell 2 |
```

### Compact table

The outer pipes and spaces around pipes are optional.

```
Header 1|Header 2
-|-
Cell 1|Cell 2
```

### Text alignment

```
| Left | Center | Right |
|:-----|:------:|------:|
| x | x | x |
```
11 changes: 10 additions & 1 deletion docs/testdox.txt
Expand Up @@ -2301,7 +2301,7 @@ s9e\TextFormatter\Tests\Plugins\Emoticons\Parser
[x] Parsing+rendering tests

s9e\TextFormatter\Tests\Plugins\Escaper\Configurator
[x] By default only escapes the characters !#()*+-.:@[\]^_`{}
[x] By default only escapes the characters !#()*+-.:@[\]^_`{|}
[x] $plugin->escapeAll() makes it escape any Unicode character
[x] $plugin->escapeAll(false) reverts to default escape list
[x] Automatically creates an "ESC" tag
Expand Down Expand Up @@ -2545,6 +2545,15 @@ s9e\TextFormatter\Tests\Plugins\MediaEmbed\Parser
[x] Parsing tests (JavaScript)
[x] Parsing+rendering tests

s9e\TextFormatter\Tests\Plugins\PipeTables\Configurator
[x] Creates TABLE, TBODY, TD, TH, THEAD and TR tags
[x] Does not attempt to create a tag if it already exists

s9e\TextFormatter\Tests\Plugins\PipeTables\Parser
[x] Parsing tests
[x] Parsing tests (JavaScript)
[x] Parsing+rendering tests

s9e\TextFormatter\Tests\Plugins\Preg\Configurator
[x] replace() generates a tag name automatically
[x] The name of the tag can be specified
Expand Down
3 changes: 3 additions & 0 deletions mkdocs.yml
Expand Up @@ -92,6 +92,9 @@ pages:
- Add a link to the original URL: Plugins/MediaEmbed/Append_template.md
- Change a template: Plugins/MediaEmbed/Change_template.md
- Configure a cache dir: Plugins/MediaEmbed/Performance_cache_dir.md
- PipeTables:
- Synopsis: Plugins/PipeTables/Synopsis.md
- Syntax: Plugins/PipeTables/Syntax.md
- Preg:
- Synopsis: Plugins/Preg/Synopsis.md
- Your own plugin:
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml
Expand Up @@ -247,6 +247,8 @@
<file>tests/Plugins/MediaEmbed/Configurator/TemplateGenerators/IframeTest.php</file>
<file>tests/Plugins/MediaEmbed/ConfiguratorTest.php</file>
<file>tests/Plugins/MediaEmbed/ParserTest.php</file>
<file>tests/Plugins/PipeTables/ConfiguratorTest.php</file>
<file>tests/Plugins/PipeTables/ParserTest.php</file>
<file>tests/Plugins/Preg/ConfiguratorTest.php</file>
<file>tests/Plugins/Preg/ParserTest.php</file>

Expand Down
2 changes: 1 addition & 1 deletion src/Bundles/Fatdown.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Plugins/Escaper/Configurator.php
Expand Up @@ -34,7 +34,7 @@ class Configurator extends ConfiguratorBase
*/
public function escapeAll($bool = true)
{
$this->regexp = ($bool) ? '/\\\\./su' : '/\\\\[-!#()*+.:<>@[\\\\\\]^_`{}]/';
$this->regexp = ($bool) ? '/\\\\./su' : '/\\\\[-!#()*+.:<>@[\\\\\\]^_`{|}]/';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Litedown/Parser.js
Expand Up @@ -569,7 +569,7 @@ function matchBlockLevelMarkup()
if (!codeTag)
{
// Create code block
codeTag = addStartTag('CODE', matchPos + ignoreLen, 0);
codeTag = addStartTag('CODE', matchPos + ignoreLen, 0, -999);
}

// Clear the captures to prevent any further processing
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Litedown/Parser.php
Expand Up @@ -589,7 +589,7 @@ protected function matchBlockLevelMarkup()
if (!isset($codeTag))
{
// Create code block
$codeTag = $this->parser->addStartTag('CODE', $matchPos + $ignoreLen, 0);
$codeTag = $this->parser->addStartTag('CODE', $matchPos + $ignoreLen, 0, -999);
}

// Clear the captures to prevent any further processing
Expand Down
82 changes: 82 additions & 0 deletions src/Plugins/PipeTables/Configurator.php
@@ -0,0 +1,82 @@
<?php

/**
* @package s9e\TextFormatter
* @copyright Copyright (c) 2010-2016 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\TextFormatter\Plugins\PipeTables;

use s9e\TextFormatter\Configurator\Items\AttributeFilters\ChoiceFilter;
use s9e\TextFormatter\Plugins\ConfiguratorBase;

class Configurator extends ConfiguratorBase
{
/**
* {@inheritdoc}
*/
protected $quickMatch = '|';

/**
* Create the tags used by this plugin
*
* @return void
*/
protected function setUp()
{
$tags = [
'TABLE' => ['template' => '<table><xsl:apply-templates/></table>'],
'TBODY' => ['template' => '<tbody><xsl:apply-templates/></tbody>'],
'TD' => $this->generateCellTagConfig('td'),
'TH' => $this->generateCellTagConfig('th'),
'THEAD' => ['template' => '<thead><xsl:apply-templates/></thead>'],
'TR' => ['template' => '<tr><xsl:apply-templates/></tr>']
];
foreach ($tags as $tagName => $tagConfig)
{
if (!isset($this->configurator->tags[$tagName]))
{
$this->configurator->tags->add($tagName, $tagConfig);
}
}
}

/**
* Generate the tag config for give cell element
*
* @param string $elName Element's name, either "td" or "th"
* @return array Tag config
*/
protected function generateCellTagConfig($elName)
{
$alignFilter = new ChoiceFilter(['left', 'center', 'right', 'justify'], true);

return [
'attributes' => [
'align' => [
'filterChain' => ['strtolower', $alignFilter],
'required' => false
]
],
'rules' => ['createParagraphs' => false],
'template' =>
'<' . $elName . '>
<xsl:if test="@align">
<xsl:attribute name="style">text-align:<xsl:value-of select="@align"/></xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</' . $elName . '>'
];
}

/**
* {@inheritdoc}
*/
public function asConfig()
{
return [
'overwriteEscapes' => isset($this->configurator->Escaper),
'overwriteMarkdown' => isset($this->configurator->Litedown)
];
}
}

0 comments on commit bda0c45

Please sign in to comment.