Add escape directive

This commit is contained in:
Philipp Lang 2022-11-03 14:47:03 +01:00
parent 5a68cce19e
commit c0e77ec198
3 changed files with 30 additions and 0 deletions

View File

@ -4,6 +4,7 @@ namespace Zoomyboy\Tex;
use Illuminate\Contracts\Support\Responsable; use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\File; use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Log; use Log;
@ -34,6 +35,13 @@ abstract class BaseCompiler implements Responsable
return $this; return $this;
} }
public function storeAs(string $directory, string $name, string $disk): void
{
$contents = $this->file->getContent();
Storage::disk($disk)->put($name, $contents);
}
public function toResponse($request) public function toResponse($request)
{ {
return response()->file($this->file->getRealPath(), [ return response()->file($this->file->getRealPath(), [

View File

@ -9,4 +9,25 @@ class BladeCompiler extends BaseBladeCompiler
protected $contentTags = ['<<<', '>>>']; protected $contentTags = ['<<<', '>>>'];
protected $rawTags = ['<<<!!', '!!>>>']; protected $rawTags = ['<<<!!', '!!>>>'];
protected string $slashReplace = 'GuYeuquexaeReu8uaedeel2PooYahtha';
public function registerEscapeDirective(): void
{
$this->directive('escape', fn ($expression) => implode('', [
'<?php echo str('.$expression.')',
'->replace("\\\", "'.$this->slashReplace.'")',
'->replace("$", "\\\\$")',
'->replace("#", "\\\\#")',
'->replace("%", "\\\\%")',
'->replace("&", "\\\\&")',
'->replace("_", "\\\\_")',
'->replace("{", "\\\\{")',
'->replace("}", "\\\\}")',
'->replace("~", "$\\\textasciitilde$")',
'->replace("^", "$\\\textasciicircum$")',
'->replace("'.$this->slashReplace.'", "$\\\textbackslash$")',
'; ?>',
]));
}
} }

View File

@ -16,6 +16,7 @@ class TexServiceProvider extends ServiceProvider
{ {
view()->addExtension('tex', 'tex', function () { view()->addExtension('tex', 'tex', function () {
$compiler = new BladeCompiler(app('files'), config('view.compiled')); $compiler = new BladeCompiler(app('files'), config('view.compiled'));
$compiler->registerEscapeDirective();
return new CompilerEngine($compiler, app('files')); return new CompilerEngine($compiler, app('files'));
}); });