Skip to content

Commit

Permalink
Add support for spaces commands
Browse files Browse the repository at this point in the history
  • Loading branch information
xemlock committed Sep 20, 2020
1 parent 2fb8f28 commit abb9faa
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ trim_trailing_whitespace = false

[Makefile]
indent_style = tab

[data/*.txt]
trim_trailing_whitespace = false
1 change: 0 additions & 1 deletion data/TODO.txt

This file was deleted.

4 changes: 4 additions & 0 deletions data/both-spaces.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\
\,
\enspace
\quad
42 changes: 34 additions & 8 deletions data/compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@
continue;
}

$fileCommands = array_filter(array_map('trim', file($file)), 'strlen');

// command mode is stored in file name
if (!preg_match('#^(?P<mode>both|math|text)#', basename($file), $match)) {
continue;
}

$fileCommands = array_filter(array_map(function ($str) {
$str = ltrim($str);

// skip comments
if (substr($str, 0, 1) === '%') {
return '';
}

return rtrim($str, "\n");
}, file($file)), 'strlen');
$mode = $match['mode'];

foreach ($fileCommands as $command) {
// extract command name and number of args

if (!preg_match('#^(?P<command>\\\\([a-zA-Z]+|[^a-zA-Z]))#', $command, $match)) {
if (!preg_match('#^(?P<command>\\\\([a-zA-Z]+|[^a-zA-Z]| ))#', $command, $match)) {
throw new Exception(
sprintf('File %s contains invalid command name %s', $file, $command)
sprintf("File %s contains invalid command name '%s'", $file, $command)
);
}

Expand All @@ -43,9 +51,27 @@
}
}

$commands[$name]['mode'] = $mode;
$commands[$name]['numArgs'] = $numArgs;
$commands[$name]['numOptArgs'] = $numOptArgs;
if (isset($commands[$name])) {
if ($commands[$name]['numArgs'] !== $numArgs) {
throw new Exception(sprintf(
'Duplicate definition of %s, conflicting number of arguments %d vs %d',
$name, $commands[$name]['numArgs'], $numArgs
));
}
if ($commands[$name]['numOptArgs'] !== $numOptArgs) {
throw new Exception(sprintf(
'Duplicate definition of %s, conflicting number of optional arguments %d vs %d',
$name, $commands[$name]['numOptArgs'], $numOptArgs
));
}
if ($commands[$name]['mode'] !== 'both' && $commands[$name]['mode'] !== $mode) {
$commands[$name]['mode'] = 'both';
}
} else {
$commands[$name]['mode'] = $mode;
$commands[$name]['numArgs'] = $numArgs;
$commands[$name]['numOptArgs'] = $numOptArgs;
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions library/PhpLatex/Renderer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,16 @@ protected function _renderNode($node, $flags = 0)
case '\\$':
return substr($node->value, 1);

// spaces, based on https://en.wikipedia.org/wiki/Whitespace_character#Unicode
case '\\ ':
return '&nbsp;';
case '\\,':
return '&thinsp;';
case '\\enspace':
return '&ensp;';
case '\\quad':
return '&emsp;';

case '\\ref':
// TODO if ref target resides in math mode render \\ref, so that
// it can be handled by JS.
Expand Down
Loading

0 comments on commit abb9faa

Please sign in to comment.