Skip to content

Commit dd83568

Browse files
authored
Allow 'parseArgs' and 'numOptArgs' in environments (#14)
Resolves #13
1 parent f4d3604 commit dd83568

File tree

4 files changed

+58
-33
lines changed

4 files changed

+58
-33
lines changed

library/PhpLatex/Parser.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ protected function _createEnviron($name, $mode, $environ = null) // {{{
318318
assert(($mode & ($mode - 1)) === 0); // mode must be a power of 2
319319

320320
$math = false;
321+
$optArgs = array();
321322
$args = array();
322323

323324
if (isset($this->_environs[$name])) {
@@ -342,12 +343,23 @@ protected function _createEnviron($name, $mode, $environ = null) // {{{
342343
// or displaymath), if so, prepare math node instead of environ node
343344
$math = isset($spec['math']) && $spec['math'];
344345

346+
$parseArgs = isset($spec['parseArgs']) ? $spec['parseArgs'] : true;
347+
348+
$numOptArgs = isset($spec['numOptArgs']) ? intval($spec['numOptArgs']) : 0;
349+
while (count($optArgs) < $numOptArgs) {
350+
if (false !== ($arg = $this->_parseOptArg($mode, $environ, $parseArgs))) {
351+
$optArgs[] = $arg;
352+
} else {
353+
break;
354+
}
355+
}
356+
345357
// parse args, will be placed as environs first children, with
346358
// no spaces between them, btw: \begin{tabular}c is a perfectly
347359
// correct specification for a single-column table.
348360
$nargs = isset($spec['numArgs']) ? intval($spec['numArgs']) : 0;
349361
while (count($args) < $nargs) {
350-
if (false === ($arg = $this->_parseArg($mode, $environ))) {
362+
if (false === ($arg = $this->_parseArg($mode, $environ, $parseArgs))) {
351363
$arg = $this->_createNode(self::TYPE_GROUP, $mode);
352364
}
353365
$arg->setProp('arg', true);
@@ -364,6 +376,10 @@ protected function _createEnviron($name, $mode, $environ = null) // {{{
364376
$node->math = $math;
365377
}
366378

379+
foreach ($optArgs as $arg) {
380+
$node->appendChild($arg);
381+
}
382+
367383
foreach ($args as $arg) {
368384
$node->appendChild($arg);
369385
}

library/PhpLatex/Renderer/Abstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static function toLatex($node) // {{{
9797
$argsEnd = 0;
9898

9999
foreach ($children as $child) {
100-
if ($child->arg) {
100+
if ($child->arg || $child->optional) {
101101
++$argsEnd;
102102
} else {
103103
break;

library/PhpLatex/environs.php

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,65 @@
22

33
return array(
44
'verbatim' => array(
5-
'verbatim' => true,
6-
'mode' => PhpLatex_Parser::MODE_TEXT,
7-
'environs' => array('itemize', 'enumerate'),
8-
'starred' => true,
5+
'verbatim' => true,
6+
'mode' => PhpLatex_Parser::MODE_TEXT,
7+
'environs' => array('itemize', 'enumerate'),
8+
'starred' => true,
99
// verbatim in tabular causes
1010
// ! LaTeX Error: Something's wrong--perhaps a missing \item.
1111
),
1212
'Verbatim' => array(
13-
'verbatim' => true,
14-
'mode' => PhpLatex_Parser::MODE_TEXT,
15-
'environs' => array('itemize', 'enumerate'),
13+
'verbatim' => true,
14+
'mode' => PhpLatex_Parser::MODE_TEXT,
15+
'environs' => array('itemize', 'enumerate'),
1616
),
1717
'lstlisting' => array(
18-
'verbatim' => true,
19-
'mode' => PhpLatex_Parser::MODE_TEXT,
20-
'environs' => array('itemize', 'enumerate'),
18+
'numOptArgs' => 1,
19+
'verbatim' => true,
20+
'mode' => PhpLatex_Parser::MODE_TEXT,
21+
'environs' => array('itemize', 'enumerate'),
2122
),
2223
'enumerate' => array(
23-
'mode' => PhpLatex_Parser::MODE_TEXT,
24-
'environs' => array('itemize', 'enumerate'),
24+
'mode' => PhpLatex_Parser::MODE_TEXT,
25+
'environs' => array('itemize', 'enumerate'),
2526
),
2627
'itemize' => array(
27-
'mode' => PhpLatex_Parser::MODE_TEXT,
28-
'environs' => array('itemize', 'enumerate'),
28+
'numOptArgs' => 1,
29+
'mode' => PhpLatex_Parser::MODE_TEXT,
30+
'environs' => array('itemize', 'enumerate'),
2931
// itemize in tabular causes
3032
// ! LaTeX Error: Something's wrong--perhaps a missing \item.
3133
),
3234
'displaymath' => array(
33-
'math' => true,
34-
'mode' => PhpLatex_Parser::MODE_TEXT,
35-
'environs' => array('itemize', 'enumerate'),
35+
'math' => true,
36+
'mode' => PhpLatex_Parser::MODE_TEXT,
37+
'environs' => array('itemize', 'enumerate'),
3638
// displaymath in tabular causes
3739
// ! LaTeX Error: Bad math environment delimiter.
3840
),
3941
'math' => array(
40-
'math' => true,
41-
'mode' => PhpLatex_Parser::MODE_TEXT,
42-
'environs' => array('itemize', 'enumerate', 'tabular'),
42+
'math' => true,
43+
'mode' => PhpLatex_Parser::MODE_TEXT,
44+
'environs' => array('itemize', 'enumerate', 'tabular'),
4345
),
4446
'equation' => array(
45-
'mode' => PhpLatex_Parser::MODE_TEXT,
46-
'math' => true,
47-
'starred' => true,
47+
'mode' => PhpLatex_Parser::MODE_TEXT,
48+
'math' => true,
49+
'starred' => true,
4850
),
4951
'eqnarray' => array(
50-
'mode' => PhpLatex_Parser::MODE_TEXT,
51-
'math' => true,
52-
'starred' => true,
52+
'mode' => PhpLatex_Parser::MODE_TEXT,
53+
'math' => true,
54+
'starred' => true,
5355
),
5456
'tabular' => array(
55-
'numArgs' => 1,
56-
'mode' => PhpLatex_Parser::MODE_TEXT,
57-
'environs' => array('itemize', 'enumerate', 'tabular'),
57+
'numArgs' => 1,
58+
'numOptArgs' => 1,
59+
'mode' => PhpLatex_Parser::MODE_TEXT,
60+
'environs' => array('itemize', 'enumerate', 'tabular'),
5861
),
5962
'array' => array(
60-
'numArgs' => 1,
61-
'mode' => PhpLatex_Parser::MODE_MATH,
63+
'numArgs' => 1,
64+
'mode' => PhpLatex_Parser::MODE_MATH,
6265
),
6366
);

tests/PhpLatex/Test/ParserTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,10 @@ public function testNewlinesAfterComment()
128128
$tree = $this->parser->parse("A% comment\n\nB");
129129
$this->assertSame("A\par B", PhpLatex_Renderer_Abstract::toLatex($tree));
130130
}
131+
132+
public function testEnvironmentWithArgsConfig()
133+
{
134+
$tree = $this->parser->parse("\\begin{tabular}[t]{|c|c|c|}\ncell1 & cell2 & cell3\n\\end{tabular}");
135+
$this->assertSame("\\begin{tabular}[{t}]{|c|c|c|}\n cell1 & cell2 & cell3\n\\end{tabular}", PhpLatex_Renderer_Abstract::toLatex($tree));
136+
}
131137
}

0 commit comments

Comments
 (0)