loilo/lowlight 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

loilo/lowlight

Composer 安装命令:

composer require loilo/lowlight

包简介

Show syntax-highlighted code of 150+ languages in your terminal

README 文档

README

Lowlight logo showing a couch and a shade lamp

Lowlight

Tests Packagist

Lowlight shows syntax-highlighted code of 150+ languages in your terminal. It's built on top of highlight.php.

Part of Lowlight's source code, highlighted with Lowlight

Installation

This package is available via Composer. To add it to your project, just run:

composer require loilo/lowlight

Usage

Preconditions

If you're not running Lowlight in a framework context, you may need to initialize Composer's autoloading first:

require_once __DIR__ . '/vendor/autoload.php';

Getting Started

To highlight a piece of PHP code, do this:

$ll = new Lowlight\Lowlight;

echo $ll->highlight('php', $somePhpSnippet)->value;

There are a lot of languages available. See the full list here.

Automatic Language Detection

If you're handling user-provided code, you may not always know the used language. That's where auto detection comes into play.

Let's say we got configuration code, but we aren't sure if it's JSON, YAML or INI, then the following approach would highlight it appropriately:

$ll = new Lowlight\Lowlight;

echo $ll->highlightAuto($userProvidedCode, [ 'json', 'yaml', 'ini' ])->value;

The second argument to the highlightAuto() method are the languages the code will be checked against.

  • Passing the parameter is optional. It defaults to [ 'xml', 'json', 'javascript', 'css', 'php', 'http' ].
  • You can adjust the default detection languages by calling
    $ll->setDefaultAutodetectLanguages([ 'json', 'yaml', 'ini' ]);

Theming

Theming works in analogy to highlight.js' theming. Highlight.js represents each token as CSS class name (e.g. hljs-comment) while Lowlight uses the tokens as keys in a theming array:

$ll->theme['comment'] = 'green';

As you can see, styling is quite limited in the terminal. Lowlight only allows you to set a token's text color, nothing else. Therefore, you usually won't be able to create the same amount of atmosphere through a theme as you can do in CSS (except, maybe, when using arbitrary RGB colors).

Colors

Available colors are:

  • default (the terminal's default text color)
  • black
  • gray / grey (actually the same)
  • white, bright-white
  • red, bright-red
  • green, bright-green
  • yellow, bright-yellow
  • blue, bright-blue
  • magenta, bright-magenta
  • cyan, bright-cyan

RGB Colors

Many modern terminals do support a 24-bit color palette. Instead of using the 16ish oldschool color names, you can provide an RGB array for a token:

$ll->theme['comment'] = [ 0, 175, 95 ];

If you want to use those, detecting support for 24-bit colors (as with all color-related features) is up to you.

Custom themes

To roll your own theme, it's a viable approach to take an existing highlight.js theme, extract all .hljs-xxx classes and transform them to a PHP associative array.

Note: You usually don't know the general style of the user's terminal and how your theme integrates with that (most notably if they're using a dark or a light theme), so be careful and restrictive with the colors you use.

By the way, this is Lowlight's default theme:

$ll->theme = [
    'comment' => 'gray',
    'string' => 'blue',
    'number' => 'green',
    'literal' => 'bright-green',
    'meta' => 'cyan',
    'meta-string' => 'bright-cyan',

    'keyword' => 'yellow',
    'attribute' => 'yellow',
    'selector-tag' => 'yellow',
    'meta-keyword' => 'yellow',
    'doctag' => 'yellow',
    'name' => 'yellow',

    'type' => 'red',
    'subst' => 'red',
    'selector-id' => 'red',
    'selector-class' => 'red',
    'quote' => 'red',
    'template-tag' => 'red',
    'deletion' => 'red',

    'title' => 'magenta',
    'section' => 'magenta',
    'built_in' => 'magenta',

    'regexp' => 'bright-red',
    'symbol' => 'bright-red',
    'variable' => 'bright-red',
    'template-variable' => 'bright-red',
    'link' => 'bright-red',
    'selector-attr' => 'bright-red',
    'selector-pseudo' => 'bright-red',

    'built' => 'green',
    'bullet' => 'green',
    'code' => 'green',
    'addition' => 'green',

    'emphasis' => 'red',
    'strong' => 'red'
];

Credit

loilo/lowlight 适用场景与选型建议

loilo/lowlight 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.56k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2018 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 20
  • 点击次数: 23
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 20
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2018-08-22