Sha256: 56e5ecc13c9f8e5362eb64c76ca45db350447f82c139890bd381f98e2f7b5c02

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 KB

Contents

<?php

namespace MtHaml\Support\Twig;

use MtHaml\Environment;

/**
 * Example integration of MtHaml with Twig, by proxying the Lexer
 *
 * This lexer will parse Twig templates as HAML if their filename end with
 * `.haml`, or if the code starts with `{% haml %}`.
 *
 * Alternatively, use MtHaml\Support\Twig\Loader.
 *
 * <code>
 * $lexer = new \MtHaml\Support\Twig\Lexer($mthaml);
 * $lexer->setLexer($twig->getLexer());
 * $twig->setLexer($lexer);
 * </code>
 */
class Lexer implements \Twig_LexerInterface
{
    protected $env;
    protected $lexer;

    public function __construct(Environment $env)
    {
        $this->env = $env;
    }

    public function setLexer(\Twig_LexerInterface $lexer)
    {
        $this->lexer = $lexer;
    }

    public function tokenize($code, $filename = null)
    {
        if (preg_match('#^\s*{%\s*haml\s*%}#', $code, $match)) {
            $padding = str_repeat(' ', strlen($match[0]));
            $code = $padding . substr($code, strlen($match[0]));
            $code = $this->env->compileString($code, $filename);
        } elseif (null !== $filename && 'haml' === pathinfo($filename, PATHINFO_EXTENSION)) {
            $code = $this->env->compileString($code, $filename);
        }

        return $this->lexer->tokenize($code, $filename);
    }
}

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
guard-mthaml-0.4.0 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php
guard-mthaml-0.3.1 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php
guard-mthaml-0.3.0 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php
guard-mthaml-0.2.5 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php
guard-mthaml-0.2.4 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php
guard-mthaml-0.2.3 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php
guard-mthaml-0.2.2 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php
guard-mthaml-0.2.1 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php
guard-mthaml-0.2.0 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php
guard-mthaml-0.1.0 vendor/mthaml/mthaml/lib/MtHaml/Support/Twig/Lexer.php