PHP code example of mensbeam / lit

1. Go to this page and download the library: Download mensbeam/lit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mensbeam / lit example snippets


public function MensBeam\Lit\Grammar::__construct(?string $scopeName = null, ?array $patterns = null, ?string $name = null, ?array $injections = null, ?array $repository = null)

public function MensBeam\Lit\Grammar::loadJSON(string $filename)

public static function MensBeam\Lit\GrammarRegistry::clear()

public static function MensBeam\Lit\GrammarRegistry::get(string $scopeName): Grammar|false

public static function MensBeam\Lit\GrammarRegistry::set(string $scopeName, MensBeam\Lit\Grammar $grammar): bool

public static MensBeam\Lit\Highlight::toElement(string $data, string $scopeName, ?\DOMDocument $document = null, string $encoding = 'windows-1252'): \DOMElement

public static MensBeam\Lit\Highlight::toString(string $data, string $scopeName, string $encoding = 'windows-1252'): string

$code = <<<CODE

echo "🐵 OOK! 🐵";

...
$document = new DOMDocument();
// $element will be owned by $document.
$element = MensBeam\Lit\Highlight::toElement($code, 'text.html.php', $document);

...
$document = new MensBeam\HTML\DOM\Document();
// $element will be owned by $document.
$element = MensBeam\Lit\Highlight::toElement($code, 'text.html.php', $document->innerNode);
$element = $element->ownerDocument->getWrapperNode($element);
// MensBeam\HTML\DOM\Element can simply be cast to a string to serialize.
$string = (string)$element;

...
$string = MensBeam\Lit\Highlight::toString($code, 'text.html.php');

...
// Import a hypothetical Ook Atom JSON language grammar into a Grammar object
// and add it to the registry.
$grammar = new MensBeam\Lit\Grammar;
$grammar->loadJSON('/path/to/source.ook.json');
MensBeam\Lit\GrammarRegistry::set($grammar->scopeName, $grammar);

// Now the grammar can be used to highlight code
$element = MensBeam\Lit\Highlight::toElement($code, $grammar->scopeName);
html
<pre class="highlighted"><code class="text html php"><span class="meta embedded block php"><span class="punctuation section embedded begin php">&lt;?php</span><span class="source php">
<span class="support function construct output php">echo</span> <span class="string quoted double php"><span class="punctuation definition string begin php">"</span>🐵 OOK! 🐵<span class="punctuation definition string end php">"</span></span><span class="punctuation terminator expression php">;</span>
</span><span class="punctuation section embedded end php"><span class="source php">?</span>&gt;</span></span></code></pre>