highlight_用于Text_Highlight的JSON渲染器

highlight

highlight

Text_Highlighter is one of my favourite PEAR packages, seems like I'm addicted to highlighting source code. After adding BB code and simple HTML renderers and an ABAP code syntax definition, today I played with adding a JSON renderer. Useful in case you want to get highlighted source code in your new shiny AJAX app.

Text_Highlighter是我最喜欢的PEAR软件包之一,似乎让我沉迷于突出显示源代码。 在添加了BB代码和简单HTML渲染器以及ABAP代码语法定义之后,今天我开始添加JSON渲染器。 如果希望在新的闪亮AJAX应用程序中获得突出显示的源代码,该功能很有用。

阵列渲染器 (Array renderer)

After I did the JSON renderer, I said, OK, what if I want to tweak the JSON output just a bit (or the output from any renderer for that matter)? Add more options? Nah, I had a better idea, I scrapped the whole thing and did an Array renderer first. If you have the array output from the renderer, it's trivial to format it as JSON, or XML, or HTML, or anything. I believe even the exisitng Text_Highlighter renderers should be rewritten, to extend an Array renderer. Anyway, back to JSON.

在完成JSON渲染器之后,我说,好的,如果我想稍微调整JSON输出(或与此相关的任何渲染器的输出)怎么办? 添加更多选项? 不,我有一个更好的主意,我废弃了整件事,先做了一个Array渲染器。 如果您具有来自渲染器的数组输出,则将其格式化为JSON或XML或HTML或其他任何格式都是很简单的。 我相信甚至应该重写现有的Text_Highlighter渲染器,以扩展Array渲染器。 无论如何,回到JSON。

演示版 (Demo)

To see the JSON renderer in action, you can go to my hiliteme.com site and check the JSON tab.

要查看运行中的JSON渲染器,您可以转到我的hiliteme.com网站并检查JSON选项卡。

资源 (Source)

The source code is available here - JSON.phps which extends Array.phps. To test, you need to add the two renderers to your PEAR repository under Text/Highlighter/Renderer

源代码在这里可用-JSON.phps ,它扩展了Array.phps 。 要进行测试,您需要将两个渲染器添加到Text / Highlighter / Renderer下的PEAR存储库中

(Example)

So let's say you need to highlight the PHP code

因此,假设您需要突出显示PHP代码

<?php 
    echo "Hello Highlighted World!"; 
?>

You create an instance of Text_Highlighter and Text_Highlighter_Renderer_JSON and call the highlight() method, assuming that the code you need highlighted is in $source

您创建Text_Highlighter和Text_Highlighter_Renderer_JSON的实例,并调用highlight()方法,假设您需要突出显示的代码在$source

<?php
// dependencies
require_once 'Text/Highlighter.php';
require_once 'Text/Highlighter/Renderer/JSON.php';
 
// instance
$json_options = array();
$json_options['tabsize'] = 4;
$json_options['enumerated'] = true;
$renderer =& new Text_Highlighter_Renderer_JSON($json_options);
$highlighter =& Text_Highlighter::factory($_POST['language']);
$highlighter->setRenderer($renderer);
 
// do the highlighting
$json_result = $highlighter->highlight($source);
?>

Now $json_result will look like:

现在$ json_result看起来像:

[["inlinetags","&lt;?php"],["code"," \\n    "],["reserved","echo"],["code"," "],["quotes","&quot;"],["string","Hello Highlighted World!"],["quotes","&quot;"],["code","; \\n"],["inlinetags","?&gt;"]]

As you see the JSON output is an array, one element per highlighted keyword, and in this array there is a sub array - class/keyword. If you want to display this in your page (let's say you got it from an AJAX call), you can do a loop through the array and surround the keywords with span tags of the selected style:

如您所见,JSON输出是一个数组,每个突出显示的关键字一个元素,并且在此数组中有一个子数组-class / keyword。 如果要在页面中显示此内容(假设您是通过AJAX调用获得的),则可以遍历数组,并用所选样式的span标签将关键字括起来:

// say your ajax call returned var src 
// var src = xmlhttp.responseText;
var data = eval(src); 
 
var res = ''; 
for (var i in data) {
    res += '<span class="hl-' + data[i][0] + '">';
    res += data[i][1];
    res += '</span>';
}
 
var el = document.getElementById('some-div').
el.innerHTML = '<pre>' + res + '</pre>';

Here I used innerHTML, you can also use DOM, but in this case you need a special case for the "\n" so that you can create a br element to address IE's habit of ignoring line feeds in a DOM-generated pre tag.

在这里,我使用了innerHTML ,也可以使用DOM,但是在这种情况下,您需要为"\n"一个特殊情况,以便创建br元素来解决IE忽略DOM生成的pre标记中的换行符的习惯。

BTW, if you don't set the enumerated option to true, you'll get objects inside the main array, check hiliteme.com's JSON tab for an idea of how this would look like.

顺便说一句,如果您没有将枚举选项设置为true,则会在主数组中获取对象,请查看hiliteme.com的JSON标签以了解其外观。

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/json-renderer-for-text_highlight/

highlight

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值