在Notepad ++自定义语言中突出显示关键字之类的数字(用于访问日志)

时间:2012-09-13 09:54:24

标签: syntax-highlighting notepad++ http-status-codes

我想在Notepad ++中为访问日志编写自定义语言。

问题是数字(此处:HTTP状态代码)不会像真实关键字(即GET)那样突出显示。 Notepad ++通常只为数字提供高亮颜色。

如何处理文字等数字?

示例日志文件

192.23.0.9 - - [10/Sep/2012:13:46:42 +0200] "GET /js/jquery-ui.custom.min.js HTTP/1.1" 200 206731
192.23.0.9 - - [10/Sep/2012:13:46:43 +0200] "GET /js/onmediaquery.min.js HTTP/1.1" 200 1229
192.23.0.9 - - [10/Sep/2012:13:46:43 +0200] "GET /en/contact HTTP/1.1" 200 12836
192.23.0.9 - - [10/Sep/2012:13:46:44 +0200] "GET /en/imprint HTTP/1.1" 200 17380
192.23.0.9 - - [10/Sep/2012:13:46:46 +0200] "GET /en/nothere HTTP/1.1" 404 2785

自定义语言示例
http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=User_Defined_Language_Files

我也尝试过编辑和导入这样的预定义语言:
http://notepad-plus.sourceforge.net/commun/userDefinedLang/Log4Net.xml

我认为自定义语言应如下所示:

<KeywordLists>
[...]
    <Keywords name="Words1">404 501</Keywords>
    <Keywords name="Words2">301 303</Keywords>
    <Keywords name="Words3">200</Keywords>
</KeywordLists>

<Styles>
    <WordsStyle name="DEFAULT" styleID="11" fgColor="000000" bgColor="FFFFFF" colorStyle="0" fontName="Courier New" fontStyle="0"/>
    [...]
    <WordsStyle name="KEYWORD1" styleID="5" fgColor="FF0000" bgColor="FFFFFF" colorStyle="1" fontName="" fontStyle="0"/>
    <WordsStyle name="KEYWORD2" styleID="6" fgColor="0000FF" bgColor="FFFFFF" colorStyle="1" fontName="" fontStyle="1"/>
    <WordsStyle name="KEYWORD3" styleID="7" fgColor="00FF00" bgColor="FFFFFF" colorStyle="1" fontName="" fontStyle="0"/>
    [...]

    // This line causes number highlighting. Deletion doesn't work either.
    <WordsStyle name="NUMBER" styleID="4" fgColor="0F7F00" bgColor="FFFFFF" fontName="" fontStyle="0"/>
</Styles>

不幸的是,数字会以相同的颜色着色。

我想像这样给他们上色:
Sample highlighting of numbers in the access log

有什么建议吗?如何处理像关键字这样的数字?

1 个答案:

答案 0 :(得分:2)

无法将数字突出显示为关键字,因为内置词法分析器(解析器/语言定义)使用数字作为标记,这意味着区分数字和您的数字的唯一方法关键字将解析整个数字块,然后与关键字列表进行比较,在这种情况下,还需要解析数字块周围的分隔符,以确保.200.不突出显示为200 。这就是为什么你的数字都突出显示为相同的颜色;即“数字”颜色。

虽然可以使用自定义词法分析器使用固定位置标记或正则表达式匹配来完成,但您会发现用户定义的语言(我听到的最后一种语言)没有此功能。

由于您的请求实际上相当简单,从我的理解,尽可能一般(按照您的评论中的要求)......

Highlight space delimited numeric values contained in a given set.

    [[:space:]](200|301|404)[[:space:]]

我们可以使用“查找”对话框中的“标记”功能和正则表达式,但是然后所有内容都标记为与失败实验相同的颜色。

也许简单且适合您需求的是使用Mark Style中的npp pythonscript和Style Configurator设置来获得所需的结果?

像这样粗糙的宏观风格:

from Npp import *

def found(line, m):
    global first
    pos = editor.positionFromLine(line)
    if first:
        editor.setSelection(pos + m.end(), pos + m.start())
        first = False
    else:
        editor.addSelection(pos + m.end(), pos + m.start())


editor.setMultipleSelection(True)
lines = editor.getUserLineSelection()

# Use space padded search since MARKALLEXT2 will act just
# like the internal lexer if only the numeric is selected
# when it is called.

first = True
editor.pysearch( " 200 ", found, 0, lines[0], lines[1])
notepad.menuCommand(MENUCOMMAND.SEARCH_MARKALLEXT1)

first = True
editor.pysearch( " 301 ", found, 0, lines[0], lines[1])
notepad.menuCommand(MENUCOMMAND.SEARCH_MARKALLEXT2)

first = True
editor.pysearch( " 404 ", found, 0, lines[0], lines[1])
notepad.menuCommand(MENUCOMMAND.SEARCH_MARKALLEXT3)

使用,只需使用插件管理器安装Python Script,转到插件菜单并选择New Script然后粘贴,保存,选择要解析的文档的标签,以及执行脚本(再次从插件菜单中)。

显然你可以将所有5种Mark样式用于不同的术语,你可以分配给一个快捷方式,你可以更多地进入nppPython的'脚本'-vs-'宏'样式,并制作一个完整的脚本来解析任何东西你想要...只要你选择一个特定的词法分析器风格,就可以使用脚本触发器进行拍摄。

相关问题