Skip to content

Commit 1a1825a

Browse files
committed
Add allow_composite_extensions option to handle .x.y extensions
1 parent 87176f5 commit 1a1825a

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/Phug/Compiler.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,26 @@ class Compiler implements ModuleContainerInterface, CompilerInterface
138138
public function __construct($options = null)
139139
{
140140
$this->setOptionsDefaults($options ?: [], [
141-
'paths' => [],
142-
'debug' => false,
143-
'default_tag' => 'div',
144-
'default_doctype' => 'html',
145-
'extensions' => ['', '.pug', '.jade'],
146-
'get_file_contents' => 'file_get_contents',
147-
'on_compile' => null,
148-
'on_output' => null,
149-
'on_node' => null,
150-
'on_element' => null,
151-
'filename' => null,
152-
'filters' => [],
153-
'filter_resolvers' => [],
154-
'includes' => [],
155-
'parser_class_name' => Parser::class,
156-
'formatter_class_name' => Formatter::class,
157-
'locator_class_name' => FileLocator::class,
158-
'compiler_modules' => [],
159-
'node_compilers' => [
141+
'paths' => [],
142+
'debug' => false,
143+
'default_tag' => 'div',
144+
'default_doctype' => 'html',
145+
'extensions' => ['', '.pug', '.jade'],
146+
'get_file_contents' => 'file_get_contents',
147+
'on_compile' => null,
148+
'on_output' => null,
149+
'on_node' => null,
150+
'on_element' => null,
151+
'filename' => null,
152+
'filters' => [],
153+
'filter_resolvers' => [],
154+
'includes' => [],
155+
'parser_class_name' => Parser::class,
156+
'formatter_class_name' => Formatter::class,
157+
'locator_class_name' => FileLocator::class,
158+
'compiler_modules' => [],
159+
'allow_composite_extensions' => false,
160+
'node_compilers' => [
160161
AssignmentListNode::class => AssignmentListNodeCompiler::class,
161162
AssignmentNode::class => AssignmentNodeCompiler::class,
162163
AttributeListNode::class => AttributeListNodeCompiler::class,

src/Phug/Compiler/NodeCompiler/ImportNodeCompiler.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,29 @@
2020

2121
class ImportNodeCompiler extends AbstractNodeCompiler
2222
{
23+
protected function isPugImport($path)
24+
{
25+
$compiler = $this->getCompiler();
26+
$extension = pathinfo($path, PATHINFO_EXTENSION) ?: '';
27+
$extensions = $compiler->getOption('extensions');
28+
29+
if ($extension === '') {
30+
return in_array('', $extensions);
31+
}
32+
33+
if (!$compiler->getOption('allow_composite_extensions')) {
34+
return in_array(".$extension", $extensions, true);
35+
}
36+
37+
foreach ($extensions as $endPattern) {
38+
if (substr($path, -strlen($endPattern)) === $endPattern) {
39+
return true;
40+
}
41+
}
42+
43+
return false;
44+
}
45+
2346
/**
2447
* @param NodeInterface $node
2548
* @param ElementInterface $parent
@@ -67,10 +90,7 @@ public function compileNode(NodeInterface $node, ElementInterface $parent = null
6790
return $element;
6891
}
6992

70-
$extension = pathinfo($path, PATHINFO_EXTENSION) ?: '';
71-
$extensions = $compiler->getOption('extensions');
72-
73-
if (!in_array($extension === '' ? '' : ".$extension", $extensions, true)) {
93+
if (!$this->isPugImport($path)) {
7494
return new TextElement($compiler->getFileContents($path), $node);
7595
}
7696

0 commit comments

Comments
 (0)