承接 mensbeam/lit 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mensbeam/lit

Composer 安装命令:

composer require mensbeam/lit

包简介

TextMate-like syntax highlighting in PHP

README 文档

README

Lit is a multilanguage syntax highlighter written in PHP. It takes code as input and returns an HTML pre element containing the code highlighted using span elements with classes based upon tokens in the code. It is loosely based upon Atom's Highlights which is used in the Atom text editor to syntax highlight code. Atom's Highlights is in turn based upon TextMate's syntax highlighting using its concepts of scope selectors and common keywords for components of programming languages. Lit is not a port of Atom's Highlights but instead an independent implementation of what I can understand of TextMate's grammar syntax, parsing, and tokenization by analyzing other implementations. It aims to at least have feature parity or better with Atom's Highlights.

Warning Before Using

This library is experimental. The code is not tested at all, and writing tests for it will be an incredible undertaking because there's no specification whatsoever to test against. It would require creating a specification as well which is beyond the scope of a project that exists just to scratch an itch. Atom's Highlights is also barely tested itself for the same reason. There's numerous PHP libraries out there without a test suite; not having one is not up to our usual standards, though. So, that's why this warning exists.

Documentation

MensBeam\Lit\Grammar::__construct

Creates a new MensBeam\Lit\Grammar object.

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

Parameters

In normal usage of the library the parameters won't be used (see MensBeam\Lit\Grammar::loadJSON and examples below for more information), but they are listed below for completeness' sake.

scopeName - The scope name of the grammar
patterns - The list of patterns in the grammar
name - A human-readable name for the grammar
injections - The list of injections in the grammar
repository - The list of repository items in the grammar

MensBeam\Lit\Grammar::loadJSON

Imports an Atom JSON grammar into the MensBeam\Lit\Grammar object.

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

Parameters

filename - The JSON file to be imported

MensBeam\Lit\GrammarRegistry::clear

Clears all grammars from the registry

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

MensBeam\Lit\GrammarRegistry::get

Retrieves a grammar from the registry

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

Parameters

scopeName - The scope name (eg: text.html.php) of the grammar that is being requested

Return Values

Returns a MensBeam\Lit\Grammar object on success and false on failure.

MensBeam\Lit\GrammarRegistry::set

Retrieves a grammar from the registry

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

Parameters

scopeName - The scope name (eg: text.html.php) of the grammar that is being set
grammar - The grammar to be put into the registry

Return Values

Returns true on success and false on failure.

MensBeam\Lit\Highlight::toElement

Highlights incoming string data and outputs a PHP DOMElement.

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

Parameters

data - The input data string
scopeName - The scope name (eg: text.html.php) of the grammar that's needed to highlight the input data
document - An existing DOMDocument to use as the owner document of the returned DOMElement; if omitted one will be created instead
encoding - The encoding of the input data string; only used if a document wasn't provided in the previous parameter, otherwise it uses the encoding of the existing DOMDocument; defaults to HTML standard default windows-1252

Return Values

Returns a pre DOMElement.

MensBeam\Lit\Highlight::toString

Highlights incoming string data and outputs a string containing serialized HTML.

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

Parameters

data - The input data string
scopeName - The scope name (eg: text.html.php) of the grammar that's needed to highlight the input data
encoding - The encoding of the input data string; defaults to HTML standard default windows-1252

Return Values

Returns a string.

Examples

Here's an example of highlighting PHP code:

$code = <<<CODE
<?php
echo "🐵 OOK! 🐵";
?>
CODE;

// Use UTF-8 as the encoding to preserve the emojis.
$element = MensBeam\Lit\Highlight::toElement($code, 'text.html.php', null, 'UTF-8');
$element->setAttribute('class', 'highlighted');

// Use PHP DOM's DOMDocument::saveHTML method to print the highlighted markup
// when finished with manipulating it.
echo $element->ownerDocument->saveHTML($element);

This will produce:

<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>

An already existing DOMDocument may be used as the owner document of the returned pre element:

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

Other DOM libraries which inherit from and/or encapsulate PHP's DOM such as MensBeam\HTML-DOM may also be used:

...
$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;

Of course Lit can simply output a string, too:

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

Lit has quite a long list of out-of-the-bag supported languages, but sometimes other languages need to be highlighted:

...
// 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);

Supported Languages & Formats

  • AppleScript
  • C
  • C#
  • C# Cake file
  • C# Script file
  • C++
  • CoffeeScript
  • CSS
  • Diff
  • Github Flavored Markdown
  • Git config
  • Go
  • Go modules
  • Go templates
  • HTML
  • Java
  • Java expression language
  • Java properties
  • JavaScript
  • JavaScript Regular Expressions
  • JSDoc
  • JSON
  • Less
  • Lua
  • Makefile
  • Markdown (CommonMark)
  • Objective C
  • Perl
  • Perl 6
  • PHP
  • Plist
  • Plist (XML, old-style)
  • Python
  • Python console
  • Python Regular Expressions
  • Python traceback
  • Ruby
  • Ruby gemfile
  • Ruby on Rails (RJS)
  • Rust
  • Sass
  • SassDoc
  • SCSS
  • Shell (Bash)
  • Shell session (Bash)
  • SQL
  • SQL (Mustache)
  • Textile
  • Todo
  • XML
  • XSL

mensbeam/lit 适用场景与选型建议

mensbeam/lit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mensbeam/lit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mensbeam/lit 我们能提供哪些服务?
定制开发 / 二次开发

基于 mensbeam/lit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-11