SyntaxHighlighter 使你的代码像csdn一样


摘要:SyntaxHighlighter,是一款用于web页面的代码着色工具,可以用来着色多种语言,可以是HTML,CSS,Javascript,还可以是C,JAVA等编程语言,本文不对该开源项目进行解析,只是简单阐述一下它的几种用法。该项目可以从http://code.google.com/p/syntaxhighlighter/获得。

本站文章除非注明,否则都为原创,转载请注明出处。

本文链接:SyntaxHighlighter 使你的代码像csdn一样

1. 下载该项目源代码,解压放在网站的目录下,得到该项目的访问路径,示例中的路径是:http://uefacl-cn.com/home/highlight/,定义该路径为__HIGHLIGHT_PATH__,你可以访问http://uefacl-cn.com/home/highlight/Styles/SyntaxHighlighter.css来进行测试

2. 在需要加入代码特效的网页(或者直接在生成网页的模版)中的<head></head>之间加入如下代码:

<link type=”text/css” rel=”stylesheet” href=”http://uefacl-cn.com/home/highlight/Styles/SyntaxHighlighter.css”></link> 以引用该css。如果使用php进行动态网页构建,可以采取如下引用方式:

<link type=”text/css” rel=”stylesheet” href=”<?php echo __HIGHLIGHT_PATH__; ?>SyntaxHighlighter.css”></link>

3. 在需要加入代码特效的位置添加如下代码:当然textarea的其他属性需要由你自己定制。

<textarea name=”code” style=”resize: none; padding: 3px; font-family: ‘Courier New’,Courier,monospace; width: 100%; height: 280px;” rows=”12″>

这里写下你的代码就可以了。

</textarea>

4. 在网页尾部的</body>之前插入以下代码,也可以在模版页面footer.php中加入(一般的动态网站或者建站平台都是这样的吧?)

<script src=”http://uefacl-cn.com/home/highlight/Scripts/shCore.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushCSharp.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushPhp.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushJScript.js”></script>

<script src=”http://uefacl-cn.com/home/highlight/Scripts/shBrushJava.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushVb.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushSql.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushXml.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushDelphi.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushPython.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushRuby.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushCss.js”></script>

<script src=”<?php echo __HIGHLIGHT_PATH__; ?>Scripts/shBrushCpp.js”></script>

<script>

dp.SyntaxHighlighter.HighlightAll(‘code’);

</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
PyQt 中的 `QSyntaxHighlighter` 类可以用于实现文本编辑器中的语法高亮。以下是一个简单的示例: ```python from PyQt5.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter from PyQt5.QtCore import Qt, QRegExp class SyntaxHighlighter(QSyntaxHighlighter): def __init__(self, parent=None): super(SyntaxHighlighter, self).__init__(parent) keyword_format = QTextCharFormat() keyword_format.setForeground(Qt.darkBlue) keyword_format.setFontWeight(QFont.Bold) keyword_patterns = ["\\bdef\\b", "\\bclass\\b", "\\bif\\b", "\\belif\\b", "\\belse\\b", "\\bwhile\\b", "\\bfor\\b"] self.highlighting_rules = [(QRegExp(pattern), keyword_format) for pattern in keyword_patterns] def highlightBlock(self, text): for pattern, format in self.highlighting_rules: expression = QRegExp(pattern) index = expression.indexIn(text) while index >= 0: length = expression.matchedLength() self.setFormat(index, length, format) index = expression.indexIn(text, index + length) ``` 在上面的示例中,我们定义了一个名为 `SyntaxHighlighter` 的类,该类继承自 PyQt 中的 `QSyntaxHighlighter` 类。在 `__init__` 方法中,我们定义了一些关键字的格式和正则表达式模式。在 `highlightBlock` 方法中,我们会对输入的文本进行匹配,并且对匹配到的关键字进行高亮处理。 为了使用该类,我们需要将其应用于文本编辑器的某个部分。例如: ```python from PyQt5.QtWidgets import QApplication, QTextEdit, QVBoxLayout, QWidget app = QApplication([]) editor = QTextEdit() highlighter = SyntaxHighlighter(editor.document()) layout = QVBoxLayout() layout.addWidget(editor) widget = QWidget() widget.setLayout(layout) widget.show() app.exec_() ``` 在上面的示例中,我们首先创建了一个 `QTextEdit` 部件,然后创建了一个 `SyntaxHighlighter` 实例,并将其应用于 `QTextEdit` 部件的文档中。最后我们将 `QTextEdit` 部件添加到一个 `QVBoxLayout` 中,并将其添加到一个 `QWidget` 中进行显示。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值