Skip to content

Commit 2cbcd27

Browse files
Add support for passing options to pygments
1 parent 93aa735 commit 2cbcd27

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

src/PygmentsHighlighterRenderer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,21 @@ public function render(
6161
} else {
6262
$pygments = new Pygments( $path );
6363
}
64+
$options = [];
65+
if ( count( $infoWords ) > 1 ) {
66+
foreach ( array_slice( $infoWords, 1 ) as $opt ) {
67+
$parts = explode( '=', $opt );
68+
if ( count( $parts ) === 2 ) {
69+
$options[ $parts[0] ] = $parts[1];
70+
}
71+
}
72+
}
6473
try {
6574
$rendered = $pygments->highlight(
6675
$node->getLiteral(),
6776
$lang,
68-
'html'
77+
'html',
78+
$options
6979
);
7080
} catch ( PygmentizeProcessFailed $e ) {
7181
$onException = $this->config->get(

tests/data/extra-options.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h1>Testing</h1>
2+
<p>Before</p>
3+
<div class="pygments-highlighter"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">User</span> <span class="p">{</span>
4+
<span class="k">private</span> <span class="nx">int</span> <span class="nv">$id</span><span class="p">;</span>
5+
<span class="k">private</span> <span class="nx">string</span> <span class="nv">$name</span><span class="p">;</span>
6+
7+
<span class="k">public</span> <span class="k">function</span> <span class="fm">__construct</span><span class="p">(</span> <span class="nx">int</span> <span class="nv">$id</span><span class="p">,</span> <span class="nx">string</span> <span class="nv">$name</span> <span class="p">)</span> <span class="p">{</span>
8+
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">id</span> <span class="o">=</span> <span class="nv">$id</span><span class="p">;</span>
9+
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">name</span> <span class="o">=</span> <span class="nv">$name</span><span class="p">;</span>
10+
<span class="p">}</span>
11+
<span class="p">}</span>
12+
</pre></div>
13+
</div>
14+
<p>after</p>

tests/data/extra-options.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Testing
2+
3+
Before
4+
5+
```php startinline=True
6+
class User {
7+
private int $id;
8+
private string $name;
9+
10+
public function __construct( int $id, string $name ) {
11+
$this->id = $id;
12+
$this->name = $name;
13+
}
14+
}
15+
```
16+
17+
after

tests/data/extra-options.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document xmlns="http://commonmark.org/xml/1.0">
3+
<heading level="1">
4+
<text>Testing</text>
5+
</heading>
6+
<paragraph>
7+
<text>Before</text>
8+
</paragraph>
9+
<code_block info="php startinline=True">class User {
10+
private int $id;
11+
private string $name;
12+
13+
public function __construct( int $id, string $name ) {
14+
$this-&gt;id = $id;
15+
$this-&gt;name = $name;
16+
}
17+
}
18+
</code_block>
19+
<paragraph>
20+
<text>after</text>
21+
</paragraph>
22+
</document>

0 commit comments

Comments
 (0)